Skip to content

Commit 0c73e4d

Browse files
committed
fix(desktop): agent keys in a background browser tab no longer pass the shortcut gate
GuestInput sends an agent's keys straight to a tab that does not hold focus, which fires before-input-event there. Window focus alone would let those keys open the native editing roles or forward an app shortcut, so the gate also requires the tab's own focused frame.
1 parent b3a3d8c commit 0c73e4d

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ const makeFaviconWebContents = (options?: {
507507
session: { fetch },
508508
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
509509
isFocused: vi.fn(() => true),
510+
focusedFrame: {} as object | null,
510511
setIgnoreMenuShortcuts: vi.fn(),
511512
setWindowOpenHandler: vi.fn(),
512513
executeJavaScriptInIsolatedWorld,
@@ -840,7 +841,7 @@ describe("PreviewManager", () => {
840841
),
841842
);
842843

843-
effectIt.effect("keeps editing and forwarded shortcuts in every tab of a focused window", () =>
844+
effectIt.effect("keeps editing and forwarded shortcuts in whichever tab holds focus", () =>
844845
withManager((manager) =>
845846
Effect.gen(function* () {
846847
const send = vi.fn();
@@ -873,6 +874,24 @@ describe("PreviewManager", () => {
873874
beforeInput({ preventDefault } as never, { ...key, key: "T", shift: true } as never);
874875
expect(preventDefault).toHaveBeenCalledOnce();
875876
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();
876895
}),
877896
),
878897
);

‎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-
// An unfocused window's input must not edit whichever renderer has focus.
1485-
// Not getFocusedWebContents(): every guest of a focused window reports focus.
14861488
contents.setIgnoreMenuShortcuts(
1487-
!isPreviewEditingShortcut(input, hostPlatform) || !contents.isFocused(),
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-
wc.isFocused() &&
1510+
holdsKeyboardFocus(wc) &&
15091511
!(
15101512
hostPlatform !== "darwin" &&
15111513
input.control &&

0 commit comments

Comments
 (0)