@devries-cameron20 / t3code Mirror · read-only / commits / 8171cd09d5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(desktop): mouse back and forward stay inside the browser tab (#33276)

Julius Marminge committed Nov 13, 2026 at 11:14 UTC 8171cd09d59ea0f49dd80c31260c8c9f9b8fc675
5 files changed +116 -44
apps/desktop/src/preview/GuestProtocol.ts
+1 -1
@@ -5,7 +5,6 @@ export const ANNOTATION_CAPTURED_CHANNEL = "preview:annotation-captured";
5 export const ANNOTATION_THEME_CHANNEL = "preview:annotation-theme";
6 export const ANNOTATION_SEND_ENABLED_CHANNEL = "preview:annotation-send-enabled";
7 export const HUMAN_INPUT_CHANNEL = "preview:human-input";
8 -export const MOUSE_NAVIGATE_CHANNEL = "preview:mouse-navigate";
8 // Sent to the hosting <webview> element (ipc-message). The web app mirrors this
9 // literal in HostedBrowserWebview because it cannot import from the desktop app.
10 export const POINTER_DOWN_HOST_CHANNEL = "preview:pointer-down";
@@ -15,6 +14,7 @@ export const RECORDING_POINTER_CHANNEL = "preview:recording-pointer";
14 export const RECORDING_KEY_CHANNEL = "preview:recording-key";
15 export const RECORDING_INPUT_CHANNEL = "preview:recording-input";
16 export const RECORDING_CONTROLLER_CHANNEL = "preview:recording-controller";
17 +export const THUMB_PRESS_CHANNEL = "preview:thumb-press";
18 export const PASSKEY_CREATE_CHANNEL = "preview:passkey-create";
19 export const PASSKEY_GET_CHANNEL = "preview:passkey-get";
20 export const PASSKEY_BRIDGE_ARGUMENT = "--t3code-preview-passkey-bridge";
apps/desktop/src/preview/Manager.test.ts
+63
@@ -745,6 +745,69 @@ describe("PreviewManager", () => {
745 ),
746 );
747
748 + effectIt.effect("keeps mouse back and forward buttons inside the preview tab", () =>
749 + withManager((manager) =>
750 + Effect.gen(function* () {
751 + const preview = makeFaviconWebContents();
752 + const goBack = vi.fn();
753 + const goForward = vi.fn();
754 + const send = vi.fn();
755 + let canGoBack = false;
756 + Object.assign(preview.webContents, {
757 + navigationHistory: {
758 + canGoBack: () => canGoBack,
759 + canGoForward: () => true,
760 + goBack,
761 + goForward,
762 + },
763 + getZoomFactor: () => 2,
764 + send,
765 + });
766 + fromId.mockReturnValue(preview.webContents);
767 + yield* manager.createTab("tab_mouse_navigation");
768 + yield* manager.registerWebview("tab_mouse_navigation", 42);
769 +
770 + // Electron reports thumb buttons as "back"/"forward" for every frame of the guest, iframes included.
771 + const beforeMouse = preview.listeners.get("before-mouse-event");
772 + expect(beforeMouse, "guest has no before-mouse-event listener").toBeDefined();
773 + const press = (type: "mouseDown" | "mouseUp", button: string) => {
774 + const preventDefault = vi.fn();
775 + beforeMouse!({ preventDefault } as never, { type, button, x: 10, y: 10 } as never);
776 + return preventDefault;
777 + };
778 +
779 + // At the start of tab history the press is still swallowed, or the host app goes back instead.
780 + expect(press("mouseDown", "back")).toHaveBeenCalledOnce();
781 + expect(press("mouseUp", "back")).toHaveBeenCalledOnce();
782 + expect(goBack).not.toHaveBeenCalled();
783 +
784 + canGoBack = true;
785 + expect(press("mouseDown", "back")).toHaveBeenCalledOnce();
786 + expect(goBack).not.toHaveBeenCalled();
787 + expect(press("mouseUp", "back")).toHaveBeenCalledOnce();
788 + expect(goBack).toHaveBeenCalledOnce();
789 + expect(press("mouseDown", "forward")).toHaveBeenCalledOnce();
790 + expect(press("mouseUp", "forward")).toHaveBeenCalledOnce();
791 + expect(goForward).toHaveBeenCalledOnce();
792 + for (const button of ["left", "right"]) {
793 + expect(press("mouseDown", button)).not.toHaveBeenCalled();
794 + expect(press("mouseUp", button)).not.toHaveBeenCalled();
795 + }
796 + expect(goBack).toHaveBeenCalledOnce();
797 + expect(goForward).toHaveBeenCalledOnce();
798 + // The guest replays dropped presses for human input and recordings, in CSS px.
799 + expect(send.mock.calls.filter(([channel]) => channel === "preview:thumb-press")).toEqual([
800 + ["preview:thumb-press", "down", 5, 5, 3],
801 + ["preview:thumb-press", "up", 5, 5, 3],
802 + ["preview:thumb-press", "down", 5, 5, 3],
803 + ["preview:thumb-press", "up", 5, 5, 3],
804 + ["preview:thumb-press", "down", 5, 5, 4],
805 + ["preview:thumb-press", "up", 5, 5, 4],
806 + ]);
807 + }),
808 + ),
809 + );
810 +
811 effectIt.effect("keeps editing and forwarded shortcuts in whichever tab holds focus", () =>
812 withManager((manager) =>
813 Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+22 -10
@@ -81,13 +81,13 @@ import {
81 CANCEL_PICK_CHANNEL,
82 ELEMENT_PICKED_CHANNEL,
83 HUMAN_INPUT_CHANNEL,
84 - MOUSE_NAVIGATE_CHANNEL,
84 PAGE_UNHANDLED_SHORTCUT_CHANNEL,
85 RECORDING_CURSOR_CHANNEL,
86 RECORDING_INPUT_CHANNEL,
87 RECORDING_CONTROLLER_CHANNEL,
88 RECORDING_POINTER_CHANNEL,
89 START_PICK_CHANNEL,
90 + THUMB_PRESS_CHANNEL,
91 } from "./GuestProtocol.ts";
92 import { isPreviewAnnotationPayload } from "./PickedElementPayload.ts";
93 import { captureFavicon, safeHttpOrigin, selectFaviconCandidates } from "./FaviconCapture.ts";
@@ -1490,15 +1490,27 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1490 const humanInput = (_event: unknown, rawSignal?: unknown): void => {
1491 runFork(handleHumanInput(rawSignal));
1492 };
1493 - const mouseNavigate = (_event: unknown, payload?: unknown): void => {
1494 - const direction =
1495 - typeof payload === "object" && payload !== null && "direction" in payload
1496 - ? (payload as { direction?: unknown }).direction
1497 - : undefined;
1498 - if (direction !== "back" && direction !== "forward") return;
1493 + // Chromium navigates the outermost WebContents (the host app) on thumb buttons, even from guest iframes.
1494 + const beforeMouse = (event: Electron.Event, mouse: Electron.MouseInputEvent): void => {
1495 + // Electron reports thumb buttons as "back" and "forward", which its typings omit.
1496 + const button: string | undefined = mouse.button;
1497 + if (button !== "back" && button !== "forward") return;
1498 + if (mouse.type !== "mouseDown" && mouse.type !== "mouseUp") return;
1499 + event.preventDefault();
1500 + // The page never sees the press, so the guest replays it; these coordinates are DIPs, not CSS px.
1501 + const zoom = wc.getZoomFactor();
1502 + const phase = mouse.type === "mouseDown" ? "down" : "up";
1503 + wc.send(
1504 + THUMB_PRESS_CHANNEL,
1505 + phase,
1506 + mouse.x / zoom,
1507 + mouse.y / zoom,
1508 + button === "back" ? 3 : 4,
1509 + );
1510 + if (phase !== "up") return;
1511 runFork(
1512 attempt({ operation: "mouseNavigate", tabId, webContentsId: wc.id }, () => {
1501 - if (direction === "back") {
1513 + if (button === "back") {
1514 if (wc.navigationHistory.canGoBack()) wc.navigationHistory.goBack();
1515 } else if (wc.navigationHistory.canGoForward()) {
1516 wc.navigationHistory.goForward();
@@ -1608,9 +1620,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1620 wc.off("found-in-page", foundInPage as never);
1621 wc.off("did-create-window", windowCreated);
1622 wc.off("before-input-event", beforeInput);
1623 + wc.off("before-mouse-event", beforeMouse);
1624 wc.ipc.off(HUMAN_INPUT_CHANNEL, humanInput);
1625 wc.ipc.off(RECORDING_INPUT_CHANNEL, recordingInput);
1613 - wc.ipc.off(MOUSE_NAVIGATE_CHANNEL, mouseNavigate);
1626 wc.ipc.off(PAGE_UNHANDLED_SHORTCUT_CHANNEL, pageUnhandledShortcut);
1627 detachPasskeys();
1628 }).pipe(Effect.ignore),
@@ -1631,7 +1643,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1643 wc.on("found-in-page", foundInPage as never);
1644 wc.ipc.on(HUMAN_INPUT_CHANNEL, humanInput);
1645 wc.ipc.on(RECORDING_INPUT_CHANNEL, recordingInput);
1634 - wc.ipc.on(MOUSE_NAVIGATE_CHANNEL, mouseNavigate);
1646 wc.ipc.on(PAGE_UNHANDLED_SHORTCUT_CHANNEL, pageUnhandledShortcut);
1647 detachPasskeys = passkeys.attachGuest(wc);
1648 wc.setWindowOpenHandler((details) => {
@@ -1659,6 +1670,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1670 });
1671 wc.on("did-create-window", windowCreated);
1672 wc.on("before-input-event", beforeInput);
1673 + wc.on("before-mouse-event", beforeMouse);
1674 });
1675 yield* Ref.update(attachedRef, (attached) =>
1676 replaceMap(attached, (copy) => {
apps/desktop/src/preview/PickPreload.ts
+23 -29
@@ -39,7 +39,6 @@ import {
39 CANCEL_PICK_CHANNEL,
40 ELEMENT_PICKED_CHANNEL,
41 HUMAN_INPUT_CHANNEL,
42 - MOUSE_NAVIGATE_CHANNEL,
42 POINTER_DOWN_HOST_CHANNEL,
43 PAGE_UNHANDLED_SHORTCUT_CHANNEL,
44 RECORDING_CURSOR_CHANNEL,
@@ -48,6 +47,7 @@ import {
47 RECORDING_INPUT_CHANNEL,
48 RECORDING_CONTROLLER_CHANNEL,
49 START_PICK_CHANNEL,
50 + THUMB_PRESS_CHANNEL,
51 } from "./GuestProtocol.ts";
52 const OVERLAY_ATTRIBUTE = "data-t3code-annotation-ui";
53 const INPUT_LAYER_ATTRIBUTE = "data-t3code-annotation-input";
@@ -249,34 +249,28 @@ const isolateOverlayKey = (event: KeyboardEvent): void => {
249 for (const type of ["keydown", "keypress", "keyup"] as const)
250 window.addEventListener(type, isolateOverlayKey, true);
251
252 -const MOUSE_BUTTON_BACK = 3;
253 -const MOUSE_BUTTON_FORWARD = 4;
254 -
255 -const navigationDirectionForButton = (button: number): "back" | "forward" | null => {
256 - if (button === MOUSE_BUTTON_BACK) return "back";
257 - if (button === MOUSE_BUTTON_FORWARD) return "forward";
258 - return null;
259 -};
260 -
261 -// Chromium routes thumb-button navigation to the focused WebContents; navigate this hovered guest explicitly.
262 -const suppressNavigationButton = (event: MouseEvent): void => {
263 - if (!event.isTrusted || navigationDirectionForButton(event.button) === null) return;
264 - event.preventDefault();
265 - event.stopImmediatePropagation();
266 -};
267 -
268 -const requestNavigationForButton = (event: MouseEvent): void => {
269 - if (!event.isTrusted) return;
270 - const direction = navigationDirectionForButton(event.button);
271 - if (direction === null) return;
272 - event.preventDefault();
273 - event.stopImmediatePropagation();
274 - ipcRenderer.send(MOUSE_NAVIGATE_CHANNEL, { direction });
275 -};
276 -
277 -window.addEventListener("mousedown", suppressNavigationButton, true);
278 -window.addEventListener("mouseup", requestNavigationForButton, true);
279 -window.addEventListener("auxclick", suppressNavigationButton, true);
252 +let replayedThumbPress: { readonly x: number; readonly y: number } | null = null;
253 +ipcRenderer.on(
254 + THUMB_PRESS_CHANNEL,
255 + (_event, phase: unknown, x: unknown, y: unknown, button: unknown) => {
256 + if (phase !== "down" && phase !== "up") return;
257 + if (typeof x !== "number" || typeof y !== "number" || typeof button !== "number") return;
258 + // Main reports presses inside a frame in that frame's coordinates.
259 + const inFrame = document.querySelector("iframe:hover, frame:hover, object:hover, embed:hover");
260 + if (phase === "up") {
261 + const pressed = replayedThumbPress;
262 + replayedThumbPress = null;
263 + // A replayed press is always released, at its own point if the release lands in a frame.
264 + if (pressed) recordingCursor?.press(inFrame ? pressed : { x, y }, "up");
265 + return;
266 + }
267 + if (inFrame) return;
268 + replayedThumbPress = { x, y };
269 + ipcRenderer.sendToHost(POINTER_DOWN_HOST_CHANNEL);
270 + ipcRenderer.send(HUMAN_INPUT_CHANNEL, { kind: "pointer", x, y, button });
271 + recordingCursor?.press({ x, y }, "down");
272 + },
273 +);
274
275 const nextId = createAnnotationIdSource();
276
apps/desktop/src/preview/RecordingCursor.ts
+7 -4
@@ -156,13 +156,15 @@ export function installRecordingCursor(
156 moveHuman(point);
157 pointer(point, "move");
158 };
159 + const press = (point: { readonly x: number; readonly y: number }, phase: "down" | "up") => {
160 + if (phase === "down") moveHuman(point);
161 + pointer(point, phase);
162 + };
163 const pointerDown = (event: PointerEvent) => {
160 - if (event.pointerType === "touch") return;
161 - moveHuman({ x: event.clientX, y: event.clientY });
162 - pointer({ x: event.clientX, y: event.clientY }, "down");
164 + if (event.pointerType !== "touch") press({ x: event.clientX, y: event.clientY }, "down");
165 };
166 const pointerUp = (event: PointerEvent) => {
165 - if (event.pointerType !== "touch") pointer({ x: event.clientX, y: event.clientY }, "up");
167 + if (event.pointerType !== "touch") press({ x: event.clientX, y: event.clientY }, "up");
168 };
169 const keyDown = (event: KeyboardEvent) => {
170 if (event.isComposing || event.repeat) return;
@@ -196,6 +198,7 @@ export function installRecordingCursor(
198 window.addEventListener("blur", hide);
199 return {
200 move,
201 + press,
202 keyPress,
203 setController,
204 setTheme,