Skip to content

Commit b3a3d8c

Browse files
committed
fix(desktop): paste and other editing shortcuts work in every browser tab
Electron reports every webview guest of a focused window as focused, so getFocusedWebContents() names one arbitrary tab. The preview gate compared against it, which turned off Cmd+V, Cmd+C and the forwarded app shortcuts in every other open tab. Gate on the tab's own isFocused() instead.
1 parent 86534db commit b3a3d8c

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ const makeFaviconWebContents = (options?: {
506506
send: webviewSend,
507507
session: { fetch },
508508
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
509+
isFocused: vi.fn(() => true),
509510
setIgnoreMenuShortcuts: vi.fn(),
510511
setWindowOpenHandler: vi.fn(),
511512
executeJavaScriptInIsolatedWorld,
@@ -643,7 +644,6 @@ describe("PreviewManager", () => {
643644
const hostWebContents = { sendInputEvent, isDestroyed: () => false };
644645
Object.assign(preview.webContents, { hostWebContents });
645646
fromId.mockReturnValue(preview.webContents);
646-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
647647
yield* manager.setMainWindow({
648648
isDestroyed: () => false,
649649
once: vi.fn(),
@@ -708,7 +708,6 @@ describe("PreviewManager", () => {
708708
hostWebContents: { isDestroyed: () => false, send },
709709
});
710710
fromId.mockReturnValue(preview.webContents);
711-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
712711
yield* manager.createTab("tab_reopen");
713712
yield* manager.registerWebview("tab_reopen", 42);
714713
const shortcut = parseKeybindingShortcut(chord)!;
@@ -737,12 +736,12 @@ describe("PreviewManager", () => {
737736
expect(preventDefault).toHaveBeenCalledTimes("isAutoRepeat" in overrides ? 1 : 0);
738737
expect(send).toHaveBeenCalledOnce();
739738
}
740-
getFocusedWebContents.mockReturnValue(null);
739+
vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(false);
741740
preventDefault.mockClear();
742741
beforeInput({ preventDefault } as never, input as never);
743742
expect(preventDefault).not.toHaveBeenCalled();
744743
expect(send).toHaveBeenCalledOnce();
745-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
744+
vi.mocked((preview.webContents as Electron.WebContents).isFocused).mockReturnValue(true);
746745
yield* manager.setForwardedShortcuts([]);
747746
beforeInput({ preventDefault } as never, input as never);
748747
expect(preventDefault).not.toHaveBeenCalled();
@@ -770,7 +769,6 @@ describe("PreviewManager", () => {
770769
hostWebContents: { isDestroyed: () => false, send },
771770
});
772771
fromId.mockReturnValue(preview.webContents);
773-
getFocusedWebContents.mockReturnValue(preview.webContents as never);
774772
yield* manager.createTab("tab_alt_graph");
775773
yield* manager.registerWebview("tab_alt_graph", 42);
776774
yield* manager.setForwardedShortcuts([
@@ -812,7 +810,6 @@ describe("PreviewManager", () => {
812810

813811
for (const browser of [preview, popup]) {
814812
const contents = browser.webContents as Electron.WebContents;
815-
getFocusedWebContents.mockReturnValue(browser.webContents as never);
816813
const beforeInput = browser.listeners.get("before-input-event")!;
817814
const preventDefault = vi.fn();
818815
const input = {
@@ -834,7 +831,7 @@ describe("PreviewManager", () => {
834831
beforeInput({ preventDefault } as never, { ...input, key: "w" } as never);
835832
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
836833

837-
getFocusedWebContents.mockReturnValue(null);
834+
vi.mocked(contents.isFocused).mockReturnValue(false);
838835
beforeInput({ preventDefault } as never, input as never);
839836
expect(contents.setIgnoreMenuShortcuts).toHaveBeenLastCalledWith(true);
840837
expect(preventDefault).not.toHaveBeenCalled();
@@ -843,6 +840,43 @@ describe("PreviewManager", () => {
843840
),
844841
);
845842

843+
effectIt.effect("keeps editing and forwarded shortcuts in every tab of a focused window", () =>
844+
withManager((manager) =>
845+
Effect.gen(function* () {
846+
const send = vi.fn();
847+
const host = { isDestroyed: () => false, send };
848+
const older = makeFaviconWebContents({ id: 42 });
849+
const newer = makeFaviconWebContents({ id: 43 });
850+
for (const tab of [older, newer]) Object.assign(tab.webContents, { hostWebContents: host });
851+
// Electron names one arbitrary guest here when several share a focused window.
852+
getFocusedWebContents.mockReturnValue(newer.webContents as never);
853+
fromId.mockReturnValue(older.webContents);
854+
yield* manager.createTab("tab_older");
855+
yield* manager.registerWebview("tab_older", 42);
856+
fromId.mockReturnValue(newer.webContents);
857+
yield* manager.createTab("tab_newer");
858+
yield* manager.registerWebview("tab_newer", 43);
859+
const shortcut = parseKeybindingShortcut("mod+shift+t")!;
860+
yield* manager.setForwardedShortcuts([{ command: "view.reopenClosed", shortcut }]);
861+
862+
const beforeInput = older.listeners.get("before-input-event")!;
863+
const key = { type: "keyDown", meta: true, control: false, alt: false };
864+
beforeInput(
865+
{ preventDefault: vi.fn() } as never,
866+
{ ...key, key: "v", shift: false } as never,
867+
);
868+
expect(
869+
(older.webContents as Electron.WebContents).setIgnoreMenuShortcuts,
870+
).toHaveBeenLastCalledWith(false);
871+
872+
const preventDefault = vi.fn();
873+
beforeInput({ preventDefault } as never, { ...key, key: "T", shift: true } as never);
874+
expect(preventDefault).toHaveBeenCalledOnce();
875+
expect(send).toHaveBeenCalledExactlyOnceWith("desktop:menu-action", "view.reopenClosed");
876+
}),
877+
),
878+
);
879+
846880
effectIt.effect("rejects a destroyed webview during registration", () =>
847881
withManager((manager) =>
848882
Effect.gen(function* () {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,10 +1481,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
14811481
};
14821482
const syncMenuShortcuts = (contents: Electron.WebContents, input: Electron.Input): void => {
14831483
if (input.type !== "keyDown") return;
1484-
// Background automation must not edit whichever other renderer has focus.
1484+
// An unfocused window's input must not edit whichever renderer has focus.
1485+
// Not getFocusedWebContents(): every guest of a focused window reports focus.
14851486
contents.setIgnoreMenuShortcuts(
1486-
!isPreviewEditingShortcut(input, hostPlatform) ||
1487-
webContents.getFocusedWebContents() !== contents,
1487+
!isPreviewEditingShortcut(input, hostPlatform) || !contents.isFocused(),
14881488
);
14891489
};
14901490
// A popup opens with Electron's default handler, so the page inside it could
@@ -1505,7 +1505,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
15051505
!input.isComposing &&
15061506
host &&
15071507
!host.isDestroyed() &&
1508-
webContents.getFocusedWebContents() === wc &&
1508+
wc.isFocused() &&
15091509
!(
15101510
hostPlatform !== "darwin" &&
15111511
input.control &&

0 commit comments

Comments
 (0)