Skip to content

Commit 558b55a

Browse files
authored
fix(desktop): paste and other editing shortcuts work in every browser tab (#19452)
1 parent 555fb6f commit 558b55a

2 files changed

Lines changed: 66 additions & 11 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ const makeFaviconWebContents = (options?: {
506506
send: webviewSend,
507507
session: { fetch },
508508
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
509+
isFocused: vi.fn(() => true),
510+
focusedFrame: {} as object | null,
509511
setIgnoreMenuShortcuts: vi.fn(),
510512
setWindowOpenHandler: vi.fn(),
511513
executeJavaScriptInIsolatedWorld,
@@ -643,7 +645,6 @@ describe("PreviewManager", () => {
643645
const hostWebContents = { sendInputEvent, isDestroyed: () => false };
644646
Object.assign(preview.webContents, { hostWebContents });
645647
fromId.mockReturnValue(preview.webContents);
646-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
647648
yield* manager.setMainWindow({
648649
isDestroyed: () => false,
649650
once: vi.fn(),
@@ -708,7 +709,6 @@ describe("PreviewManager", () => {
708709
hostWebContents: { isDestroyed: () => false, send },
709710
});
710711
fromId.mockReturnValue(preview.webContents);
711-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
712712
yield* manager.createTab("tab_reopen");
713713
yield* manager.registerWebview("tab_reopen", 42);
714714
const shortcut = parseKeybindingShortcut(chord)!;
@@ -737,12 +737,12 @@ describe("PreviewManager", () => {
737737
expect(preventDefault).toHaveBeenCalledTimes("isAutoRepeat" in overrides ? 1 : 0);
738738
expect(send).toHaveBeenCalledOnce();
739739
}
740-
getFocusedWebContents.mockReturnValue(null);
740+
vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(false);
741741
preventDefault.mockClear();
742742
beforeInput({ preventDefault } as never, input as never);
743743
expect(preventDefault).not.toHaveBeenCalled();
744744
expect(send).toHaveBeenCalledOnce();
745-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
745+
vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(true);
746746
yield* manager.setForwardedShortcuts([]);
747747
beforeInput({ preventDefault } as never, input as never);
748748
expect(preventDefault).not.toHaveBeenCalled();
@@ -770,7 +770,6 @@ describe("PreviewManager", () => {
770770
hostWebContents: { isDestroyed: () => false, send },
771771
});
772772
fromId.mockReturnValue(preview.webContents);
773-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
774773
yield* manager.createTab("tab_alt_graph");
775774
yield* manager.registerWebview("tab_alt_graph", 42);
776775
yield* manager.setForwardedShortcuts([
@@ -812,7 +811,6 @@ describe("PreviewManager", () => {
812811

813812
for (const browser of [preview, popup]) {
814813
const contents = browser.webContents as Electron.WebContents;
815-
getFocusedWebContents.mockReturnValue(browser.webContents as never);
816814
const beforeInput = browser.listeners.get("before-input-event")!;
817815
const preventDefault = vi.fn();
818816
const input = {
@@ -834,7 +832,7 @@ describe("PreviewManager", () => {
834832
beforeInput({ preventDefault } as never, { ...input, key: "w" } as never);
835833
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
836834

837-
getFocusedWebContents.mockReturnValue(null);
835+
vi.mocked(contents.isFocused).mockReturnValue(false);
838836
beforeInput({ preventDefault } as never, input as never);
839837
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
840838
expect(preventDefault).not.toHaveBeenCalled();
@@ -843,6 +841,61 @@ describe("PreviewManager", () => {
843841
),
844842
);
845843

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+
846899
effectIt.effect("rejects a destroyed webview during registration", () =>
847900
withManager((manager) =>
848901
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,12 +1479,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
14791479
}).pipe(Effect.ignore),
14801480
);
14811481
};
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;
14821486
const syncMenuShortcuts = (contents: Electron.WebContents, input: Electron.Input): void => {
14831487
if (input.type !== "keyDown") return;
1484-
// Background automation must not edit whichever other renderer has focus.
14851488
contents.setIgnoreMenuShortcuts(
1486-
!isPreviewEditingShortcut(input, hostPlatform) ||
1487-
webContents.getFocusedWebContents() !== contents,
1489+
!isPreviewEditingShortcut(input, hostPlatform) || !holdsKeyboardFocus(contents),
14881490
);
14891491
};
14901492
// 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
15051507
!input.isComposing &&
15061508
host &&
15071509
!host.isDestroyed() &&
1508-
webContents.getFocusedWebContents() === wc &&
1510+
holdsKeyboardFocus(wc) &&
15091511
!(
15101512
hostPlatform !== "darwin" &&
15111513
input.control &&

0 commit comments

Comments
 (0)