import { app, Menu, Tray } from "electron"; import { join } from "node:path"; import { DesktopCommandIds, desktopMenuMessageIds, getDesktopMenuMessage, ZCODE_PRODUCT_FLAVOR, type DesktopCommandId, type Locale, } from "@zcode/shared"; let desktopTray: Tray | null = null; let rebuildDesktopTrayContextMenu: (() => void) | null = null; export function resolveDesktopTrayIconPath(platform: NodeJS.Platform = process.platform) { // Linux 托盘只接受 PNG(.ico 不会渲染);打包态 icon.png 已由 extraResources 无条件放入 // resources 目录(electron-builder.config.js),Windows 继续用独立托盘 .ico 避免高 DPI 模糊。 if (platform === "linux") { return app.isPackaged ? join(process.resourcesPath, "icon.png") : join(import.meta.dirname, "../../build/icon.png"); } return app.isPackaged ? join(process.resourcesPath, "tray_icon.ico") : join(import.meta.dirname, "../../build/icon.ico"); } export function createDesktopTray(options: { platform?: NodeJS.Platform; getLocale: () => Locale; showCurrentWindow: () => Promise | void; executeDesktopCommand: (command: DesktopCommandId) => Promise; quitApp: () => void; logger: { warn: (...args: unknown[]) => void }; }) { const platform = options.platform ?? process.platform; // Linux 驻留能力由 createCloseToTrayCapabilityMonitor 探测决定(spec: // docs/desktop/linux-close-to-tray.md);这里只挡平台,能力不过关时调用方不应传入 linux。 if (platform !== "win32" && platform !== "linux") { return null; } if (desktopTray) { return desktopTray; } try { desktopTray = new Tray(resolveDesktopTrayIconPath(platform)); } catch (error) { options.logger.warn("[desktop-tray] failed to create tray icon", error); return null; } const getLabel = (id: (typeof desktopMenuMessageIds)[keyof typeof desktopMenuMessageIds]) => getDesktopMenuMessage(options.getLocale(), id); const showTrayWindow = () => { void Promise.resolve(options.showCurrentWindow()).catch((error) => { options.logger.warn("[desktop-tray] failed to show current window", error); }); }; const executeTrayCommand = (command: DesktopCommandId) => { void Promise.resolve(options.showCurrentWindow()) .then(() => options.executeDesktopCommand(command)) .catch((error) => { options.logger.warn(`[desktop-tray] failed to execute tray command ${command}`, error); }); }; const rebuildContextMenu = () => { desktopTray?.setToolTip(getLabel(desktopMenuMessageIds.trayTooltip)); desktopTray?.setContextMenu( Menu.buildFromTemplate([ { label: getLabel(desktopMenuMessageIds.trayOpenZCode), click: showTrayWindow, }, { type: "separator" }, { label: getLabel(desktopMenuMessageIds.fileNewTask), click: () => executeTrayCommand(DesktopCommandIds.NewTask), }, { label: getLabel(desktopMenuMessageIds.fileOpenWorkspace), click: () => executeTrayCommand(DesktopCommandIds.OpenWorkspace), }, { type: "separator" }, // 更新入口跟随产品身份:Preview(含生产后端的 Preview)禁用更新器,托盘也不能露出入口。 ...(ZCODE_PRODUCT_FLAVOR === "production" ? [ { label: getLabel(desktopMenuMessageIds.helpCheckForUpdates), click: () => executeTrayCommand(DesktopCommandIds.CheckForUpdates), }, ] : []), { label: getLabel(desktopMenuMessageIds.helpAbout), click: () => executeTrayCommand(DesktopCommandIds.ShowAbout), }, { // 托盘只提供日志诊断入口,避免右键菜单暴露清除全部数据操作;复用应用菜单导出命令。 label: getLabel(desktopMenuMessageIds.helpExportLogs), click: () => executeTrayCommand(DesktopCommandIds.ExportLogs), }, { type: "separator" }, { label: getLabel(desktopMenuMessageIds.trayQuit), click: () => options.quitApp(), }, ]), ); }; rebuildDesktopTrayContextMenu = rebuildContextMenu; desktopTray.on("click", showTrayWindow); desktopTray.on("double-click", showTrayWindow); rebuildContextMenu(); return desktopTray; } export function updateWindowsDesktopTrayMenu() { rebuildDesktopTrayContextMenu?.(); }