Skip to content

The agent that grows with you

fix(desktop): reveal main window after missed ready event

Gille48c05e0c6b2d
diff --git a/apps/desktop/electron/main-window-reveal.test.ts b/apps/desktop/electron/main-window-reveal.test.ts
new file mode 100644
index 00000000000..e42768d383c
--- /dev/null
+++ b/apps/desktop/electron/main-window-reveal.test.ts
@@ -0,0 +1,130 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { createMainWindowRevealController } from './main-window-reveal'
+
+function createHarness({ visible = false }: { visible?: boolean } = {}) {
+ let destroyed = false
+ let isVisible = visible
+ let revealCalls = 0
+ let showCalls = 0
+ let scheduledCallback: (() => void) | null = null
+ let scheduledDelay: number | null = null
+ let clearCalls = 0
+
+ const controller = createMainWindowRevealController(
+ {
+ isDestroyed: () => destroyed,
+ isVisible: () => isVisible,
+ show: () => {
+ showCalls += 1
+ isVisible = true
+ }
+ },
+ {
+ onRevealed: () => {
+ revealCalls += 1
+ },
+ setTimer: (callback, delay) => {
+ scheduledCallback = callback
+ scheduledDelay = delay
+
+ return 1 as unknown as ReturnType<typeof setTimeout>
+ },
+ clearTimer: () => {
+ clearCalls += 1
+ }
+ }
+ )
+
+ return {
+ controller,
+ destroy: () => {
+ destroyed = true
+ },
+ get clearCalls() {
+ return clearCalls
+ },
+ get revealCalls() {
+ return revealCalls
+ },
+ get scheduledCallback() {
+ return scheduledCallback
+ },
+ get scheduledDelay() {
+ return scheduledDelay
+ },
+ get showCalls() {
+ return showCalls
+ }
+ }
+}
+
+test('reveals immediately when Electron emits ready-to-show', () => {
+ const harness = createHarness()
+
+ assert.equal(harness.controller.reveal(), true)
+ assert.equal(harness.showCalls, 1)
+ assert.equal(harness.revealCalls, 1)
+ assert.equal(harness.scheduledCallback, null)
+})
+
+test('reveals through the fallback when ready-to-show never arrives', () => {
+ const harness = createHarness()
+
+ harness.controller.scheduleFallback()
+
+ assert.equal(harness.scheduledDelay, 4_000)
+ assert.ok(harness.scheduledCallback)
+ harness.scheduledCallback()
+
+ assert.equal(harness.showCalls, 1)
+ assert.equal(harness.revealCalls, 1)
+})
+
+test('ready-to-show cancels the fallback and settles only once', () => {
+ const harness = createHarness()
+
+ harness.controller.scheduleFallback()
+ const scheduledCallback = harness.scheduledCallback
+
+ assert.equal(harness.controller.reveal(), true)
+ assert.equal(harness.clearCalls, 1)
+ scheduledCallback?.()
+
+ assert.equal(harness.showCalls, 1)
+ assert.equal(harness.revealCalls, 1)
+ assert.equal(harness.controller.reveal(), false)
+})
+
+test('finalizes an already visible window without showing it again', () => {
+ const harness = createHarness({ visible: true })
+
+ assert.equal(harness.controller.reveal(), true)
+ assert.equal(harness.showCalls, 0)
+ assert.equal(harness.revealCalls, 1)
+})
+
+test('dispose cancels a pending fallback and prevents a late reveal', () => {
+ const harness = createHarness()
+
+ harness.controller.scheduleFallback()
+ const scheduledCallback = harness.scheduledCallback
+ harness.controller.dispose()
+ scheduledCallback?.()
+
+ assert.equal(harness.clearCalls, 1)
+ assert.equal(harness.showCalls, 0)
+ assert.equal(harness.revealCalls, 0)
+})
+
+test('does not reveal a destroyed window', () => {
+ const harness = createHarness()
+
+ harness.destroy()
+
+ assert.equal(harness.controller.reveal(), false)
+ harness.controller.scheduleFallback()
+ assert.equal(harness.scheduledCallback, null)
+})
diff --git a/apps/desktop/electron/main-window-reveal.ts b/apps/desktop/electron/main-window-reveal.ts
new file mode 100644
index 00000000000..40206ea0177
--- /dev/null
+++ b/apps/desktop/electron/main-window-reveal.ts
@@ -0,0 +1,78 @@
+type MainWindowRevealTarget = {
+ isDestroyed: () => boolean
+ isVisible: () => boolean
+ show: () => void
+}
+
+type TimerHandle = ReturnType<typeof setTimeout>
+
+type MainWindowRevealOptions = {
+ onRevealed: () => void
+ delayMs?: number
+ setTimer?: (callback: () => void, delayMs: number) => TimerHandle
+ clearTimer?: (timer: TimerHandle) => void
+}
+
+export const MAIN_WINDOW_REVEAL_FALLBACK_MS = 4_000
+
+export function createMainWindowRevealController(
+ window: MainWindowRevealTarget,
+ {
+ onRevealed,
+ delayMs = MAIN_WINDOW_REVEAL_FALLBACK_MS,
+ setTimer = (callback, delay) => setTimeout(callback, delay),
+ clearTimer = timer => clearTimeout(timer)
+ }: MainWindowRevealOptions
+) {
+ let disposed = false
+ let revealed = false
+ let fallbackTimer: TimerHandle | null = null
+
+ const cancelFallback = () => {
+ if (fallbackTimer === null) {
+ return
+ }
+
+ clearTimer(fallbackTimer)
+ fallbackTimer = null
+ }
+
+ const reveal = () => {
+ if (disposed || revealed || window.isDestroyed()) {
+ return false
+ }
+
+ revealed = true
+ cancelFallback()
+
+ if (!window.isVisible()) {
+ window.show()
+ }
+
+ onRevealed()
+
+ return true
+ }
+
+ const scheduleFallback = () => {
+ if (disposed || revealed || fallbackTimer !== null || window.isDestroyed()) {
+ return
+ }
+
+ fallbackTimer = setTimer(() => {
+ fallbackTimer = null
+ reveal()
+ }, delayMs)
+ }
+
+ const dispose = () => {
+ disposed = true
+ cancelFallback()
+ }
+
+ return {
+ dispose,
+ reveal,
+ scheduleFallback
+ }
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 95ffeeed05f..1384a2e1fc1 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -136,6 +136,7 @@ import {
} from './hardening'
import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window'
import { ensureMainWindow } from './main-window-lifecycle'
+import { createMainWindowRevealController } from './main-window-reveal'
import {
oauthGuardMayHardFail,
oauthSessionIsLive,
@@ -9556,6 +9557,8 @@ function createWindow() {
webPreferences: chatWindowWebPreferences(PRELOAD_PATH)
})
+ const createdMainWindow = mainWindow
+
if (IS_MAC) {
mainWindow.setWindowButtonPosition?.(WINDOW_BUTTON_POSITION)
@@ -9579,42 +9582,34 @@ function createWindow() {
mainWindow.maximize()
}
- mainWindow.once('ready-to-show', () => {
- if (mainWindow && !mainWindow.isDestroyed()) {
- mainWindow.show()
- }
+ const revealController = createMainWindowRevealController(createdMainWindow, {
+ onRevealed: () => {
+ // Persist geometry as soon as the window is visible so a crash before the
+ // first clean resize/move/close still captures the restored bounds (#56726).
+ schedulePersistWindowState()
- // Persist geometry as soon as the window is visible so a crash before the
- // first clean resize/move/close still captures the restored bounds (#56726).
- schedulePersistWindowState()
-
- // #38216: clear the mid-boot marker only after a window is actually usable.
- // Keep sticky `fallback` when we launched with --no-sandbox so the next
- // Start Menu click does not re-enter the GPU FATAL crash loop. The marker
- // records the app version so the next update re-probes the sandbox.
- if (IS_WINDOWS) {
- try {
- writeSandboxMarker(
- app.getPath('userData'),
- markerAfterSuccessfulBoot({
- fallbackActive: windowsSandboxFallbackSticky,
- reason: windowsSandboxFallbackReason,
- appVersion: app.getVersion()
- })
- )
- } catch (error) {
- rememberLog(`[sandbox] marker update after ready-to-show failed: ${error?.message || error}`)
+ // #38216: clear the mid-boot marker only after a window is actually usable.
+ // Keep sticky `fallback` when we launched with --no-sandbox so the next
+ // Start Menu click does not re-enter the GPU FATAL crash loop. The marker
+ // records the app version so the next update re-probes the sandbox.
+ if (IS_WINDOWS) {
+ try {
+ writeSandboxMarker(
+ app.getPath('userData'),
+ markerAfterSuccessfulBoot({
+ fallbackActive: windowsSandboxFallbackSticky,
+ reason: windowsSandboxFallbackReason,
+ appVersion: app.getVersion()
+ })
+ )
+ } catch (error) {
+ rememberLog(`[sandbox] marker update after main-window reveal failed: ${error?.message || error}`)
+ }
}
}
})
- // Under Playright testing, instantly show the window.
- // `ready-to-show` doesn't fire in some testing envs.
- if (process.env.TEST_WORKER_INDEX !== undefined) {
- if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.isVisible()) {
- mainWindow.show()
- }
- }
+ mainWindow.once('ready-to-show', revealController.reveal)
mainWindow.on('will-enter-full-screen', () => sendWindowStateChanged(true))
mainWindow.on('enter-full-screen', () => sendWindowStateChanged(true))
@@ -9634,8 +9629,8 @@ function createWindow() {
mainWindow.on('close', () => schedulePersistWindowState.flush())
// the closed wrapper remains truthy, so clear only the window this callback owns.
- const createdMainWindow = mainWindow
mainWindow.on('closed', () => {
+ revealController.dispose()
closePetOverlay()
wakeIndicatorController.close()
@@ -9745,6 +9740,11 @@ function createWindow() {
startHermes().catch(error => rememberLog(error.stack || error.message))
mainWindow.webContents.once('did-finish-load', () => {
+ // Electron 40 can omit ready-to-show even after the renderer has loaded,
+ // leaving the primary window hidden indefinitely. Give the normal themed
+ // paint path four seconds to win, then reveal the same window as a fallback.
+ revealController.scheduleFallback()
+
// Zoom restore is handled by wireCommonWindowHandlers (shared with session
// windows); no need to reapply it here.
broadcastBootProgress()