hermes-agent/apps/desktop/electron/hud-close.ts
Teknium 8092f0e8ee fix(desktop): leaving HUD mode always gives the app window back (macOS)
Symptom (Discord/X report, macOS): exit HUD mode and the small HUD window
stays on screen while the main window never regains focus; the toggle now
reads the wrong state, so there is no in-app way out.

Three defects in the HUD lifecycle, all in the main process:

- closeHudWindow() detached the window's 'closed' listener before calling
  win.close(). close() is a REQUEST that waits on the renderer's unload
  handlers, so a HUD renderer that never answered left an always-on-top
  window with no teardown owner and no close broadcast. The 'closed'
  handler now stays attached and is the single teardown path; a bounded
  requestHudClose() (electron/hud-close.ts) destroys the window after a
  1.5 s grace if the renderer never lets go.

- ⌘W inside the HUD was routed to the MAIN renderer's close-active-tab
  (installPreviewShortcut is wired on every chat window), so it closed a
  tab in the hidden app window and left the HUD up. ⌘W in the HUD now
  runs closeHudWindow(), the same path as the exit button.

- restoreMainWindowFromHud() was a bare show() and only armed when the
  main window was isVisible() at HUD-open time. On macOS isVisible() is
  false for an occluded window (Electron folds occlusionState in), so
  the app never came back when the HUD was opened over another app, and
  show() alone does not make the window key. The restore now arms on a
  live main window and routes through focusWindow() (restore → show →
  focus). Closes #88513 (credit: PR #90489's diagnosis).

Renderer: the handoff now also requests composer focus on the close
broadcast, so the app window gets the keyboard, not just the session.

Docs: ⌘/Ctrl+W added to the HUD exit list.
2026-09-10 08:19:13 -07:00

53 lines
1.7 KiB
TypeScript

/**
* Bounded graceful close for the HUD window.
*
* `win.close()` is a request, not an action: Electron asks the renderer to run
* its beforeunload/unload handlers and only destroys the window once it
* answers. That round-trip is what lets the HUD flush a half-typed draft into
* the shared stash the app window re-reads on handoff — but a renderer that is
* hung never answers, and an always-on-top window that never closes is exactly
* the "HUD won't go away" report. Electron's own escalation is a 5 s
* `unresponsive` event nobody acts on.
*
* So: ask nicely, then take the window away if the renderer has not let go by
* the grace deadline. Pure and timer-injected so the deadline is testable.
*/
export interface HudCloseWindowLike {
close(): void
destroy(): void
isDestroyed(): boolean
once(event: 'closed', listener: () => void): unknown
}
interface HudCloseTimers {
setTimer?: (fn: () => void, ms: number) => unknown
clearTimer?: (handle: unknown) => void
}
/** How long a HUD renderer gets to answer the close before it is destroyed.
* A healthy beforeunload round-trip is tens of milliseconds; a transcript
* flush on a slow machine still fits comfortably. */
export const HUD_CLOSE_GRACE_MS = 1500
export function requestHudClose(
win: HudCloseWindowLike,
{
setTimer = (fn, ms) => setTimeout(fn, ms),
clearTimer = handle => clearTimeout(handle as ReturnType<typeof setTimeout>)
}: HudCloseTimers = {},
graceMs: number = HUD_CLOSE_GRACE_MS
): void {
if (win.isDestroyed()) {
return
}
const deadline = setTimer(() => {
if (!win.isDestroyed()) {
win.destroy()
}
}, graceMs)
win.once('closed', () => clearTimer(deadline))
win.close()
}