Файли
openidea/src/ui/useFrozenWhileWindowResizes.ts
T
l0ng-ai f4a7a7bafb perf(ui): smoother editor scrolling, tab switching and first file open (#4633)
Removes selectors that restyled whole subtrees on every inserted editor line or focus change (CodeMirror via a pnpm patch; selectorCost.test.ts guards them), measures space width once per font, shares one tooltip host, builds tab menus lazily, caches parsed SVG icons, prewarms an off-screen editor at idle, stats project-tree entries in parallel, and keeps the Git tool window at its size during a live window resize. Verified with full vitest, typecheck, ide-vfs cargo test and clippy, gitLog e2e, and a window resize in the app.
2026-11-20 09:13:29 +08:00

46 рядки
2.0 KiB
TypeScript

import { useEffect, type RefObject } from "react";
/** How long the window must keep its size before a frozen element follows it. */
export const WINDOW_RESIZE_SETTLE_MS = 150;
/**
* While the window is being resized, keeps the element at the size it had when the drag started,
* on its own layer and clipped by its parent, then lets it take the new size once the drag pauses.
* For dense views that WebKit cannot lay out and paint at every step of a live resize: their pixels
* are moved instead of redrawn, so the rest of the window keeps up with the pointer.
*/
export function useFrozenWhileWindowResizes(ref: RefObject<HTMLElement | null>): void {
useEffect(() => {
let frozen: { el: HTMLElement; parent: HTMLElement | null; parentOverflow: string } | null = null;
let timer: ReturnType<typeof setTimeout> | undefined;
const release = () => {
clearTimeout(timer);
if (!frozen) return;
const { el, parent, parentOverflow } = frozen;
for (const p of ["width", "height", "flex", "will-change"]) el.style.removeProperty(p);
if (parent) parent.style.overflow = parentOverflow;
frozen = null;
};
const onResize = () => {
const el = ref.current;
if (!frozen && el?.isConnected) {
const { width, height } = el.getBoundingClientRect();
const parent = el.parentElement;
frozen = { el, parent, parentOverflow: parent?.style.overflow ?? "" };
el.style.setProperty("width", `${width}px`, "important");
el.style.setProperty("height", `${height}px`, "important");
el.style.setProperty("flex", "none", "important");
el.style.setProperty("will-change", "transform");
if (parent) parent.style.overflow = "hidden";
}
clearTimeout(timer);
timer = setTimeout(release, WINDOW_RESIZE_SETTLE_MS);
};
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
release();
};
}, [ref]);
}