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 23, 04:15 UTC
fix(desktop): paste and other editing shortcuts work in every browser tab (#19452)
qdqqd committed
Oct 18, 2026 at 21:19 UTC
558b55a6b6d5a9fb8903f35dba14ceb64565c83e
2 files changed
+66
-11
apps/desktop/src/preview/Manager.test.ts
+60
-7
@@ -506,6 +506,8 @@ const makeFaviconWebContents = (options?: {
506
send: webviewSend,
507
session: { fetch },
508
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
509
+ isFocused: vi.fn(() => true),
510
+ focusedFrame: {} as object | null,
511
setIgnoreMenuShortcuts: vi.fn(),
512
setWindowOpenHandler: vi.fn(),
513
executeJavaScriptInIsolatedWorld,
@@ -643,7 +645,6 @@ describe("PreviewManager", () => {
645
const hostWebContents = { sendInputEvent, isDestroyed: () => false };
646
Object.assign(preview.webContents, { hostWebContents });
647
fromId.mockReturnValue(preview.webContents);
646
- getFocusedWebContents.mockReturnValue(preview.webContents as never);
648
yield* manager.setMainWindow({
649
isDestroyed: () => false,
650
once: vi.fn(),
@@ -708,7 +709,6 @@ describe("PreviewManager", () => {
709
hostWebContents: { isDestroyed: () => false, send },
710
});
711
fromId.mockReturnValue(preview.webContents);
711
- getFocusedWebContents.mockReturnValue(preview.webContents as never);
712
yield* manager.createTab("tab_reopen");
713
yield* manager.registerWebview("tab_reopen", 42);
714
const shortcut = parseKeybindingShortcut(chord)!;
@@ -737,12 +737,12 @@ describe("PreviewManager", () => {
737
expect(preventDefault).toHaveBeenCalledTimes("isAutoRepeat" in overrides ? 1 : 0);
738
expect(send).toHaveBeenCalledOnce();
739
}
740
- getFocusedWebContents.mockReturnValue(null);
740
+ vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(false);
741
preventDefault.mockClear();
742
beforeInput({ preventDefault } as never, input as never);
743
expect(preventDefault).not.toHaveBeenCalled();
744
expect(send).toHaveBeenCalledOnce();
745
- getFocusedWebContents.mockReturnValue(preview.webContents as never);
745
+ vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(true);
746
yield* manager.setForwardedShortcuts([]);
747
beforeInput({ preventDefault } as never, input as never);
748
expect(preventDefault).not.toHaveBeenCalled();
@@ -770,7 +770,6 @@ describe("PreviewManager", () => {
770
hostWebContents: { isDestroyed: () => false, send },
771
});
772
fromId.mockReturnValue(preview.webContents);
773
- getFocusedWebContents.mockReturnValue(preview.webContents as never);
773
yield* manager.createTab("tab_alt_graph");
774
yield* manager.registerWebview("tab_alt_graph", 42);
775
yield* manager.setForwardedShortcuts([
@@ -812,7 +811,6 @@ describe("PreviewManager", () => {
811
812
for (const browser of [preview, popup]) {
813
const contents = browser.webContents as Electron.WebContents;
815
- getFocusedWebContents.mockReturnValue(browser.webContents as never);
814
const beforeInput = browser.listeners.get("before-input-event")!;
815
const preventDefault = vi.fn();
816
const input = {
@@ -834,7 +832,7 @@ describe("PreviewManager", () => {
832
beforeInput({ preventDefault } as never, { ...input, key: "w" } as never);
833
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
834
837
- getFocusedWebContents.mockReturnValue(null);
835
+ vi.mocked(contents.isFocused).mockReturnValue(false);
836
beforeInput({ preventDefault } as never, input as never);
837
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
838
expect(preventDefault).not.toHaveBeenCalled();
@@ -843,6 +841,61 @@ describe("PreviewManager", () => {
841
),
842
);
843
844
+ effectIt.effect("keeps editing and forwarded shortcuts in whichever tab holds focus", () =>
845
+ withManager((manager) =>
846
+ Effect.gen(function* () {
847
+ const send = vi.fn();
848
+ const host = { isDestroyed: () => false, send };
849
+ const older = makeFaviconWebContents({ id: 42 });
850
+ const newer = makeFaviconWebContents({ id: 43 });
851
+ for (const tab of [older, newer]) Object.assign(tab.webContents, { hostWebContents: host });
852
+ // Electron names one arbitrary guest here when several share a focused window.
853
+ getFocusedWebContents.mockReturnValue(newer.webContents as never);
854
+ fromId.mockReturnValue(older.webContents);
855
+ yield* manager.createTab("tab_older");
856
+ yield* manager.registerWebview("tab_older", 42);
857
+ fromId.mockReturnValue(newer.webContents);
858
+ yield* manager.createTab("tab_newer");
859
+ yield* manager.registerWebview("tab_newer", 43);
860
+ const shortcut = parseKeybindingShortcut("mod+shift+t")!;
861
+ yield* manager.setForwardedShortcuts([{ command: "view.reopenClosed", shortcut }]);
862
+
863
+ const beforeInput = older.listeners.get("before-input-event")!;
864
+ const key = { type: "keyDown", meta: true, control: false, alt: false };
865
+ beforeInput(
866
+ { preventDefault: vi.fn() } as never,
867
+ { ...key, key: "v", shift: false } as never,
868
+ );
869
+ expect(
870
+ (older.webContents as Electron.WebContents).setIgnoreMenuShortcuts,
871
+ ).toHaveBeenLastCalledWith(false);
872
+
873
+ const preventDefault = vi.fn();
874
+ beforeInput({ preventDefault } as never, { ...key, key: "T", shift: true } as never);
875
+ expect(preventDefault).toHaveBeenCalledOnce();
876
+ expect(send).toHaveBeenCalledExactlyOnceWith("desktop:menu-action", "view.reopenClosed");
877
+
878
+ // The person types in the older tab, so keys reaching the newer one are an agent's.
879
+ Object.assign(newer.webContents, { focusedFrame: null });
880
+ const agentInput = newer.listeners.get("before-input-event")!;
881
+ const agentPreventDefault = vi.fn();
882
+ agentInput(
883
+ { preventDefault: agentPreventDefault } as never,
884
+ { ...key, key: "v", shift: false } as never,
885
+ );
886
+ expect(
887
+ (newer.webContents as Electron.WebContents).setIgnoreMenuShortcuts,
888
+ ).toHaveBeenLastCalledWith(true);
889
+ agentInput(
890
+ { preventDefault: agentPreventDefault } as never,
891
+ { ...key, key: "T", shift: true } as never,
892
+ );
893
+ expect(agentPreventDefault).not.toHaveBeenCalled();
894
+ expect(send).toHaveBeenCalledOnce();
895
+ }),
896
+ ),
897
+ );
898
+
899
effectIt.effect("rejects a destroyed webview during registration", () =>
900
withManager((manager) =>
901
Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+6
-4
@@ -1479,12 +1479,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1479
}).pipe(Effect.ignore),
1480
);
1481
};
1482
+ // Not getFocusedWebContents(): every guest of a focused window reports focus.
1483
+ // An agent's keys reach a guest without a focused frame through GuestInput.
1484
+ const holdsKeyboardFocus = (contents: Electron.WebContents) =>
1485
+ contents.isFocused() && contents.focusedFrame !== null;
1486
const syncMenuShortcuts = (contents: Electron.WebContents, input: Electron.Input): void => {
1487
if (input.type !== "keyDown") return;
1484
- // Background automation must not edit whichever other renderer has focus.
1488
contents.setIgnoreMenuShortcuts(
1486
- !isPreviewEditingShortcut(input, hostPlatform) ||
1487
- webContents.getFocusedWebContents() !== contents,
1489
+ !isPreviewEditingShortcut(input, hostPlatform) || !holdsKeyboardFocus(contents),
1490
);
1491
};
1492
// A popup opens with Electron's default handler, so the page inside it could
@@ -1505,7 +1507,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1507
!input.isComposing &&
1508
host &&
1509
!host.isDestroyed() &&
1508
- webContents.getFocusedWebContents() === wc &&
1510
+ holdsKeyboardFocus(wc) &&
1511
!(
1512
hostPlatform !== "darwin" &&
1513
input.control &&