Skip to content

The agent that grows with you

fix(desktop): keep the Linux tray across a minimize toggle

686f6c61d63c6eb87454
diff --git a/apps/desktop/electron/minimize-to-tray.test.ts b/apps/desktop/electron/minimize-to-tray.test.ts
index 138788e294c..d342102823e 100644
--- a/apps/desktop/electron/minimize-to-tray.test.ts
+++ b/apps/desktop/electron/minimize-to-tray.test.ts
@@ -292,6 +292,36 @@ test('opt-in minimize and primary Close preserve windows while explicit Quit sti
expect(native.trays[0].destroyed).toBe(true)
})
+test('linux reuses one tray when minimize-to-tray is toggled off and on (#126353)', async () => {
+ setPlatform('linux')
+
+ try {
+ const { controller, main } = setup()
+ await controller.start()
+ await controller.setEnabled(true)
+ expect(native.trays).toHaveLength(1)
+ main.minimize()
+ await flushDeferredHide()
+ expect(main.visible).toBe(false)
+
+ await controller.setEnabled(false)
+ expect(main.visible).toBe(true)
+ expect(native.trays).toHaveLength(1)
+ expect(native.trays[0].destroyed).toBe(false)
+
+ await controller.setEnabled(true)
+ expect(native.trays).toHaveLength(1)
+ expect(native.trays[0].destroyed).toBe(false)
+ main.minimize()
+ await flushDeferredHide()
+ expect(main.visible).toBe(false)
+ native.trays[0].menu[0].click()
+ expect(main.visible).toBe(true)
+ } finally {
+ restorePlatform()
+ }
+})
+
test('persistence, disabling, failed tray creation, and handoff never strand hidden windows', async () => {
const first = setup()
await first.controller.start()
@@ -299,7 +329,8 @@ test('persistence, disabling, failed tray creation, and handoff never strand hid
first.main.minimize()
await first.controller.setEnabled(false)
expect(first.main.visible).toBe(true)
- expect(native.trays[0].destroyed).toBe(true)
+ // Linux parks the tray so a later enable can reuse it (#126353).
+ expect(native.trays[0].destroyed).toBe(process.platform !== 'linux')
await first.controller.setEnabled(true)
const restarted = setup()
expect(await restarted.controller.start()).toEqual({ enabled: true, available: true })
diff --git a/apps/desktop/electron/minimize-to-tray.ts b/apps/desktop/electron/minimize-to-tray.ts
index 5f4ff3172d7..b7c2424cf36 100644
--- a/apps/desktop/electron/minimize-to-tray.ts
+++ b/apps/desktop/electron/minimize-to-tray.ts
@@ -106,9 +106,18 @@ export function createMinimizeToTray(options: Options) {
options.restoreMainWindow()
}
- const destroyTray = () => {
+ const destroyTray = (force = false) => {
stopWatchingHost?.()
stopWatchingHost = undefined
+
+ // Linux StatusNotifierItem stays exported after Tray.destroy(), so a later
+ // `new Tray()` in this process cannot re-export and the panel icon dies
+ // (#126353). Park the instance until the app actually quits or the host
+ // disappears.
+ if (process.platform === 'linux' && !quitting && !force) {
+ return
+ }
+
tray?.destroy()
tray = null
}
@@ -117,7 +126,7 @@ export function createMinimizeToTray(options: Options) {
// Losing the shell/tray must never strand an invisible app.
hostGeneration += 1
restoreHidden()
- destroyTray()
+ destroyTray(true)
broadcast()
}
@@ -127,6 +136,20 @@ export function createMinimizeToTray(options: Options) {
if (!on) {
restoreHidden()
destroyTray()
+ } else if (!quitting && status().available && process.platform === 'linux' && !stopWatchingHost) {
+ try {
+ const { watchLinuxTrayHost } = await import('./tray-host')
+ const generation = hostGeneration
+ stopWatchingHost = await watchLinuxTrayHost(hostLost)
+
+ if (generation !== hostGeneration) {
+ throw new Error('System tray host disappeared')
+ }
+ } catch (error) {
+ restoreHidden()
+ destroyTray(true)
+ options.log(`[tray] unavailable; ordinary window behavior retained: ${error}`)
+ }
} else if (!status().available && !quitting) {
try {
if (process.platform === 'linux') {
@@ -298,7 +321,10 @@ export function createMinimizeToTray(options: Options) {
ipcMain.handle('hermes:minimize-to-tray:get', status)
ipcMain.handle('hermes:minimize-to-tray:set', (_event, on) => setEnabled(on === true))
- app.on('will-quit', destroyTray)
+ app.on('will-quit', () => {
+ quitting = true
+ destroyTray()
+ })
return {
start,