Skip to content

Commit 15b2a4b

Browse files
fix(desktop): use unique identifier for window focus restoration
When preserving the active element during automation press events, the hardcoded global window property '__t3_restoreFocus' could clobber page-owned properties or cause race conditions during concurrent calls. By dynamically generating a unique 'focusRestoreId' per operation, these risks are eliminated. Fixes #5792
1 parent 1a003e3 commit 15b2a4b

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,6 +3008,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
30083008
() => webContents.getFocusedWebContents(),
30093009
);
30103010
let keyDownAttempted = false;
3011+
const focusRestoreId = `__t3_focus_${Math.random().toString(36).slice(2)}`;
30113012
const releaseInput = Effect.gen(function* () {
30123013
if (keyDownAttempted) {
30133014
yield* sendCleanup("Input.dispatchKeyEvent", keySequence.keyUp).pipe(Effect.ignore);
@@ -3024,6 +3025,17 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
30243025
},
30253026
() => previouslyFocused.focus(),
30263027
).pipe(Effect.ignore);
3028+
yield* attemptPromise(
3029+
{
3030+
operation: "automationPress.restoreActiveElement",
3031+
tabId,
3032+
webContentsId: previouslyFocused.id,
3033+
},
3034+
() =>
3035+
previouslyFocused.executeJavaScript(
3036+
`if (window['${focusRestoreId}'] && typeof window['${focusRestoreId}'].focus === 'function') { window['${focusRestoreId}'].focus(); } delete window['${focusRestoreId}'];`,
3037+
),
3038+
).pipe(Effect.ignore);
30273039
}
30283040
});
30293041

@@ -3032,6 +3044,19 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
30323044
// changing which thread is mounted in the UI. Restore the previous renderer
30333045
// after dispatch so automation never leaves the app's input focus behind.
30343046
yield* Effect.gen(function* () {
3047+
if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
3048+
yield* attemptPromise(
3049+
{
3050+
operation: "automationPress.saveActiveElement",
3051+
tabId,
3052+
webContentsId: previouslyFocused.id,
3053+
},
3054+
() =>
3055+
previouslyFocused.executeJavaScript(
3056+
`if (document.activeElement && document.activeElement !== document.body) { window['${focusRestoreId}'] = document.activeElement; }`,
3057+
),
3058+
).pipe(Effect.ignore);
3059+
}
30353060
yield* attempt(
30363061
{ operation: "automationPress.focusWebContents", tabId, webContentsId: wc.id },
30373062
() => wc.focus(),

0 commit comments

Comments
 (0)