@@ -1857,7 +1857,9 @@ export function focusPageForUser(contents: WebContents): void {
18571857function applyPendingUserFocus ( view : WebContentsView ) : void {
18581858 const tab = activeTab ( )
18591859 if ( ! tab ?. pendingUserFocus || tab . view !== view || view . webContents . isDestroyed ( ) ) return
1860- if ( ! isPanelVisible ( ) || getBrowserScopeId ( ) !== getActiveBrowserScopeId ( ) ) return
1860+ // A renderer modal can hide the view while the panel keeps its bounds.
1861+ if ( ! view . getVisible ( ) || ! isPanelVisible ( ) ) return
1862+ if ( getBrowserScopeId ( ) !== getActiveBrowserScopeId ( ) ) return
18611863 tab . pendingUserFocus = false
18621864 view . webContents . focus ( )
18631865}
@@ -1880,8 +1882,9 @@ function publishPageIssue(tab: AgentTab, focusRecovery = false): void {
18801882 // Recovery moves focus to the renderer's issue page only when the failed page
18811883 // held it; the user typing in chat keeps their caret.
18821884 const pageHadFocus =
1883- currentScope . focusedBrowserTabId === tab . id ||
1884- ( ! tab . view . webContents . isDestroyed ( ) && tab . view . webContents . isFocused ( ) )
1885+ ! currentScope . browserChromeFocused &&
1886+ ( currentScope . focusedBrowserTabId === tab . id ||
1887+ ( ! tab . view . webContents . isDestroyed ( ) && tab . view . webContents . isFocused ( ) ) )
18851888 if (
18861889 focusRecovery &&
18871890 pageHadFocus &&
@@ -2307,15 +2310,31 @@ function initializeTabView(
23072310 const tab = tabs . find ( ( entry ) => entry . view . webContents === contents )
23082311 // A tab the user cannot see never keeps keyboard focus. Chromium focuses
23092312 // a page opened without an opener (a target=_blank link) while creating
2310- // it, before the tab is even listed. Hand focus back once that focus
2311- // call has returned, or Chromium finishes it over the top.
2313+ // it, before the tab is even listed. Hand focus back to the visible page
2314+ // the user was in, or else to Sim, once that focus call has returned, or
2315+ // Chromium finishes it over the top.
23122316 if ( ! tab || tab . id !== currentScope . activeTabId ) {
2317+ const active = activeTab ( )
2318+ const returnTo =
2319+ active &&
2320+ ! currentScope . browserChromeFocused &&
2321+ currentScope . focusedBrowserTabId === active . id
2322+ ? active
2323+ : null
23132324 setImmediate (
23142325 bindToBrowserScope ( scopeId , ( ) => {
23152326 const current = tabs . find ( ( entry ) => entry . view . webContents === contents )
23162327 const win = panelWindow ( )
23172328 if ( current ?. id === currentScope . activeTabId || ! win || win . isDestroyed ( ) ) return
2318- if ( ! contents . isDestroyed ( ) && contents . isFocused ( ) ) win . webContents . focus ( )
2329+ if ( contents . isDestroyed ( ) || ! contents . isFocused ( ) ) return
2330+ if (
2331+ returnTo ?. id === currentScope . activeTabId &&
2332+ ! returnTo . view . webContents . isDestroyed ( )
2333+ ) {
2334+ returnTo . view . webContents . focus ( )
2335+ } else {
2336+ win . webContents . focus ( )
2337+ }
23192338 } )
23202339 )
23212340 return
0 commit comments