diff --git a/webui/src/components/thread/thread-camera.ts b/webui/src/components/thread/thread-camera.ts index 29828fdb6..2a4526c8e 100644 --- a/webui/src/components/thread/thread-camera.ts +++ b/webui/src/components/thread/thread-camera.ts @@ -1,30 +1,6 @@ -interface ThreadCameraMotionProfile { - /** - * Time constant for the ease-out chase. Smaller values react faster; the - * camera closes roughly 95% of an uncapped distance in three time constants. - */ - responseTimeMs: number; - /** Prevents a long explicit navigation from turning into a one-frame jump. */ - maxSpeedPxPerSecond: number; - /** Avoids spending frames chasing sub-pixel layout noise. */ - settleDistancePx: number; - /** Limits catch-up after a throttled or backgrounded animation frame. */ - maxFrameDeltaMs: number; -} - -const THREAD_CAMERA_NAVIGATION_MOTION: Readonly = { - responseTimeMs: 110, - maxSpeedPxPerSecond: 12_000, - settleDistancePx: 0.5, - maxFrameDeltaMs: 50, -}; - -const THREAD_CAMERA_REDUCED_NAVIGATION_MOTION: Readonly = { - responseTimeMs: 45, - maxSpeedPxPerSecond: 24_000, - settleDistancePx: 0.5, - maxFrameDeltaMs: 50, -}; +const NAVIGATION_DURATION_MS = 220; +const REDUCED_NAVIGATION_DURATION_MS = 80; +const SETTLE_DISTANCE_PX = 0.5; export interface ThreadCameraViewport { scrollTop: number; @@ -44,27 +20,6 @@ interface ThreadCameraOptions { prefersReducedMotion?: () => boolean; } -/** - * A time-based ease-out chase rather than a start/end tween. The target can - * move during explicit history navigation without restarting a duration or - * adding another frame loop. - */ -function easeOutChase( - current: number, - target: number, - deltaSeconds: number, - profile: Pick, -): number { - const distance = target - current; - const responseSeconds = Math.max(0.001, profile.responseTimeMs / 1000); - const timeStep = Math.max(0.001, deltaSeconds); - const easeOutFraction = 1 - Math.exp(-timeStep / responseSeconds); - const uncappedStep = distance * easeOutFraction; - const maxStep = Math.max(0, profile.maxSpeedPxPerSecond) * timeStep; - const step = Math.max(-maxStep, Math.min(maxStep, uncappedStep)); - return current + step; -} - function defaultScheduler(): ThreadCameraScheduler { return { request: (callback) => window.requestAnimationFrame(callback), @@ -87,6 +42,7 @@ export class ThreadCameraController { private phase: "idle" | "following" = "idle"; private target = 0; private lastTimestamp: number | null = null; + private deadline = 0; constructor( getViewport: () => ThreadCameraViewport | null, @@ -133,17 +89,19 @@ export class ThreadCameraController { const current = viewport.scrollTop; this.target = Math.max(0, top); - const motion = this.currentMotion(); if (this.phase === "following") { return "retargeted"; } - if (Math.abs(this.target - current) <= motion.settleDistancePx) { + if (Math.abs(this.target - current) <= SETTLE_DISTANCE_PX) { this.write(viewport, this.target); return "settled"; } this.phase = "following"; this.lastTimestamp = this.scheduler.now(); + this.deadline = this.lastTimestamp + (this.prefersReducedMotion() + ? REDUCED_NAVIGATION_DURATION_MS + : NAVIGATION_DURATION_MS); this.frameId = this.scheduler.request(this.advance); return "started"; } @@ -169,54 +127,22 @@ export class ThreadCameraController { return; } - const motion = this.currentMotion(); - const previousTimestamp = this.lastTimestamp ?? timestamp - (1000 / 60); - const deltaMs = Math.min( - motion.maxFrameDeltaMs, - Math.max(1, timestamp - previousTimestamp), - ); - this.lastTimestamp = timestamp; - const current = viewport.scrollTop; - const remainingDistance = this.target - current; - if (Math.abs(remainingDistance) <= motion.settleDistancePx) { + const previousTimestamp = this.lastTimestamp ?? timestamp; + const remainingMs = Math.max(0, this.deadline - timestamp); + if (remainingMs === 0) { this.write(viewport, this.target); - this.phase = "idle"; - this.lastTimestamp = null; + this.cancel(); return; } - const deltaSeconds = deltaMs / 1000; - const easedTop = easeOutChase( - current, - this.target, - deltaSeconds, - motion, - ); - // Some browsers quantize scrollTop writes to whole pixels. Keep the - // ease-out curve, but never let its subpixel tail round back to the same - // position forever. - const minimumStep = Math.min(1, Math.abs(remainingDistance)); - const nextTop = - Math.abs(easedTop - current) < minimumStep - ? current + Math.sign(remainingDistance) * minimumStep - : easedTop; - const settled = Math.abs(this.target - nextTop) <= motion.settleDistancePx; - this.write(viewport, settled ? this.target : nextTop); - - if (settled) { - this.phase = "idle"; - this.lastTimestamp = null; - return; - } + const previousRemainingMs = Math.max(remainingMs, this.deadline - previousTimestamp); + const remainingFraction = (remainingMs / previousRemainingMs) ** 3; + const current = viewport.scrollTop; + this.write(viewport, this.target + (current - this.target) * remainingFraction); + this.lastTimestamp = timestamp; this.frameId = this.scheduler.request(this.advance); }; - private currentMotion(): ThreadCameraMotionProfile { - return this.prefersReducedMotion() - ? THREAD_CAMERA_REDUCED_NAVIGATION_MOTION - : THREAD_CAMERA_NAVIGATION_MOTION; - } - private write(viewport: ThreadCameraViewport, top: number): void { try { viewport.scrollTop = top; diff --git a/webui/src/tests/thread-camera.test.ts b/webui/src/tests/thread-camera.test.ts index e5a606894..93b8d8849 100644 --- a/webui/src/tests/thread-camera.test.ts +++ b/webui/src/tests/thread-camera.test.ts @@ -39,6 +39,45 @@ function cameraHarness(prefersReducedMotion = false) { } describe("ThreadCameraController", () => { + it("uses the same duration for nearby and distant targets", () => { + const durations = [100, 100_000].map((target) => { + const { camera, viewport, advance } = cameraHarness(); + camera.navigateTo(target); + let elapsed = 0; + while (camera.isFollowing() && elapsed < 1_000) { + advance(16); + elapsed += 16; + } + expect(viewport.scrollTop).toBe(target); + expect(elapsed).toBeLessThanOrEqual(250); + return elapsed; + }); + expect(durations[0]).toBe(durations[1]); + }); + + it.each([1_000, 100_000, -100_000])("finishes a %i px navigation within 250ms", (distance) => { + const { camera, viewport, frames, advance } = cameraHarness(); + viewport.scrollTop = distance < 0 ? -distance : 0; + const target = viewport.scrollTop + distance; + camera.navigateTo(target); + advance(16); + expect(viewport.scrollTop).not.toBe(target); + advance(234); + expect(viewport.scrollTop).toBe(target); + expect(camera.isFollowing()).toBe(false); + expect(frames).toHaveLength(0); + }); + + it("keeps the original deadline when layout retargets navigation", () => { + const { camera, viewport, advance } = cameraHarness(); + camera.navigateTo(100_000); + advance(100); + expect(camera.navigateTo(120_000)).toBe("retargeted"); + advance(150); + expect(viewport.scrollTop).toBe(120_000); + expect(camera.isFollowing()).toBe(false); + }); + it("pins automatic follow in the geometry frame without camera debt", () => { const { camera, viewport, frames } = cameraHarness();