Skip to content

Commit 8a98601

Browse files
PR Batch Testerclaude
andcommitted
fix(desktop): a slow scroll-end IPC no longer drops the next gesture's first wheel
The renderer converted main's scroll-end stamp using the smallest IPC lag it had seen. That put the cutoff at main's end plus that lag, so whenever every IPC was slow, the next gesture's first wheel looked stale and was dropped. A scroller removed before the late scroll-begin then let the swipe navigate. Main now stamps the end with its wall clock, the one clock both processes share. The renderer subtracts its own wall-minus-monotonic offset when the IPC arrives, so the cutoff is main's end within 1ms whatever the IPC lag. Sleep and clock adjustments only matter if they land while the IPC is in flight. The begin IPC no longer carries a stamp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3f84177 commit 8a98601

5 files changed

Lines changed: 32 additions & 34 deletions

File tree

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
9292
return () => ipcRenderer.removeListener(IpcChannels.SET_NOTIFICATION_BADGE_CHANNEL, handler);
9393
},
9494
onTrackpadScrollBegin: (listener) => {
95-
const handler = (_event: Electron.IpcRendererEvent, at: number) => listener(at);
95+
const handler = () => listener();
9696
ipcRenderer.on(IpcChannels.TRACKPAD_SCROLL_BEGIN_CHANNEL, handler);
9797
return () => ipcRenderer.removeListener(IpcChannels.TRACKPAD_SCROLL_BEGIN_CHANNEL, handler);
9898
},

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ export const make = Effect.gen(function* () {
305305
const clientSettings = yield* DesktopClientSettings.DesktopClientSettings;
306306
const electronApp = yield* ElectronApp.ElectronApp;
307307
const rendererHistory = yield* DesktopRendererHistory.DesktopRendererHistory;
308+
const clock = yield* Clock.Clock;
308309
const backendReadyRef = yield* Ref.make(false);
309310
const splashWindowRef = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
310311
const context = yield* Effect.context<DesktopWindowRuntimeServices>();
@@ -661,9 +662,9 @@ export const make = Effect.gen(function* () {
661662
});
662663
window.webContents.on("input-event", (_event, input) => {
663664
if (input.type === "gestureScrollBegin") {
664-
window.webContents.send(TRACKPAD_SCROLL_BEGIN_CHANNEL, performance.now());
665+
window.webContents.send(TRACKPAD_SCROLL_BEGIN_CHANNEL);
665666
} else if (input.type === "gestureScrollEnd") {
666-
window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL, performance.now());
667+
window.webContents.send(TRACKPAD_SCROLL_END_CHANNEL, clock.currentTimeMillisUnsafe());
667668
}
668669
});
669670
// Ctrl+wheel bypasses menus; guest contents emit their own zoom-changed event.

‎apps/web/src/components/HistorySwipeNavigation.browser.test.tsx‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { render, stubGlobal } from "../../test/render";
44
import { HistorySwipeNavigation } from "./HistorySwipeNavigation";
55

66
type Scroller = "shadow" | "light" | "none" | "shadow-dialog";
7-
// Main stamps gesture IPCs with its own performance.now(), whose origin differs from the page's.
8-
const MAIN_CLOCK_AHEAD = 1_000_000;
9-
// "late-end": main saw the end this long before its IPC reached the page.
10-
const LATE_END_DELAY = 50;
7+
// The page's Date.now() minus its performance.now(); main stamps the end with Date.now().
8+
const WALL_CLOCK_AHEAD = 1_700_000_000_000;
9+
// Every gesture IPC reaches the page this long after main sees the native event.
10+
const IPC_LAG = 40;
1111
type Step = readonly [
12-
// "stale": a 1px wheel created before the last end, delivered after its IPC.
13-
// "fresh": a 1px wheel created after main saw a late end, before its IPC arrived.
14-
event: number | "begin" | "end" | "late-end" | "zoom" | "stale" | "fresh",
12+
// "stale": a 1px wheel created before main saw the last end, delivered after its IPC.
13+
// "fresh": a 1px wheel created after main saw the last end, before its IPC arrived.
14+
event: number | "begin" | "end" | "zoom" | "stale" | "fresh",
1515
after?: number | "remount" | "remove" | "close-dialog",
1616
];
1717

@@ -83,7 +83,7 @@ describe("HistorySwipeNavigation", () => {
8383
"a next-gesture wheel created while the previous end IPC was in flight",
8484
"shadow",
8585
400,
86-
[["begin"], [130], ["late-end"], ["fresh", "remove"], ["begin"], [130]],
86+
[["begin"], [130], ["end"], ["fresh", "remove"], ["begin"], [130]],
8787
null,
8888
],
8989
[
@@ -112,9 +112,9 @@ describe("HistorySwipeNavigation", () => {
112112
["swiping right over content without overflow", "none", 0, [[130]], "forward"],
113113
])("%s", async (_name, scroller, scrollLeft, steps, navigates) => {
114114
type Listener = (at: number) => void;
115-
const listeners = { begin: new Set<Listener>(), end: new Set<Listener>() };
115+
const listeners = { begin: new Set<() => void>(), end: new Set<Listener>() };
116116
using _bridge = stubGlobal("desktopBridge", {
117-
onTrackpadScrollBegin: (listener: Listener) => {
117+
onTrackpadScrollBegin: (listener: () => void) => {
118118
listeners.begin.add(listener);
119119
return () => listeners.begin.delete(listener);
120120
},
@@ -129,17 +129,20 @@ describe("HistorySwipeNavigation", () => {
129129
using _canGoBack = vi.spyOn(window.navigation, "canGoBack", "get").mockReturnValue(true);
130130
using _canGoForward = vi.spyOn(window.navigation, "canGoForward", "get").mockReturnValue(true);
131131
let now = 1000;
132-
let lateEndAt = 0;
132+
let endSeenAt = 0;
133133
using _now = vi.spyOn(performance, "now").mockImplementation(() => now);
134+
using _wallNow = vi.spyOn(Date, "now").mockImplementation(() => now + WALL_CLOCK_AHEAD);
134135
let { viewport, content } = mountPanel(screen.container, scroller, scrollLeft);
135136

136137
for (const [event, after] of [...steps, [0] as const]) {
137138
now += 10;
138-
if (event === "begin" || event === "end") {
139-
for (const listener of listeners[event]) listener(now + MAIN_CLOCK_AHEAD);
140-
} else if (event === "late-end") {
141-
lateEndAt = now - LATE_END_DELAY;
142-
for (const listener of listeners.end) listener(lateEndAt + MAIN_CLOCK_AHEAD);
139+
if (event === "begin") {
140+
now += IPC_LAG;
141+
for (const listener of listeners.begin) listener();
142+
} else if (event === "end") {
143+
endSeenAt = now;
144+
now += IPC_LAG;
145+
for (const listener of listeners.end) listener(endSeenAt + WALL_CLOCK_AHEAD);
143146
} else {
144147
const wheel = new WheelEvent("wheel", {
145148
deltaX: typeof event === "number" ? event : event === "zoom" ? 0 : 1,
@@ -148,7 +151,7 @@ describe("HistorySwipeNavigation", () => {
148151
composed: true,
149152
});
150153
const timeStamp =
151-
event === "stale" ? 0 : event === "fresh" ? lateEndAt + LATE_END_DELAY / 2 : now;
154+
event === "stale" ? endSeenAt - 5 : event === "fresh" ? endSeenAt + IPC_LAG / 2 : now;
152155
Object.defineProperty(wheel, "timeStamp", { value: timeStamp });
153156
content.dispatchEvent(wheel);
154157
}

‎apps/web/src/components/HistorySwipeNavigation.tsx‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ export function HistorySwipeNavigation() {
9292
useEffect(() => {
9393
let gesture: SwipeGesture | null = null;
9494
let endedAt = Number.NEGATIVE_INFINITY;
95-
// Main's clock shares the page's monotonic rate but not its origin. The smallest receipt lag
96-
// seen bounds the offset, so a converted stamp lands no later than its own IPC's arrival.
97-
let mainClockOffset = Number.POSITIVE_INFINITY;
98-
const fromMainClock = (at: number) => {
99-
mainClockOffset = Math.min(mainClockOffset, performance.now() - at);
100-
return at + mainClockOffset;
101-
};
10295
const hideIndicator = () =>
10396
setSwipe((current) => (current === null ? null : { ...current, tracking: false }));
10497
// The fingers lifted: navigate if the swipe went far enough, then ignore its momentum.
@@ -110,18 +103,19 @@ export function HistorySwipeNavigation() {
110103
hideIndicator();
111104
};
112105
// Scroll-begin IPC can arrive after the first wheel already gave ownership to content.
113-
const unsubscribeScrollBegin = window.desktopBridge?.onTrackpadScrollBegin((at) => {
114-
fromMainClock(at);
106+
const unsubscribeScrollBegin = window.desktopBridge?.onTrackpadScrollBegin(() => {
115107
if (gesture?.kind === "ignored" && !gesture.scrollerOwned) gesture = null;
116108
});
117109
const unsubscribeScrollEnd = window.desktopBridge?.onTrackpadScrollEnd((at) => {
118110
if (gesture?.kind === "tracking") release();
119111
gesture = null;
120-
endedAt = fromMainClock(at);
112+
// Wall time is the only clock main shares with the page. Converting on receipt keeps
113+
// sleep and clock changes out of the offset, leaving Date.now()'s 1ms resolution.
114+
endedAt = at - (Date.now() - performance.now());
121115
});
122116
const onWheel = (event: WheelEvent) => {
123-
// End IPC can overtake its gesture's last wheels, and the next gesture's first wheel can
124-
// be created before that IPC arrives. Their OS timestamps fall on either side of main's.
117+
// End IPC can overtake its gesture's last wheels. Those were created before main saw the
118+
// end; the next gesture's wheels usually come after, even when the IPC is slow.
125119
if (event.timeStamp < endedAt) return;
126120
if (gesture === null) {
127121
gesture =

‎packages/contracts/src/ipc.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,8 @@ export interface DesktopBridge {
10181018
getClientPlatform: () => string;
10191019
setNotificationBadge: (badge: { count: number; image: string | null }) => Promise<void>;
10201020
onNotificationBadgeClear: (listener: () => void) => () => void;
1021-
/** `at` is the main process's `performance.now()`, whose origin differs from the page's. */
1022-
onTrackpadScrollBegin: (listener: (at: number) => void) => () => void;
1021+
onTrackpadScrollBegin: (listener: () => void) => () => void;
1022+
/** `at` is the main process's wall clock (`Date.now()`) when it saw the end. */
10231023
onTrackpadScrollEnd: (listener: (at: number) => void) => () => void;
10241024
/**
10251025
* Renderer `window.focus()` cannot raise an Electron window, and on Wayland

0 commit comments

Comments
 (0)