diff --git a/src/features/messages/scroll-correction.test.ts b/src/features/messages/scroll-correction.test.ts index 7011118582..cabba95205 100644 --- a/src/features/messages/scroll-correction.test.ts +++ b/src/features/messages/scroll-correction.test.ts @@ -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; @@ -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]); +}); diff --git a/src/features/messages/scroll-correction.ts b/src/features/messages/scroll-correction.ts index cb3190cba7..2db859f12e 100644 --- a/src/features/messages/scroll-correction.ts +++ b/src/features/messages/scroll-correction.ts @@ -1,15 +1,25 @@ import { macWebKit } from "./mac-webkit"; const restores = new WeakMap void>(); +// The part of each scroller's last correction that its offset did not take. +const remainders = new WeakMap(); /** * 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)?.(); @@ -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); } diff --git a/tests/browser/history-loading.spec.mjs b/tests/browser/history-loading.spec.mjs index 4cb6dd77da..b0580b82ef 100644 --- a/tests/browser/history-loading.spec.mjs +++ b/tests/browser/history-loading.spec.mjs @@ -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);