Skip to content

Commit 6d8296b

Browse files
committed
Fix stale resize run's finally block corrupting concurrent measurement styles
When two resize() calls run concurrently, the stale run's finally block would restore DOM styles that the active run still needs for measurement. Additionally, the active run would snapshot measurement styles (set by the earlier run) as 'previous', permanently leaving the page with overflow: visible. Fix: Store original styles in a shared ref that is only captured when no measurement is already in progress. The finally block only restores styles when the run is still the latest one — stale runs skip cleanup entirely, leaving the DOM for the active run to handle.
1 parent acd6682 commit 6d8296b

1 file changed

Lines changed: 45 additions & 23 deletions

File tree

‎apps/desktop-tauri/src/surfaces/TrayPanel.tsx‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ export default function TrayPanel({ state }: { state: BootstrapState }) {
7676
const [layoutReady, setLayoutReady] = useState(false);
7777
const layoutReadyRef = useRef(false);
7878
const resizeRunRef = useRef(0);
79+
const originalStylesRef = useRef<{
80+
htmlOverflow: string;
81+
bodyOverflow: string;
82+
bodyMinHeight: string;
83+
surfaceMaxHeight: string;
84+
surfaceOverflow: string;
85+
bodyInnerOverflow: string | undefined;
86+
bodyFlex: string | undefined;
87+
stackOverflow: string | undefined;
88+
} | null>(null);
7989

8090
// Cards to display based on mode
8191
// Overview: all providers in the grid — non-error first, then errors
@@ -115,16 +125,21 @@ export default function TrayPanel({ state }: { state: BootstrapState }) {
115125
const body = surface.querySelector<HTMLElement>(".menu-surface__body");
116126
const stack = surface.querySelector<HTMLElement>(".menu-stack");
117127

118-
const previous = {
119-
htmlOverflow: document.documentElement.style.overflow,
120-
bodyOverflow: document.body.style.overflow,
121-
bodyMinHeight: document.body.style.minHeight,
122-
surfaceMaxHeight: surface.style.maxHeight,
123-
surfaceOverflow: surface.style.overflow,
124-
bodyInnerOverflow: body?.style.overflow,
125-
bodyFlex: body?.style.flex,
126-
stackOverflow: stack?.style.overflow,
127-
};
128+
// Only capture original styles if no other run already has them saved.
129+
// This prevents a later run from snapshotting the measurement styles
130+
// applied by an earlier concurrent run.
131+
if (!originalStylesRef.current) {
132+
originalStylesRef.current = {
133+
htmlOverflow: document.documentElement.style.overflow,
134+
bodyOverflow: document.body.style.overflow,
135+
bodyMinHeight: document.body.style.minHeight,
136+
surfaceMaxHeight: surface.style.maxHeight,
137+
surfaceOverflow: surface.style.overflow,
138+
bodyInnerOverflow: body?.style.overflow,
139+
bodyFlex: body?.style.flex,
140+
stackOverflow: stack?.style.overflow,
141+
};
142+
}
128143
let committedHeight = false;
129144

130145
document.documentElement.style.overflow = "visible";
@@ -181,19 +196,26 @@ export default function TrayPanel({ state }: { state: BootstrapState }) {
181196
setLayoutReady(true);
182197
}
183198
} finally {
184-
if (!committedHeight) {
185-
surface.style.maxHeight = previous.surfaceMaxHeight;
186-
}
187-
surface.style.overflow = previous.surfaceOverflow;
188-
document.documentElement.style.overflow = previous.htmlOverflow;
189-
document.body.style.overflow = previous.bodyOverflow;
190-
document.body.style.minHeight = previous.bodyMinHeight;
191-
if (body) {
192-
body.style.overflow = previous.bodyInnerOverflow ?? "";
193-
body.style.flex = previous.bodyFlex ?? "";
194-
}
195-
if (stack) {
196-
stack.style.overflow = previous.stackOverflow ?? "";
199+
// Only restore styles if this is still the latest run.
200+
// A stale run must not touch the DOM — a newer run is still
201+
// using the measurement styles and will handle cleanup itself.
202+
if (run === resizeRunRef.current) {
203+
const previous = originalStylesRef.current!;
204+
originalStylesRef.current = null;
205+
if (!committedHeight) {
206+
surface.style.maxHeight = previous.surfaceMaxHeight;
207+
}
208+
surface.style.overflow = previous.surfaceOverflow;
209+
document.documentElement.style.overflow = previous.htmlOverflow;
210+
document.body.style.overflow = previous.bodyOverflow;
211+
document.body.style.minHeight = previous.bodyMinHeight;
212+
if (body) {
213+
body.style.overflow = previous.bodyInnerOverflow ?? "";
214+
body.style.flex = previous.bodyFlex ?? "";
215+
}
216+
if (stack) {
217+
stack.style.overflow = previous.stackOverflow ?? "";
218+
}
197219
}
198220
}
199221
};

0 commit comments

Comments
 (0)