Skip to content

The agent that grows with you

refactor(desktop): route deep-link activation through activateWindow

kshitijk4poore473b72df286
diff --git a/apps/desktop/electron/main-window-lifecycle.test.ts b/apps/desktop/electron/main-window-lifecycle.test.ts
index b16d1a22424..329ba0c4046 100644
--- a/apps/desktop/electron/main-window-lifecycle.test.ts
+++ b/apps/desktop/electron/main-window-lifecycle.test.ts
@@ -108,76 +108,18 @@ test('explicit relaunch activates a minimized tray-hidden window', () => {
// down. It must not consult the overlay-suppression latch, must stay quiet
// on macOS, during handoff, with peers left, or mid-quit.
test('last-chat fallback quits only for a final non-macOS close outside a quit', () => {
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'win32',
- isQuittingForHandoff: false,
- remainingChatWindows: 0,
- quitInProgress: false
- }),
- true
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'linux',
- isQuittingForHandoff: false,
- remainingChatWindows: 0,
- quitInProgress: false
- }),
- true
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'darwin',
- isQuittingForHandoff: false,
- remainingChatWindows: 0,
- quitInProgress: false
- }),
- false
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'win32',
- isQuittingForHandoff: true,
- remainingChatWindows: 0,
- quitInProgress: false
- }),
- false
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'win32',
- isQuittingForHandoff: false,
- remainingChatWindows: 1,
- quitInProgress: false
- }),
- false
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'linux',
- isQuittingForHandoff: false,
- remainingChatWindows: 2,
- quitInProgress: false
- }),
- false
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'win32',
- isQuittingForHandoff: false,
- remainingChatWindows: 0,
- quitInProgress: true
- }),
- false
- )
- assert.equal(
- shouldQuitOnLastChatClosed({
- platform: 'linux',
- isQuittingForHandoff: false,
- remainingChatWindows: 0,
- quitInProgress: true
- }),
- false
- )
+ const cases: [string, boolean, number, boolean, boolean][] = [
+ // platform, isQuittingForHandoff, remainingChatWindows, quitInProgress, expected
+ ['win32', false, 0, false, true],
+ ['linux', false, 0, false, true],
+ ['darwin', false, 0, false, false],
+ ['win32', true, 0, false, false],
+ ['win32', false, 1, false, false],
+ ['win32', false, 0, true, false]
+ ]
+
+ for (const [platform, isQuittingForHandoff, remainingChatWindows, quitInProgress, expected] of cases) {
+ const input = { platform, isQuittingForHandoff, remainingChatWindows, quitInProgress }
+ assert.equal(shouldQuitOnLastChatClosed(input), expected, JSON.stringify(input))
+ }
})
diff --git a/apps/desktop/electron/main-window-lifecycle.ts b/apps/desktop/electron/main-window-lifecycle.ts
index 40051cf57c8..813ece61237 100644
--- a/apps/desktop/electron/main-window-lifecycle.ts
+++ b/apps/desktop/electron/main-window-lifecycle.ts
@@ -1,3 +1,5 @@
+import { shouldFocusToTakeKeyboard } from './window-focus-policy'
+
type MainWindowLike = {
isDestroyed: () => boolean
}
@@ -47,7 +49,8 @@ export function ensureMainWindow<T extends MainWindowLike>(
* `appQuitting = true` in its `close` handler (for #55920 pop-in
* suppression) before `closed` fires, so gating on that latch would suppress
* the very quit the fallback exists for and strand a windowless
- * single-instance lock holder.
+ * single-instance lock holder. A tray-absorbed close never reaches the
+ * fallback (preventDefault) and a multi-window close still has peers left.
*/
export function shouldQuitOnLastChatClosed({
platform,
@@ -60,19 +63,7 @@ export function shouldQuitOnLastChatClosed({
remainingChatWindows: number
quitInProgress: boolean
}): boolean {
- if (platform === 'darwin') {
- return false
- }
-
- if (isQuittingForHandoff) {
- return false
- }
-
- if (quitInProgress) {
- return false
- }
-
- return remainingChatWindows === 0
+ return platform !== 'darwin' && !isQuittingForHandoff && !quitInProgress && remainingChatWindows === 0
}
/**
@@ -102,7 +93,7 @@ export function activateWindow(window: ActivatableWindow | null | undefined): vo
window.show()
}
- if (!window.isFocused()) {
+ if (shouldFocusToTakeKeyboard(window)) {
window.focus()
}
}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index d7c8b779a2e..f9b87e105c4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -360,11 +360,7 @@ import { localSkinProfileKey, readLocalSkinPayload } from './local-skin'
import { ACTIVE_LOG_POLL_MS, planLogRotation, reclaimActiveLogIfOversized } from './log-rotation'
import { registerMachineProfile } from './machine-profile'
import { createMainProcessLagWatchdog } from './main-process-lag-watchdog'
-import {
- activateWindow as activateRestoredWindow,
- ensureMainWindow,
- shouldQuitOnLastChatClosed
-} from './main-window-lifecycle'
+import { activateWindow, ensureMainWindow, shouldQuitOnLastChatClosed } from './main-window-lifecycle'
import {
assertManagedUpdatePreflightClear,
executeManagedRemoteUpdate,
@@ -13788,7 +13784,7 @@ const minimizeToTray = createMinimizeToTray({
// A tray click is an explicit relaunch gesture (#130810): restore with
// activation (show + focus), not the ambient showInactive path.
restoreMainWindow: () =>
- ensureMainWindow(mainWindow, { isReady: app.isReady(), createWindow, focusWindow: activateRestoredWindow }),
+ ensureMainWindow(mainWindow, { isReady: app.isReady(), createWindow, focusWindow: activateWindow }),
isQuittingForHandoff: () => isQuittingForHandoff,
log: rememberLog
})
@@ -14136,14 +14132,7 @@ let petOverlayWindow = null
// persisted popped-out flag survives for the next boot's restorePetOverlay
// (#55920).
let appQuitting = false
-// Whether a quit is actually in progress (#130810). Set in before-quit only
-// once the active-work guard has passed (quit is confirmed/proceeding, not
-// held for confirmation), so the last-chat `closed` fallback can tell "the
-// user closed the last chat surface, quit now" apart from "windows are
-// closing because a quit is already tearing down". Deliberately distinct from
-// appQuitting above, which is also set by the primary window's `close`
-// handler for overlay pop-in suppression and would otherwise suppress the
-// fallback on every ordinary close.
+// Real quit proceeding; see shouldQuitOnLastChatClosed (#130810).
let quitInProgress = false
// Set while a close is in flight: Electron's close() is async and can be
// aborted on macOS, so the window may still be alive after closePetOverlay().
@@ -19277,24 +19266,8 @@ function handleDeepLink(url) {
}
try {
- if (mainWindow.isMinimized()) {
- mainWindow.restore()
- }
-
- // #130810: same tray-hidden case as above — a second-instance deep link
- // must un-hide the window before delivering, otherwise the payload goes
- // to an invisible renderer and the relaunch looks like a silent exit.
- // Showing emits `show`, so minimize-to-tray releases the window from its
- // hidden set (and clears skipTaskbar on Windows).
- if (!mainWindow.isVisible()) {
- mainWindow.show()
- }
-
- // #83998: a deep link must deliver without re-pumping the Windows
- // foreground when the window already has focus.
- if (shouldFocusToTakeKeyboard(mainWindow)) {
- mainWindow.focus()
- }
+ // #130810 un-hide a tray-hidden window; #83998 no foreground re-pump.
+ activateWindow(mainWindow)
mainWindow.webContents.send('hermes:deep-link', payload)
rememberLog(`[deeplink] delivered ${kind}/${name}`)
@@ -19401,7 +19374,7 @@ if (!isPrimaryInstance) {
ensureMainWindow(mainWindow, {
isReady: app.isReady(),
createWindow,
- focusWindow: activateRestoredWindow,
+ focusWindow: activateWindow,
// deep-link delivery focuses a live window after its renderer is ready.
focusExisting: !url
})
@@ -19559,7 +19532,7 @@ app.whenReady().then(() => {
if (!mainWindow || mainWindow.isDestroyed()) {
createWindow()
} else {
- activateRestoredWindow(mainWindow)
+ activateWindow(mainWindow)
}
})
})
@@ -19711,16 +19684,7 @@ function registerChatWindow(window: BrowserWindow) {
window.once('closed', () => {
chatWindows.delete(window)
- // #130810: hidden helpers (Quick Entry, HUD, pet overlay) are still
- // BrowserWindows, so `window-all-closed` never fires while one lingers
- // and a windowless app keeps holding the single-instance lock — every
- // later launch then exits silently. A tray-absorbed close never reaches
- // here (preventDefault), and a multi-window close still has peers left,
- // so quitting only when no chat surface remains is safe: it forces the
- // ordinary before-quit teardown (backends, SSH, PTYs, watchers) even when
- // window-all-closed is blocked. Keyed on quitInProgress, not appQuitting:
- // an ordinary primary-window close sets appQuitting for overlay pop-in
- // suppression before `closed` fires, which must not suppress this quit.
+ // See shouldQuitOnLastChatClosed (#130810).
// Popped-out Browser windows (browserWindows registry) are user-visible
// surfaces too, so they keep the app alive.
if (