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