fix(web): preserve composer focus during preview presses #9530
Closed0bkevin(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/9530/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 656OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -652,8 +653,4 @@652653 );653654 }654655 case "press": {655- const ready = await requireReadyTab();656- return await ready.bridge.automation.press(657- ready.runtimeTabId,658- request.input as Parameters<typeof ready.bridge.automation.press>[1],659- );656+ return await withPreviewAutomationFocus(async () => {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/preview/Manager.ts line 3920Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -3887,0 +3891,21 @@3891+ ).pipe(Effect.orElseSucceed(() => null));3892+ const automationOwnsFocus = focusedWindow !== null && focusedWebContents?.id === wc.id;3893+ if (3894+ restoreFocus &&3895+ automationOwnsFocus &&3896+ previouslyFocused &&3897+ previouslyFocused.id !== wc.id &&3898+ !previouslyFocused.isDestroyed()3899+ ) {3900+ const restored = yield* attempt(3901+ {3902+ operation: "automationPress.restoreFocusedWebContents",3903+ tabId,3904+ webContentsId: previouslyFocused.id,3905+ },3906+ () => previouslyFocused.focus(),3907+ ).pipe(3908+ Effect.as(true),3909+ Effect.orElseSucceed(() => false),3910+ );3911+ if (restored) focusDisposition = "restored";
- apps/desktop/src/preview/Manager.ts line 3946OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3897,7 +3932,15 @@3932+ () => wc.focus(),3933+ );3934+ yield* send("Page.bringToFront");3935+ yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });3936+ yield* expectAgentInput(tabId, keySequence.signal);3937+ keyDownAttempted = true;3938+ yield* send("Input.dispatchKeyEvent", keySequence.keyDown);3939+ }),3940+ );3941+ const interrupted =3942+ pressExit._tag === "Failure" &&3943+ isPreviewAutomationControlInterruptedError(3944+ Option.getOrNull(Cause.findErrorOption(pressExit.cause)),38973945 );3898- yield* send("Page.bringToFront");3899- yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });3900- yield* expectAgentInput(tabId, keySequence.signal);3901- keyDownAttempted = true;3902- yield* send("Input.dispatchKeyEvent", keySequence.keyDown);3903- }).pipe(Effect.ensuring(releaseInput));3946+ yield* releaseInput(!interrupted);
0bkevin(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Alternatives considered#
I reviewed the two earlier open approaches and then verified the behavior in the real Electron desktop app:
main. This PR keeps that usefultry/finallyrestoration behavior but scopes it only topreview_pressand adds current focused regression coverage.executeJavaScriptand a temporary randomwindowproperty. That adds an unnecessary process boundary and global lifecycle state, and it has no regression tests for the focus behavior.The integrated Electron pass also caught and corrected an earlier guard in this PR: native Tab dispatch can move focus to another connected host button, so restoration must not be conditional on
document.activeElementbecomingbody/null. The final implementation always restores the previously focused element when it remains connected, on both success and failure. On the unfixed branch, the next typed character was dropped; on this branch, focus stayed oncomposer-editorand the character was accepted.