Skip to content

Commit b13427d

Browse files
committed
fix(desktop): hand keyboard focus back after an automation click
1 parent e16b8b0 commit b13427d

2 files changed

Lines changed: 125 additions & 11 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,6 +3921,83 @@ describe("PreviewManager", () => {
39213921
),
39223922
);
39233923

3924+
effectIt.effect(
3925+
"hands keyboard focus back to the previous renderer after an automation click",
3926+
() =>
3927+
withManager((manager) =>
3928+
Effect.gen(function* () {
3929+
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
3930+
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
3931+
if (method === "Runtime.evaluate") {
3932+
return { result: { value: { width: 800, height: 600 } } };
3933+
}
3934+
if (method === "Input.dispatchMouseEvent" && params?.type === "mousePressed") {
3935+
humanInput?.({}, { kind: "pointer", x: params.x, y: params.y, button: 0 });
3936+
}
3937+
return undefined;
3938+
});
3939+
const restoreFocus = vi.fn();
3940+
getFocusedWebContents.mockReturnValue({
3941+
id: 7,
3942+
isDestroyed: () => false,
3943+
focus: restoreFocus,
3944+
} as never);
3945+
fromId.mockReturnValue({
3946+
id: 42,
3947+
isDestroyed: () => false,
3948+
getType: () => "webview",
3949+
getURL: () => "https://example.com",
3950+
getTitle: () => "Example",
3951+
isLoading: () => false,
3952+
isDevToolsOpened: () => false,
3953+
getZoomFactor: () => 1,
3954+
setZoomFactor: vi.fn(),
3955+
setAudioMuted: vi.fn(),
3956+
isCurrentlyAudible: () => false,
3957+
on: vi.fn(),
3958+
off: vi.fn(),
3959+
ipc: {
3960+
on: vi.fn((channel: string, listener: typeof humanInput) => {
3961+
if (channel === "preview:human-input") humanInput = listener;
3962+
}),
3963+
off: vi.fn(),
3964+
},
3965+
send: webviewSend,
3966+
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
3967+
setIgnoreMenuShortcuts: vi.fn(),
3968+
setWindowOpenHandler: vi.fn(),
3969+
debugger: {
3970+
isAttached: () => false,
3971+
attach: vi.fn(),
3972+
sendCommand,
3973+
on: vi.fn(),
3974+
off: vi.fn(),
3975+
},
3976+
} as never);
3977+
3978+
yield* manager.createTab("tab_1");
3979+
yield* manager.registerWebview("tab_1", 42);
3980+
const click = yield* manager
3981+
.automationClick("tab_1", { x: 120, y: 80 })
3982+
.pipe(Effect.forkChild({ startImmediately: true }));
3983+
yield* TestClock.adjust(200);
3984+
yield* Fiber.join(click);
3985+
3986+
expect(restoreFocus).toHaveBeenCalledTimes(1);
3987+
expect(restoreFocus.mock.invocationCallOrder[0]).toBeGreaterThan(
3988+
sendCommand.mock.invocationCallOrder.at(-1) ?? 0,
3989+
);
3990+
3991+
const offscreen = yield* manager
3992+
.automationClick("tab_1", { x: 5000, y: 80 })
3993+
.pipe(Effect.exit, Effect.forkChild({ startImmediately: true }));
3994+
yield* TestClock.adjust(200);
3995+
expect((yield* Fiber.join(offscreen))._tag).toBe("Failure");
3996+
expect(restoreFocus).toHaveBeenCalledTimes(2);
3997+
}),
3998+
),
3999+
);
4000+
39244001
effectIt.effect("types in background webviews and enables native key input", () =>
39254002
withManager((manager) =>
39264003
Effect.gen(function* () {

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,7 +3719,48 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
37193719
);
37203720
});
37213721

3722+
// Dispatching input moves keyboard focus into the guest renderer as a side
3723+
// effect. Hand it back to whatever had it before, so the user's next keystroke
3724+
// does not land in the previewed page, which may not even be visible.
3725+
const restoreFocusedWebContents = Effect.fn("PreviewManager.restoreFocusedWebContents")(
3726+
function* (
3727+
operation: string,
3728+
tabId: string,
3729+
wc: Electron.WebContents,
3730+
previouslyFocused: Electron.WebContents | null,
3731+
) {
3732+
if (!previouslyFocused || previouslyFocused.id === wc.id || previouslyFocused.isDestroyed()) {
3733+
return;
3734+
}
3735+
yield* attempt({ operation, tabId, webContentsId: previouslyFocused.id }, () =>
3736+
previouslyFocused.focus(),
3737+
).pipe(Effect.ignore);
3738+
},
3739+
);
3740+
37223741
const performAutomationClick = Effect.fn("PreviewManager.performAutomationClick")(function* (
3742+
tabId: string,
3743+
wc: Electron.WebContents,
3744+
input: PreviewAutomationClickInput,
3745+
send: SendCommand,
3746+
) {
3747+
const previouslyFocused = yield* attempt(
3748+
{ operation: "automationClick.getFocusedWebContents", tabId, webContentsId: wc.id },
3749+
() => webContents.getFocusedWebContents(),
3750+
);
3751+
yield* dispatchAutomationClick(tabId, input, send).pipe(
3752+
Effect.ensuring(
3753+
restoreFocusedWebContents(
3754+
"automationClick.restoreFocusedWebContents",
3755+
tabId,
3756+
wc,
3757+
previouslyFocused,
3758+
),
3759+
),
3760+
);
3761+
});
3762+
3763+
const dispatchAutomationClick = Effect.fn("PreviewManager.dispatchAutomationClick")(function* (
37233764
tabId: string,
37243765
input: PreviewAutomationClickInput,
37253766
send: SendCommand,
@@ -3782,7 +3823,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
37823823
) {
37833824
const wc = yield* requireWebContents(tabId);
37843825
yield* withControlSession(tabId, wc, "click", (send) =>
3785-
performAutomationClick(tabId, input, send),
3826+
performAutomationClick(tabId, wc, input, send),
37863827
);
37873828
});
37883829

@@ -3935,16 +3976,12 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
39353976
yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(
39363977
Effect.ignore,
39373978
);
3938-
if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
3939-
yield* attempt(
3940-
{
3941-
operation: "automationPress.restoreFocusedWebContents",
3942-
tabId,
3943-
webContentsId: previouslyFocused.id,
3944-
},
3945-
() => previouslyFocused.focus(),
3946-
).pipe(Effect.ignore);
3947-
}
3979+
yield* restoreFocusedWebContents(
3980+
"automationPress.restoreFocusedWebContents",
3981+
tabId,
3982+
wc,
3983+
previouslyFocused,
3984+
);
39483985
});
39493986

39503987
// Focus the guest WebContents itself, not its containing BrowserWindow. This

0 commit comments

Comments
 (0)