Skip to content

Commit 6181a55

Browse files
committed
Preserve absolute sidebar width when resizing browser window
1 parent 40798a7 commit 6181a55

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

web/src/lib/layout.svelte.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
import { GlobalOptions } from "$lib/global-options.svelte";
21
import type { Pane } from "paneforge";
32
import { clearCookie, setCookie } from "./util";
3+
import { watch } from "runed";
44

55
export const ROOT_LAYOUT_KEY = "diff-viewer-root-layout";
66
export interface PersistentLayoutState {
77
sidebarWidth: number;
88
}
99

1010
export class LayoutState {
11-
private readonly globalOptions: GlobalOptions;
12-
1311
sidebarCollapsed = $state(false);
1412

1513
windowInnerWidth: number | undefined = $state();
1614
sidebarPane: Pane | undefined = $state();
1715
lastSidebarWidth: number | undefined = $state();
1816

1917
minSidebarWidth = $derived.by(() => {
20-
return this.getProportion(200, 0);
18+
return this.getContainerProportion(200, 0);
2119
});
2220
defaultSidebarWidth = $derived.by(() => {
2321
if (this.lastSidebarWidth !== undefined) {
2422
return this.lastSidebarWidth;
2523
}
26-
return this.getProportion(350, 0.25);
24+
return this.getContainerProportion(350, 0.25);
2725
});
2826

2927
defaultMainWidth = $derived.by(() => {
@@ -35,18 +33,33 @@ export class LayoutState {
3533

3634
constructor(state: PersistentLayoutState | null) {
3735
this.lastSidebarWidth = state?.sidebarWidth;
38-
this.globalOptions = GlobalOptions.get();
36+
37+
// Maintain sidebar size when resizing window
38+
watch.pre(
39+
() => this.windowInnerWidth,
40+
(newValue, oldValue) => {
41+
if (oldValue !== undefined && newValue !== undefined && this.sidebarPane) {
42+
const oldPx = (this.sidebarPane.getSize() / 100) * oldValue;
43+
const newProportion = this.getProportion(oldPx, newValue);
44+
this.sidebarPane.resize(newProportion);
45+
}
46+
},
47+
);
3948
}
4049

4150
toggleSidebar() {
4251
this.sidebarCollapsed = !this.sidebarCollapsed;
4352
}
4453

45-
private getProportion(px: number, defaultValue: number) {
54+
private getContainerProportion(px: number, defaultValue: number) {
4655
if (this.windowInnerWidth === undefined) {
4756
return defaultValue;
4857
}
49-
return Math.max(0, Math.min(100, Math.ceil((px / this.windowInnerWidth) * 100)));
58+
return this.getProportion(px, this.windowInnerWidth);
59+
}
60+
61+
private getProportion(px: number, max: number) {
62+
return Math.max(0, Math.min(100, (px / max) * 100));
5063
}
5164

5265
resetLayout() {

0 commit comments

Comments
 (0)