fix(desktop): isolate preview keyboard input #8494
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromt3code/fix-preview-keyboard-isolation-3715opened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/desktop/src/preview/Manager.ts lines 4585 to 4601OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3987,0 +4581,21 @@4581+]);4582+export type PreviewAutomationKeyboardUnavailableReason =4583+ typeof PreviewAutomationKeyboardUnavailableReason.Type;4584+4585+export class PreviewAutomationKeyboardUnavailableError extends Schema.TaggedErrorClass<PreviewAutomationKeyboardUnavailableError>()(4586+ "PreviewAutomationKeyboardUnavailableError",4587+ {4588+ tabId: Schema.String,4589+ webContentsId: Schema.Number,4590+ reason: PreviewAutomationKeyboardUnavailableReason,4591+ },4592+) {4593+ override get message(): string {4594+ if (this.reason === "window-not-focused") {4595+ return `Preview tab ${this.tabId} cannot receive keyboard input while its desktop window is unfocused`;4596+ }4597+ return this.reason === "focused-frame-unsupported"4598+ ? `Preview tab ${this.tabId} cannot receive keyboard input while a frame is focused`4599+ : `Preview tab ${this.tabId} did not confirm keyboard input delivery`;4600+ }4601+}apps/web/src/components/preview/previewAutomationErrors.ts lines 137 to 164OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -136,0 +144,21 @@144+ tabId: Schema.NullOr(PreviewTabId),145+ reason: Schema.Literals([146+ "window-not-focused",147+ "focused-frame-unsupported",148+ "delivery-not-confirmed",149+ ]),150+ },151+) {152+ get responseTag() {153+ return "PreviewAutomationExecutionError" as const;154+ }155+156+ override get message(): string {157+ if (this.reason === "window-not-focused") {158+ return `Preview automation ${this.operation} request ${this.requestId} cannot send keyboard input because the desktop window is not focused.`;159+ }160+ return this.reason === "focused-frame-unsupported"161+ ? `Preview automation ${this.operation} request ${this.requestId} cannot send keyboard input to a focused frame.`162+ : `Preview automation ${this.operation} request ${this.requestId} did not receive keyboard delivery confirmation from tab ${this.tabId ?? "unassigned"}.`;163+ }164+}
- apps/desktop/src/preview/PreviewKeyboard.ts line 105OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -162,4 +102,4 @@162102 return {163103 code: input.key.length > 1 ? input.key : "",164104 key: input.key,165- keyCode: 0,105+ nativeKeyCode: input.key,@@ -3458,8 +3895,14 @@3895+ },3896+ );3897+ const sendNativeKey = (marker: ActiveAgentKey, event: Electron.KeyboardInputEvent): void => {3898+ activeAgentKeys.set(attachment.attachmentId, marker);3899+ try {3900+ wc.sendInputEvent(event);3901+ } finally {3902+ if (activeAgentKeys.get(attachment.attachmentId) === marker) {3903+ activeAgentKeys.delete(attachment.attachmentId);3904+ }34583905 }3459- yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(3460- Effect.ignore,3461- );3462- if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {3463- yield* attempt(3464- {3465- operation: "automationPress.restoreFocusedWebContents",3906+ };3907+ let keyDownAttempted = false;3908+ const releaseInput = attempt(
- apps/desktop/src/preview/PreviewKeyboard.ts line 129OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -182,18 +125,5 @@182- const location = definition.location ?? 0;183- const commands = options?.isMac ? macEditingCommands(definition.code, input.modifiers) : [];184- const shared = {185- key: definition.key,186- code: definition.code,187- modifiers,188- windowsVirtualKeyCode: definition.keyCode,189- location,190- isKeypad: location === 3,191- };125+ const shared = { keyCode: definition.nativeKeyCode, modifiers };192126193127 return {194- keyDown: {195- type: text ? "keyDown" : "rawKeyDown",196- ...shared,197- ...(text ? { text, unmodifiedText: text } : {}),198- ...(commands.length > 0 ? { commands } : {}),199- },128+ keyDown: { type: "rawKeyDown", ...shared },129+ ...(!suppressText && definition.text ? { char: { type: "char" as const, ...shared } } : {}),
- apps/desktop/src/preview/Manager.ts line 4081Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3448,0 +4058,21 @@4058+ const tagName = document.activeElement?.tagName;4059+ return tagName === "IFRAME" || tagName === "FRAME";4060+ })()`,4061+ true,4062+ );4063+ if (focusedFrame) {4064+ return yield* new PreviewAutomationKeyboardFocusedFrameUnsupportedError({4065+ tabId,4066+ webContentsId: wc.id,4067+ });4068+ }4069+4070+ const receiptDocument = captureKeyboardDocument(attachment, wc);4071+ if (receiptDocument.frame.detached) {4072+ return yield* new PreviewAutomationTargetChangedError({4073+ operation: "press",4074+ tabId,4075+ webContentsId: wc.id,4076+ });4077+ }4078+ const keySequence = makePreviewAutomationKeySequence(input);
- apps/desktop/src/preview/PreviewKeyboard.ts line 175Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -200,2 +125,20 @@125+ keyDown: { type: "rawKeyDown", ...shared },126+ ...(!suppressText && definition.text127+ ? {128+ char: {129+ type: "char" as const,130+ keyCode: definition.text,131+ modifiers,132+ },133+ }134+ : {}),200135 keyUp: { type: "keyUp", ...shared },201- signal: { kind: "key", key: definition.key, code: definition.code },136+ signal: {137+ kind: "key",138+ key: definition.key,139+ code: definition.code,140+ meta: modifiers.includes("meta"),141+ shift: modifiers.includes("shift"),142+ control: modifiers.includes("control"),143+ alt: modifiers.includes("alt"),144+ },
- apps/desktop/src/preview/Manager.ts line 2056OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1626,0 +2036,21 @@2036+ const physicalMatchIndex = attachment.pendingKeyInputs.findIndex((candidate) => {2037+ if (candidate.nativeKey) return false;2038+ return matchesReceipt(candidate);2039+ });2040+ const agentIndex = attachment.pendingKeyInputs.findIndex(2041+ (candidate) => candidate.nativeKey !== undefined,2042+ );2043+ if (physicalMatchIndex >= 0 && (agentIndex === -1 || physicalMatchIndex < agentIndex)) {2044+ attachment.pendingKeyInputs.splice(0, physicalMatchIndex + 1);2045+ return;2046+ }2047+ }2048+ const pendingAgentDocument = attachment.pendingKeyInputs.find(2049+ (candidate) => candidate.nativeKey !== undefined,2050+ )?.document;2051+ if (pendingAgentDocument || attachment.pendingAgentKeys.size > 0) {2052+ markKeyboardDeliveryUncertain(2053+ attachment,2054+ wc,2055+ pendingAgentDocument ?? captureKeyboardDocument(attachment, wc),2056+ );@@ -3477,0 +4211,21 @@4211+ }4212+ const focusedFrame = wc.focusedFrame;4213+ if (focusedFrame != null && focusedFrame !== wc.mainFrame) {4214+ releaseMenuShortcutLease(attachment, wc, menuShortcutLease);4215+ preflightError = new PreviewAutomationKeyboardFocusedFrameUnsupportedError({4216+ tabId,4217+ webContentsId: wc.id,4218+ });4219+ return;4220+ }4221+ sendNativeKey(keyDownMarker, keySequence.keyDown);4222+ keyDownAttempted = true;4223+ nativeKeyDispatched = true;4224+ if (!keyDownMarker.accepted || !keyDownMarker.valid) {4225+ preflightError = new PreviewAutomationKeyboardDeliveryNotConfirmedError({4226+ tabId,4227+ webContentsId: wc.id,4228+ });4229+ return;4230+ }4231+ if (keySequence.char) sendNativeKey(keyDownMarker, keySequence.char);
- @@ -3477,0 +4203,21 @@4203+ wc.hostWebContents !== mainWindow.webContents ||4204+ !isKeyboardDocumentCurrent(attachment, wc, receiptDocument)4205+ ) {4206+ releaseMenuShortcutLease(attachment, wc, menuShortcutLease);4207+ preflightError = new PreviewAutomationTargetChangedError({4208+ operation: "press",4209+ tabId,4210+ webContentsId: wc.id,4211+ });4212+ return;4213+ }4214+ const focusedFrame = wc.focusedFrame;4215+ if (focusedFrame != null && focusedFrame !== wc.mainFrame) {4216+ releaseMenuShortcutLease(attachment, wc, menuShortcutLease);4217+ preflightError = new PreviewAutomationKeyboardFocusedFrameUnsupportedError({4218+ tabId,4219+ webContentsId: wc.id,4220+ });4221+ return;4222+ }4223+ sendNativeKey(keyDownMarker, keySequence.keyDown);
juliusmarminge added labelsmacroscope-review
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
c3f71730-96cf-4a80-940a-fa92162021c0You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.