Skip to content
Open
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
31 changes: 29 additions & 2 deletions src/features/messages/scroll-correction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { correctScrollTop } from "./scroll-correction";
const stubNavigator = (platform: string, vendor: string, maxTouchPoints = 0) =>
vi.stubGlobal("navigator", { platform, vendor, maxTouchPoints });

function scroller() {
function scroller(writes?: number[], end = Number.POSITIVE_INFINITY) {
const element = document.createElement("section");
let top = 100;
Object.defineProperty(element, "scrollTop", {
get: () => top,
set: (value: number) => {
top = value;
// With `writes`, keep whole offsets inside the range, as WebKit does.
writes?.push(value);
top = writes ? Math.min(end, Math.trunc(value)) : value;
},
});
return element;
Expand Down Expand Up @@ -89,3 +91,28 @@ it("ignores a zero correction", () => {
expect(element.scrollTop).toBe(100);
expect(element.getAttribute("style")).toBeNull();
});

it("does not write again the fraction a whole-pixel scroller left", () => {
const writes: number[] = [];
const element = scroller(writes);

correctScrollTop(element, 40.75);
// The reader scrolls on, and the next measurement asks for the same fraction.
element.scrollTop = 120;
writes.length = 0;
correctScrollTop(element, 0.75);

expect(writes).toEqual([]);
correctScrollTop(element, 12.75);
expect(element.scrollTop).toBe(132);
});

it("asks again for a correction the end of the scroll range refused", () => {
const writes: number[] = [];
const element = scroller(writes, 100);

correctScrollTop(element, 40);
correctScrollTop(element, 40);

expect(writes).toEqual([140, 140]);
});
17 changes: 16 additions & 1 deletion src/features/messages/scroll-correction.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { macWebKit } from "./mac-webkit";

const restores = new WeakMap<HTMLElement, () => void>();
// The part of each scroller's last correction that its offset did not take.
const remainders = new WeakMap<HTMLElement, number>();

/**
* Applies an automatic scroll correction. Mac WebKit can leave the corrected
* viewport unpainted while native momentum continues, so stop the momentum the
* same way the Virtua patch does (patches/virtua@0.54.2.patch): hide vertical overflow for
* one task. The scroller must keep its inline size (`scrollbar-gutter: stable`).
*
* A scroller keeps whole offsets (WebKit truncates scrollTop), so a caller
* holding an exact position is left a fraction of a pixel short and asks for
* that fraction again after every measurement. Writing it again moves nothing,
* but any scroll write ends a wheel that Linux WebKit is still animating, and
* the reader loses the rest of it. Skip the fraction the last correction left.
*/
export function correctScrollTop(element: HTMLElement, delta: number) {
if (!delta) return;
const left = remainders.get(element);
if (left !== undefined && Math.abs(delta - left) < 0.001) return;
if (macWebKit()) {
// Corrections in one task share the original declaration.
restores.get(element)?.();
Expand All @@ -31,5 +41,10 @@ export function correctScrollTop(element: HTMLElement, delta: number) {
const timer = setTimeout(restore);
restores.set(element, restore);
}
element.scrollTop += delta;
const target = element.scrollTop + delta;
element.scrollTop = target;
const remainder = target - element.scrollTop;
// A larger shortfall is the end of the scroll range, which content can move.
if (remainder && Math.abs(remainder) < 1) remainders.set(element, remainder);
else remainders.delete(element);
}
16 changes: 12 additions & 4 deletions tests/browser/history-loading.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,20 @@ test.describe("pages inside one second", () => {
.poll(() => history(page).evaluate((e) => e.scrollHeight))
.toBeGreaterThan(height);
await expectAnchor(page, before);
// Linux WebKit animates a wheel over several frames, and any scroll
// write ends it there. A row measuring meanwhile moves nothing above
// the reader, so it must not cost them the rest of the wheel.
const wheeled = await history(page).evaluateHandle((e) => {
const state = { wheel: false };
e.addEventListener("wheel", () => (state.wheel = true), {
once: true,
passive: true,
});
e.addEventListener(
"wheel",
() => {
state.wheel = true;
const rows = e.querySelectorAll("[data-message-id]");
rows[rows.length - 1].style.paddingBottom = "10px";
},
{ once: true, passive: true },
);
return state;
});
await page.mouse.wheel(0, -40);
Expand Down
Loading