Skip to content

The agent that grows with you

fix(desktop): release keyboard focus when hiding to the tray on Windows

Brooklyn Nicholson49b17a04017f
diff --git a/apps/desktop/electron/minimize-to-tray.test.ts b/apps/desktop/electron/minimize-to-tray.test.ts
index 90bd7d735a2..138788e294c 100644
--- a/apps/desktop/electron/minimize-to-tray.test.ts
+++ b/apps/desktop/electron/minimize-to-tray.test.ts
@@ -67,9 +67,13 @@ class Window extends EventEmitter {
setSkipTaskbar(on: boolean) {
this.skipped = on
}
+ blur() {
+ this.focused = false
+ }
hide() {
+ // A real Win32 window does NOT release keyboard focus by being hidden
+ // (#126570); only an explicit blur does.
this.visible = false
- this.focused = false
this.emit('hide')
}
show() {
@@ -191,6 +195,31 @@ test('Windows restore re-activates the window instead of showInactive (#119252)'
}
})
+test('Windows tray hide releases keyboard focus before hiding (#126570)', async () => {
+ setPlatform('win32')
+
+ try {
+ const { controller, main } = setup()
+ await controller.start()
+ await controller.setEnabled(true)
+
+ // The close-to-tray path hides a focused window; a real hidden Win32
+ // window keeps the UI thread's keyboard focus (the fake above models
+ // that: hide does not touch focus), so the shared hide path must blur
+ // explicitly, before the hide itself -- after it, it no longer takes.
+ main.focused = true
+ const blur = vi.spyOn(main, 'blur')
+ const hide = vi.spyOn(main, 'hide')
+ main.close()
+ expect(main.visible).toBe(false)
+ expect(main.focused).toBe(false)
+ expect(blur).toHaveBeenCalledOnce()
+ expect(blur.mock.invocationCallOrder[0]).toBeLessThan(hide.mock.invocationCallOrder[0])
+ } finally {
+ restorePlatform()
+ }
+})
+
test('a restore before the deferred hide fires cancels the stale hide', async () => {
const { controller, main } = setup()
await controller.start()
diff --git a/apps/desktop/electron/minimize-to-tray.ts b/apps/desktop/electron/minimize-to-tray.ts
index 2218b0b97b6..5f4ff3172d7 100644
--- a/apps/desktop/electron/minimize-to-tray.ts
+++ b/apps/desktop/electron/minimize-to-tray.ts
@@ -202,6 +202,11 @@ export function createMinimizeToTray(options: Options) {
if (process.platform === 'win32') {
win.setSkipTaskbar(true)
+ // A hidden Chromium window on Windows neither emits blur nor releases
+ // the UI thread's keyboard focus: keys keep going to the invisible
+ // page, trapping keyboard navigation and screen readers in it. Release
+ // focus before hiding -- once hidden it no longer takes (#126570).
+ win.blur()
}
win.hide()