fix: keep tray menu alive

Fixes #1813
This commit is contained in:
zjdndjf
2026-05-12 03:45:02 +08:00
parent 9041c74d1a
commit b000a76a3f

View File

@@ -41,6 +41,7 @@ import { trayLogger } from '../utils/logger'
import { floatingWindow, triggerFloatingWindow } from './floatingWindow' import { floatingWindow, triggerFloatingWindow } from './floatingWindow'
export let tray: Tray | null = null export let tray: Tray | null = null
let trayMenu: Menu | null = null
// macOS 流量显示状态,避免异步读取配置导致的时序问题 // macOS 流量显示状态,避免异步读取配置导致的时序问题
let macTrafficIconEnabled = false let macTrafficIconEnabled = false
type TrayIconStatus = 'white' | 'blue' | 'green' | 'red' type TrayIconStatus = 'white' | 'blue' | 'green' | 'red'
@@ -399,8 +400,8 @@ export async function createTray(): Promise<void> {
const { useDockIcon = true, swapTrayClick = false } = await getAppConfig() const { useDockIcon = true, swapTrayClick = false } = await getAppConfig()
if (process.platform === 'linux') { if (process.platform === 'linux') {
tray = new Tray(pngIcon) tray = new Tray(pngIcon)
const menu = await buildContextMenu() trayMenu = await buildContextMenu()
tray.setContextMenu(menu) tray.setContextMenu(trayMenu)
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
const icon = nativeImage.createFromPath(templateIcon).resize({ height: 16 }) const icon = nativeImage.createFromPath(templateIcon).resize({ height: 16 })
@@ -484,10 +485,10 @@ export async function createTray(): Promise<void> {
} }
async function updateTrayMenu(): Promise<void> { async function updateTrayMenu(): Promise<void> {
const menu = await buildContextMenu() trayMenu = await buildContextMenu()
tray?.popUpContextMenu(menu) // 弹出菜单 tray?.popUpContextMenu(trayMenu) // 弹出菜单
if (process.platform === 'linux') { if (process.platform === 'linux') {
tray?.setContextMenu(menu) tray?.setContextMenu(trayMenu)
} }
} }
@@ -540,6 +541,7 @@ export async function closeTrayIcon(): Promise<void> {
tray.destroy() tray.destroy()
} }
tray = null tray = null
trayMenu = null
} }
export async function showDockIcon(): Promise<void> { export async function showDockIcon(): Promise<void> {