diff --git a/apps/app/src/components/layout/WindowFindHost.tsx b/apps/app/src/components/layout/WindowFindHost.tsx index 7d845bc1278..c92bd9fe391 100644 --- a/apps/app/src/components/layout/WindowFindHost.tsx +++ b/apps/app/src/components/layout/WindowFindHost.tsx @@ -1,6 +1,7 @@ import { useCallback } from "react"; import { useAppCommandHandler } from "@/components/commands/AppCommandProvider"; import { getBbDesktopInfo, readWindowFindTopOffset } from "@/lib/bb-desktop"; +import { WINDOW_FIND_OPEN_EVENT } from "@/lib/window-find"; export function WindowFindHost() { const openWindowFind = useCallback((): boolean => { @@ -8,6 +9,7 @@ export function WindowFindHost() { if (desktop?.openWindowFind === undefined) { return false; } + window.dispatchEvent(new Event(WINDOW_FIND_OPEN_EVENT)); desktop.openWindowFind({ topOffset: readWindowFindTopOffset() }); return true; }, []); diff --git a/apps/app/src/components/ui/scroll-position-keeper.model.test.ts b/apps/app/src/components/ui/scroll-position-keeper.model.test.ts index 2f9b75952cd..b5ed208e9af 100644 --- a/apps/app/src/components/ui/scroll-position-keeper.model.test.ts +++ b/apps/app/src/components/ui/scroll-position-keeper.model.test.ts @@ -355,6 +355,17 @@ function runSeed(seed: number): string[] { }; }, }, + { + weight: 2, + make: () => { + const delta = random.int(-600, 600); + return { + kind: keeper.isAttached() ? "layout" : "navigation", + label: `scroll ${delta} without input`, + run: () => layout.layoutScrollTo(layout.scrollTop + delta), + }; + }, + }, { weight: 2, make: () => { diff --git a/apps/app/src/components/ui/scroll-position-keeper.test.ts b/apps/app/src/components/ui/scroll-position-keeper.test.ts index 3a8d541cd45..f327aaa3cf7 100644 --- a/apps/app/src/components/ui/scroll-position-keeper.test.ts +++ b/apps/app/src/components/ui/scroll-position-keeper.test.ts @@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { installFakeFlowLayout, setFakeHeight } from "@/test/fake-flow-layout"; +import { WINDOW_FIND_OPEN_EVENT } from "@/lib/window-find"; import { ScrollPositionKeeper } from "./scroll-position-keeper"; const VIEWPORT_HEIGHT = 500; @@ -246,6 +247,58 @@ describe("ScrollPositionKeeper", () => { }, ); + it("keeps an attached view at the end when it scrolls without input or a size change", () => { + const timeline = createTimeline(rowsOf(10), { attached: true }); + + timeline.layout.layoutScrollTo(timeline.layout.scrollTop - 300); + + expect(timeline.keeper.isAttached()).toBe(true); + expect(timeline.layout.scrollTop).toBe(timeline.layout.maxScrollTop()); + expect(timeline.keeper.hasUserScrolled()).toBe(false); + }); + + it("keeps a pending restore when the view scrolls without input before the row mounts", () => { + const rows = rowsOf(10); + const timeline = createTimeline(rows.slice(5), { attached: true }); + + timeline.keeper.restore({ + rowId: "row-2", + offsetWithinRow: 0, + atBottom: false, + }); + timeline.layout.layoutScrollTo(timeline.layout.scrollTop - 200); + timeline.setRows(rows); + timeline.layout.flushResizeObservers(); + + expect(timeline.topOf("row-2")).toBe(0); + expect(timeline.keeper.hasUserScrolled()).toBe(false); + }); + + it("follows a scroll without input while the window find bar is open, and keeps the end again once the window has focus", () => { + const timeline = createTimeline(rowsOf(10), { attached: true }); + + window.dispatchEvent(new Event(WINDOW_FIND_OPEN_EVENT)); + timeline.layout.layoutScrollTo(400); + expect(timeline.keeper.isAttached()).toBe(false); + expect(timeline.layout.scrollTop).toBe(400); + + timeline.keeper.scrollToBottom(); + window.dispatchEvent(new Event("focus")); + timeline.layout.layoutScrollTo(400); + expect(timeline.keeper.isAttached()).toBe(true); + expect(timeline.layout.scrollTop).toBe(timeline.layout.maxScrollTop()); + }); + + it("follows a focus move after Tab as the reader's scroll", () => { + const timeline = createTimeline(rowsOf(10), { attached: true }); + + window.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab" })); + timeline.layout.layoutScrollTo(400); + + expect(timeline.keeper.isAttached()).toBe(false); + expect(timeline.layout.scrollTop).toBe(400); + }); + it("follows content that stays shrunk to the new end", () => { vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame", "performance"], diff --git a/apps/app/src/components/ui/scroll-position-keeper.ts b/apps/app/src/components/ui/scroll-position-keeper.ts index 3285236d4df..83934233192 100644 --- a/apps/app/src/components/ui/scroll-position-keeper.ts +++ b/apps/app/src/components/ui/scroll-position-keeper.ts @@ -1,3 +1,5 @@ +import { WINDOW_FIND_OPEN_EVENT } from "@/lib/window-find"; + export type ScrollHoldEdge = "top" | "bottom"; export type ReadingPosition = @@ -149,6 +151,7 @@ export class ScrollPositionKeeper { private clampFrame: number | null = null; private attachFrame: number | null = null; private floorHeight = 0; + private windowFindOpen = false; private readonly lists = new Set(); private readonly listeners = new Set<() => void>(); private readonly positionListeners = new Set<() => void>(); @@ -252,6 +255,10 @@ export class ScrollPositionKeeper { this.extendInput(WHEEL_SESSION_MS); }; const onKeyDown = (event: KeyboardEvent) => { + if (event.key === "Tab") { + this.extendInput(KEY_SESSION_MS); + return; + } if (!SCROLL_KEYS.has(event.key) || isEditableTarget(event.target)) { return; } @@ -265,6 +272,12 @@ export class ScrollPositionKeeper { } this.extendInput(KEY_SESSION_MS); }; + const onWindowFindOpen = () => { + this.windowFindOpen = true; + }; + const onWindowFocus = () => { + this.windowFindOpen = false; + }; scrollElement.addEventListener("scroll", onScroll, { passive: true }); scrollElement.addEventListener("wheel", onWheel, { passive: true }); scrollElement.addEventListener("touchstart", onTouchStart, PASSIVE_CAPTURE); @@ -276,6 +289,8 @@ export class ScrollPositionKeeper { window.addEventListener("pointerup", onPointerUp, true); window.addEventListener("pointercancel", onPointerUp, true); window.addEventListener("keydown", onKeyDown); + window.addEventListener(WINDOW_FIND_OPEN_EVENT, onWindowFindOpen); + window.addEventListener("focus", onWindowFocus); let observer: ResizeObserver | undefined; if (typeof ResizeObserver !== "undefined") { observer = new ResizeObserver(() => { @@ -318,6 +333,9 @@ export class ScrollPositionKeeper { window.removeEventListener("pointerup", onPointerUp, true); window.removeEventListener("pointercancel", onPointerUp, true); window.removeEventListener("keydown", onKeyDown); + window.removeEventListener(WINDOW_FIND_OPEN_EVENT, onWindowFindOpen); + window.removeEventListener("focus", onWindowFocus); + this.windowFindOpen = false; this.touching = false; this.pointerDown = false; if (this.settleTimeout !== null) window.clearTimeout(this.settleTimeout); @@ -461,7 +479,11 @@ export class ScrollPositionKeeper { const cachedMax = this.maxScrollTop; const extentChanged = Math.abs(this.refreshExtent(scrollElement) - cachedMax) >= 1; - if (extentChanged || this.isFloorStale()) { + if ( + extentChanged || + this.isFloorStale() || + !this.followsUnexplainedScroll() + ) { this.syncFloor(); this.ownScrollTop = scrollTop; this.apply(scrollElement, true); @@ -795,6 +817,13 @@ export class ScrollPositionKeeper { this.attachIfAtEnd(scrollElement); } + private followsUnexplainedScroll(): boolean { + return ( + this.windowFindOpen || + (this.position.kind !== "bottom" && this.pendingRestore === null) + ); + } + private isAtEnd(scrollTop: number): boolean { return ( this.maxScrollTop - (scrollTop + this.deferred) <= diff --git a/apps/app/src/lib/window-find.ts b/apps/app/src/lib/window-find.ts new file mode 100644 index 00000000000..eafc0d2e6d1 --- /dev/null +++ b/apps/app/src/lib/window-find.ts @@ -0,0 +1 @@ +export const WINDOW_FIND_OPEN_EVENT = "bb:window-find-open"; diff --git a/docs/timeline-pagination.md b/docs/timeline-pagination.md index 09a29328ce7..604491b2910 100644 --- a/docs/timeline-pagination.md +++ b/docs/timeline-pagination.md @@ -303,8 +303,9 @@ anchor. The position changes only when: - the view sits at the end while detached (see below). A scroll counts as the reader's during an input session: a wheel, a scroll key, -a touch or pointer drag that scrolled, and the momentum that follows (up to -three seconds after the finger lifts, on every platform). A touch ends on the +Tab (focus moving to an offscreen control), a touch or pointer drag that +scrolled, and the momentum that follows (up to three seconds after the finger +lifts, on every platform). A touch ends on the last touch pointer's `pointerup`, caught on the window in the capture phase, or on `touchend`, whichever comes first. This means a tap on a control that removes itself, such as the send button, still ends the touch. A drag counts @@ -313,9 +314,16 @@ end, as a send does, the input session ends and the touch baseline is reset, so the app's own jump is never counted as the reader's gesture. Any other scroll that comes with a change in the scroll extent, or in the content's height since the last ResizeObserver delivery, is layout, and the owner -reconciles it back to the position. A scroll without input or size change, -such as find in page or focus moving to an offscreen control, is navigation, -and the position follows it. +reconciles it back to the position. A scroll without input or size change +never moves a view attached to the bottom, and never cancels a restore or +landing that is waiting for its row: the owner reconciles it back, the same as +layout. iOS WebKit moves the scroll position on its own, for example when +inline elements are re-inserted, and these scrolls cannot be told apart from +intended ones. A detached view follows such a scroll as navigation, such as +find in page. While the desktop window find bar is open, from the +`bb:window-find-open` event until the window regains focus, an attached view +follows it too, so find works from the bottom. Browser find on the web gets no +such signal, so from the bottom it is pulled back. The thread timeline holds a floor under its scroll extent. The first child of its scroll content is a spacer whose height equals the content's height at the @@ -325,8 +333,8 @@ Content that collapses and regrows within one task, such as the composer footer or a row remounting while something forces a layout, therefore never lowers the extent, and the browser has nothing to clamp. Without the floor that clamp arrives as a scroll with no input and no size change once the content has -regrown, which reads as navigation and detaches a view at the end. A shrink -that lasts reaches the ResizeObserver, which lowers the floor before +regrown, which a detached reader near the end would follow as navigation. A +shrink that lasts reaches the ResizeObserver, which lowers the floor before reconciling. Tool output boxes have no floor. Attachment follows one rule: a detached view within 4 px of the end attaches,