diff --git a/components/aside-panel.resize.test.tsx b/components/aside-panel.resize.test.tsx index 80b67154c..565325c72 100644 --- a/components/aside-panel.resize.test.tsx +++ b/components/aside-panel.resize.test.tsx @@ -1,8 +1,47 @@ import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; import test from 'node:test'; import React from 'react'; import { AsidePanel } from './ui/aside-panel'; -import { createDomRenderer, dispatchDomEvent, installDomEnvironment } from './test-support/renderReactDom'; +import { createDomRenderer, dispatchDomEvent, installDomEnvironment, runWithAct } from './test-support/renderReactDom'; + +function installManualRaf(win: Window & typeof globalThis) { + let nextId = 1; + const scheduled = new Map(); + const cancelledIds: number[] = []; + let requestCount = 0; + + const originalRequest = win.requestAnimationFrame; + const originalCancel = win.cancelAnimationFrame; + + win.requestAnimationFrame = (cb: FrameRequestCallback) => { + requestCount += 1; + const id = nextId++; + scheduled.set(id, cb); + return id; + }; + win.cancelAnimationFrame = (id: number) => { + cancelledIds.push(id); + scheduled.delete(id); + }; + + return { + get requestCount() { return requestCount; }, + get pendingCount() { return scheduled.size; }, + cancelledIds, + async flushNext() { + const entries = Array.from(scheduled.entries()); + scheduled.clear(); + await runWithAct(() => { + for (const [, cb] of entries) cb(16); + }); + }, + restore() { + win.requestAnimationFrame = originalRequest; + win.cancelAnimationFrame = originalCancel; + }, + }; +} async function fixture(t: test.TestContext, layout: 'inline' | 'overlay' = 'inline') { const env = installDomEnvironment(); @@ -34,7 +73,7 @@ async function fixture(t: test.TestContext, layout: 'inline' | 'overlay' = 'inli else Reflect.deleteProperty(globalThis, 'localStorage'); env.cleanup(); }); - return { writes, render, unmount: () => renderer.unmount(), + return { writes, panel, render, unmount: () => renderer.unmount(), start: () => pointer(separator, 'pointerdown', 100), move: (x: number) => pointer(env.window, 'pointermove', x), up: (x: number) => pointer(env.window, 'pointerup', x), @@ -79,3 +118,88 @@ test('overlay resizing retains its existing desired-width behavior', async (t) = await f.start(); await f.move(120); await f.up(120); assert.deepEqual(f.writes, [700]); }); + +test('rapid pointermove events coalesce into one requestAnimationFrame using the latest width', async (t) => { + const f = await fixture(t); + const raf = installManualRaf(f.env.window); + t.after(() => raf.restore()); + + await f.start(); + // Fire 4 rapid moves within a single frame: 378 + (100 - x). + await f.move(110); // 368 + await f.move(120); // 358 + await f.move(130); // 348 + await f.move(140); // 338 + + assert.equal(raf.requestCount, 1, 'multiple pointermove events in one frame must schedule only one rAF'); + assert.equal(raf.pendingCount, 1); + assert.equal( + f.panel.style.width, + 'min(720px, var(--aside-inline-max-width, 720px))', + 'panel width must not re-render synchronously before the animation frame fires', + ); + + await raf.flushNext(); + assert.equal(raf.pendingCount, 0); + assert.equal( + f.panel.style.width, + 'min(338px, var(--aside-inline-max-width, 338px))', + 'flushed frame must apply the latest clamped width from the coalesced moves', + ); + + await f.up(140); + assert.deepEqual(f.writes, [338]); +}); + +test('pointerup cancels any pending rAF before committing the final width', async (t) => { + const f = await fixture(t); + const raf = installManualRaf(f.env.window); + t.after(() => raf.restore()); + + await f.start(); + await f.move(150); // schedules rAF for 328px + assert.equal(raf.pendingCount, 1); + + // Release at 110 (368px) before the queued rAF runs. + await f.up(110); + assert.equal(raf.cancelledIds.length, 1, 'cleanup on pointerup must cancel the pending rAF'); + assert.equal(raf.pendingCount, 0); + assert.deepEqual(f.writes, [368]); + assert.equal( + f.panel.style.width, + 'min(368px, var(--aside-inline-max-width, 368px))', + ); +}); + +for (const mode of ['cancel', 'hidden', 'unmounted'] as const) { + test(`${mode} cancels a pending drag rAF so no stale width commits afterward`, async (t) => { + const f = await fixture(t); + const raf = installManualRaf(f.env.window); + t.after(() => raf.restore()); + + await f.start(); + await f.move(140); + assert.equal(raf.pendingCount, 1); + + if (mode === 'cancel') await f.cancel(140); + else if (mode === 'hidden') await f.render(false); + else await f.unmount(); + + assert.equal(raf.cancelledIds.length, 1, `${mode} must cancel pending rAF`); + assert.equal(raf.pendingCount, 0); + assert.deepEqual(f.writes, []); + }); +} + +test('split-panel-enter animates compositor-only transform and opacity without width reflow', () => { + const css = readFileSync(new URL('../index.css', import.meta.url), 'utf8'); + const start = css.indexOf('@keyframes split-panel-enter'); + const end = css.indexOf('/* Security-key prompts', start); + assert.ok(start !== -1 && end > start); + const block = css.slice(start, end); + + assert.match(block, /transform:\s*translateX\(/); + assert.match(block, /will-change:\s*transform,\s*opacity;/); + assert.doesNotMatch(block, /\bwidth\s*:/); + assert.doesNotMatch(block, /\bmin-width\s*:/); +}); diff --git a/components/ui/aside-panel.tsx b/components/ui/aside-panel.tsx index 9b10399ca..b7cf4eaeb 100644 --- a/components/ui/aside-panel.tsx +++ b/components/ui/aside-panel.tsx @@ -271,9 +271,35 @@ export const AsidePanel: React.FC = ({ document.body.style.cursor = 'col-resize'; document.body.style.userSelect = 'none'; + let rafId: number | null = null; + let pendingWidth: number | null = null; + + const cancelPendingFrame = () => { + if (rafId !== null) { + window.cancelAnimationFrame?.(rafId); + rafId = null; + } + pendingWidth = null; + }; + const handlePointerMove = (moveEvent: PointerEvent) => { - setPanelWidthPx(moveEvent.clientX === startX ? panelWidthPx - : clampAsideInlineWidth(startWidth + startX - moveEvent.clientX)); + const nextWidth = moveEvent.clientX === startX + ? panelWidthPx + : clampAsideInlineWidth(startWidth + startX - moveEvent.clientX); + if (typeof window.requestAnimationFrame !== 'function') { + setPanelWidthPx(nextWidth); + return; + } + pendingWidth = nextWidth; + if (rafId !== null) return; + rafId = window.requestAnimationFrame(() => { + rafId = null; + if (pendingWidth !== null) { + const committedWidth = pendingWidth; + pendingWidth = null; + setPanelWidthPx(committedWidth); + } + }); }; const handlePointerUp = (upEvent: PointerEvent) => { cleanup(); @@ -287,6 +313,7 @@ export const AsidePanel: React.FC = ({ }; const cleanup = () => { resizeCleanupRef.current = null; + cancelPendingFrame(); setPanelWidthPx(panelWidthPx); setIsResizing(false); document.body.style.cursor = previousCursor; diff --git a/components/vault/VaultViewLayout.resize.test.tsx b/components/vault/VaultViewLayout.resize.test.tsx new file mode 100644 index 000000000..9cf3c9d25 --- /dev/null +++ b/components/vault/VaultViewLayout.resize.test.tsx @@ -0,0 +1,180 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; +import React from "react"; +import { + createDomRenderer, + dispatchDomEvent, + installDomEnvironment, + runWithAct, +} from "../test-support/renderReactDom"; +import { useVaultSidebarResize } from "./VaultViewLayout"; + +function installManualRaf(win: Window & typeof globalThis) { + let nextId = 1; + const scheduled = new Map(); + const cancelledIds: number[] = []; + let requestCount = 0; + + const originalRequest = win.requestAnimationFrame; + const originalCancel = win.cancelAnimationFrame; + + win.requestAnimationFrame = (cb: FrameRequestCallback) => { + requestCount += 1; + const id = nextId++; + scheduled.set(id, cb); + return id; + }; + win.cancelAnimationFrame = (id: number) => { + cancelledIds.push(id); + scheduled.delete(id); + }; + + return { + get requestCount() { + return requestCount; + }, + get pendingCount() { + return scheduled.size; + }, + cancelledIds, + async flushNext() { + const entries = Array.from(scheduled.entries()); + scheduled.clear(); + await runWithAct(() => { + for (const [, cb] of entries) cb(16); + }); + }, + restore() { + win.requestAnimationFrame = originalRequest; + win.cancelAnimationFrame = originalCancel; + }, + }; +} + +async function setupHarness(t: test.TestContext, initialWidth = 208) { + const env = installDomEnvironment(); + const raf = installManualRaf(env.window); + const renderer = await createDomRenderer(env.document); + + const widthUpdates: number[] = []; + const commits: number[] = []; + const resizingStates: boolean[] = []; + + function Harness({ width }: { width: number }) { + const onPointerDown = useVaultSidebarResize({ + effectiveSidebarWidth: width, + minWidth: 56, + maxWidth: 320, + setSidebarWidth: (next) => { + widthUpdates.push(next); + }, + handleSidebarWidthCommit: (next) => { + commits.push(next); + }, + setIsSidebarResizing: (resizing) => { + resizingStates.push(resizing); + }, + }); + return
; + } + + await renderer.render(); + const separator = env.document.querySelector('[role="separator"]')!; + const pointer = (target: EventTarget, type: string, x: number) => + dispatchDomEvent( + target, + new env.window.MouseEvent(type, { bubbles: true, clientX: x }), + ); + + t.after(async () => { + await renderer.unmount(); + raf.restore(); + env.cleanup(); + }); + + return { + env, + raf, + widthUpdates, + commits, + resizingStates, + unmount: () => renderer.unmount(), + start: (x = 200) => pointer(separator, "pointerdown", x), + move: (x: number) => pointer(env.window, "pointermove", x), + up: (x: number) => pointer(env.window, "pointerup", x), + cancel: (x: number) => pointer(env.window, "pointercancel", x), + }; +} + +test("VaultViewLayout sidebar resize coalesces pointermove events into a single rAF with the latest width", async (t) => { + const h = await setupHarness(t, 208); + + await h.start(200); + await h.move(210); // 218 + await h.move(225); // 233 + await h.move(240); // 248 + await h.move(360); // clamped to 320 + + assert.equal(h.raf.requestCount, 1); + assert.equal(h.raf.pendingCount, 1); + assert.deepEqual( + h.widthUpdates, + [], + "setSidebarWidth must not fire synchronously on pointermove before rAF", + ); + + await h.raf.flushNext(); + assert.deepEqual(h.widthUpdates, [320]); + assert.equal(h.raf.pendingCount, 0); + + await h.up(250); // 258 + assert.deepEqual(h.widthUpdates, [320, 258]); + assert.deepEqual(h.commits, [258]); + assert.equal(h.env.document.body.style.cursor, ""); + assert.equal(h.env.document.body.style.userSelect, ""); +}); + +test("VaultViewLayout sidebar pointerup cancels pending rAF before committing final width", async (t) => { + const h = await setupHarness(t, 208); + + await h.start(200); + await h.move(280); // schedules rAF for 288 + assert.equal(h.raf.pendingCount, 1); + + await h.up(220); // commits 228 before queued rAF runs + assert.equal(h.raf.cancelledIds.length, 1); + assert.equal(h.raf.pendingCount, 0); + assert.deepEqual(h.widthUpdates, [228]); + assert.deepEqual(h.commits, [228]); +}); + +test("VaultViewLayout sidebar resize cleans up window listeners, cursor, and pending rAF on unmount", async (t) => { + const h = await setupHarness(t, 208); + + await h.start(200); + await h.move(260); + assert.equal(h.raf.pendingCount, 1); + assert.equal(h.env.document.body.style.cursor, "col-resize"); + assert.equal(h.env.document.body.style.userSelect, "none"); + + await h.unmount(); + assert.equal(h.raf.cancelledIds.length, 1, "unmount must cancel pending rAF"); + assert.equal(h.raf.pendingCount, 0); + assert.equal(h.env.document.body.style.cursor, ""); + assert.equal(h.env.document.body.style.userSelect, ""); + + // Post-unmount window pointer events must not trigger leaked listeners. + await h.move(300); + await h.up(300); + assert.deepEqual(h.widthUpdates, []); + assert.deepEqual(h.commits, []); +}); + +test("VaultViewLayout inline panel surface clips enter-animation overflow", () => { + const source = readFileSync(new URL("./VaultViewLayout.tsx", import.meta.url), "utf8"); + const surfaceIndex = source.indexOf('data-section="vault-surface"'); + assert.ok(surfaceIndex > 0); + const surfaceOpening = source.slice(Math.max(0, surfaceIndex - 500), surfaceIndex); + assert.match(surfaceOpening, /overflow-hidden/); +}); diff --git a/components/vault/VaultViewLayout.tsx b/components/vault/VaultViewLayout.tsx index 9b8250667..025a1f014 100644 --- a/components/vault/VaultViewLayout.tsx +++ b/components/vault/VaultViewLayout.tsx @@ -130,6 +130,106 @@ function VaultConnectionLogsSection({ ); } +export function useVaultSidebarResize({ + effectiveSidebarWidth, + minWidth, + maxWidth, + setSidebarWidth, + handleSidebarWidthCommit, + setIsSidebarResizing, +}: { + effectiveSidebarWidth: number; + minWidth: number; + maxWidth: number; + setSidebarWidth: (width: number) => void; + handleSidebarWidthCommit: (width: number) => void; + setIsSidebarResizing: (resizing: boolean) => void; +}) { + const resizeCleanupRef = React.useRef<(() => void) | null>(null); + + React.useEffect(() => () => resizeCleanupRef.current?.(), []); + + return React.useCallback( + (event: React.PointerEvent) => { + event.preventDefault(); + event.stopPropagation(); + resizeCleanupRef.current?.(); + + const startX = event.clientX; + const startWidth = effectiveSidebarWidth; + const previousCursor = document.body.style.cursor; + const previousUserSelect = document.body.style.userSelect; + + setIsSidebarResizing(true); + document.body.style.cursor = "col-resize"; + document.body.style.userSelect = "none"; + + const clampWidth = (value: number) => + Math.max(minWidth, Math.min(maxWidth, value)); + + let rafId: number | null = null; + let pendingWidth: number | null = null; + + const cancelPendingFrame = () => { + if (rafId !== null) { + window.cancelAnimationFrame?.(rafId); + rafId = null; + } + pendingWidth = null; + }; + + const handlePointerMove = (moveEvent: PointerEvent) => { + const nextWidth = clampWidth(startWidth + moveEvent.clientX - startX); + if (typeof window.requestAnimationFrame !== "function") { + setSidebarWidth(nextWidth); + return; + } + pendingWidth = nextWidth; + if (rafId !== null) return; + rafId = window.requestAnimationFrame(() => { + rafId = null; + if (pendingWidth !== null) { + const committedWidth = pendingWidth; + pendingWidth = null; + setSidebarWidth(committedWidth); + } + }); + }; + + const cleanup = () => { + resizeCleanupRef.current = null; + cancelPendingFrame(); + setIsSidebarResizing(false); + document.body.style.cursor = previousCursor; + document.body.style.userSelect = previousUserSelect; + window.removeEventListener("pointermove", handlePointerMove); + window.removeEventListener("pointerup", handlePointerUp); + window.removeEventListener("pointercancel", handlePointerUp); + }; + + const handlePointerUp = (upEvent: PointerEvent) => { + cleanup(); + const nextWidth = clampWidth(startWidth + upEvent.clientX - startX); + setSidebarWidth(nextWidth); + handleSidebarWidthCommit(nextWidth); + }; + + resizeCleanupRef.current = cleanup; + window.addEventListener("pointermove", handlePointerMove); + window.addEventListener("pointerup", handlePointerUp); + window.addEventListener("pointercancel", handlePointerUp); + }, + [ + effectiveSidebarWidth, + handleSidebarWidthCommit, + maxWidth, + minWidth, + setIsSidebarResizing, + setSidebarWidth, + ], + ); +} + export function VaultViewLayout({ ctx }: { ctx: VaultViewLayoutContext }) { const { Activity, @@ -501,44 +601,14 @@ export function VaultViewLayout({ ctx }: { ctx: VaultViewLayoutContext }) { }, [onUpdateKeys], ); - const handleSidebarResizeStart = React.useCallback( - (event: React.PointerEvent) => { - event.preventDefault(); - event.stopPropagation(); - - const startX = event.clientX; - const startWidth = effectiveSidebarWidth; - const previousCursor = document.body.style.cursor; - const previousUserSelect = document.body.style.userSelect; - - setIsSidebarResizing(true); - document.body.style.cursor = "col-resize"; - document.body.style.userSelect = "none"; - - const clampWidth = (value: number) => - Math.max(sidebarMinWidth, Math.min(sidebarMaxWidth, value)); - - const handlePointerMove = (moveEvent: PointerEvent) => { - setSidebarWidth(clampWidth(startWidth + moveEvent.clientX - startX)); - }; - const handlePointerUp = (upEvent: PointerEvent) => { - const nextWidth = clampWidth(startWidth + upEvent.clientX - startX); - setSidebarWidth(nextWidth); - handleSidebarWidthCommit(nextWidth); - setIsSidebarResizing(false); - document.body.style.cursor = previousCursor; - document.body.style.userSelect = previousUserSelect; - window.removeEventListener("pointermove", handlePointerMove); - window.removeEventListener("pointerup", handlePointerUp); - window.removeEventListener("pointercancel", handlePointerUp); - }; - - window.addEventListener("pointermove", handlePointerMove); - window.addEventListener("pointerup", handlePointerUp); - window.addEventListener("pointercancel", handlePointerUp); - }, - [effectiveSidebarWidth, handleSidebarWidthCommit, setSidebarWidth], - ); + const handleSidebarResizeStart = useVaultSidebarResize({ + effectiveSidebarWidth, + minWidth: sidebarMinWidth, + maxWidth: sidebarMaxWidth, + setSidebarWidth, + handleSidebarWidthCommit, + setIsSidebarResizing, + }); React.useEffect(() => { if (!isHostAsideOpen) return; diff --git a/index.css b/index.css index 144e3a0f4..b15b300ce 100644 --- a/index.css +++ b/index.css @@ -401,23 +401,21 @@ html.platform-win32 { @keyframes split-panel-enter { 0% { - width: 0; - min-width: 0; + transform: translateX(100%); opacity: 0; } 55% { opacity: 0.88; } 100% { - width: var(--aside-inline-width); - min-width: var(--aside-inline-width); + transform: translateX(0); opacity: 1; } } .split-panel-enter { - animation: split-panel-enter 220ms cubic-bezier(0.24, 0.84, 0.32, 1) both; - will-change: width, opacity; + animation: split-panel-enter 220ms cubic-bezier(0.24, 0.84, 0.32, 1) backwards; + will-change: transform, opacity; } /* Security-key prompts use the same tokens as the shared dialog. Motion stays