Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/app/src/components/layout/WindowFindHost.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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 => {
const desktop = getBbDesktopInfo();
if (desktop?.openWindowFind === undefined) {
return false;
}
window.dispatchEvent(new Event(WINDOW_FIND_OPEN_EVENT));
desktop.openWindowFind({ topOffset: readWindowFindTopOffset() });
return true;
}, []);
Expand Down
11 changes: 11 additions & 0 deletions apps/app/src/components/ui/scroll-position-keeper.model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand Down
53 changes: 53 additions & 0 deletions apps/app/src/components/ui/scroll-position-keeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"],
Expand Down
31 changes: 30 additions & 1 deletion apps/app/src/components/ui/scroll-position-keeper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WINDOW_FIND_OPEN_EVENT } from "@/lib/window-find";

export type ScrollHoldEdge = "top" | "bottom";

export type ReadingPosition =
Expand Down Expand Up @@ -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<ScrollWindowList>();
private readonly listeners = new Set<() => void>();
private readonly positionListeners = new Set<() => void>();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) <=
Expand Down
1 change: 1 addition & 0 deletions apps/app/src/lib/window-find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const WINDOW_FIND_OPEN_EVENT = "bb:window-find-open";
22 changes: 15 additions & 7 deletions docs/timeline-pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
Loading