desktopTray.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { app, Menu, Tray } from "electron";
  2. import { join } from "node:path";
  3. import {
  4. DesktopCommandIds,
  5. desktopMenuMessageIds,
  6. getDesktopMenuMessage,
  7. ZCODE_PRODUCT_FLAVOR,
  8. type DesktopCommandId,
  9. type Locale,
  10. } from "@zcode/shared";
  11. let desktopTray: Tray | null = null;
  12. let rebuildDesktopTrayContextMenu: (() => void) | null = null;
  13. export function resolveDesktopTrayIconPath(platform: NodeJS.Platform = process.platform) {
  14. // Linux 托盘只接受 PNG(.ico 不会渲染);打包态 icon.png 已由 extraResources 无条件放入
  15. // resources 目录(electron-builder.config.js),Windows 继续用独立托盘 .ico 避免高 DPI 模糊。
  16. if (platform === "linux") {
  17. return app.isPackaged
  18. ? join(process.resourcesPath, "icon.png")
  19. : join(import.meta.dirname, "../../build/icon.png");
  20. }
  21. return app.isPackaged
  22. ? join(process.resourcesPath, "tray_icon.ico")
  23. : join(import.meta.dirname, "../../build/icon.ico");
  24. }
  25. export function createDesktopTray(options: {
  26. platform?: NodeJS.Platform;
  27. getLocale: () => Locale;
  28. showCurrentWindow: () => Promise<void> | void;
  29. executeDesktopCommand: (command: DesktopCommandId) => Promise<unknown>;
  30. quitApp: () => void;
  31. logger: { warn: (...args: unknown[]) => void };
  32. }) {
  33. const platform = options.platform ?? process.platform;
  34. // Linux 驻留能力由 createCloseToTrayCapabilityMonitor 探测决定(spec:
  35. // docs/desktop/linux-close-to-tray.md);这里只挡平台,能力不过关时调用方不应传入 linux。
  36. if (platform !== "win32" && platform !== "linux") {
  37. return null;
  38. }
  39. if (desktopTray) {
  40. return desktopTray;
  41. }
  42. try {
  43. desktopTray = new Tray(resolveDesktopTrayIconPath(platform));
  44. } catch (error) {
  45. options.logger.warn("[desktop-tray] failed to create tray icon", error);
  46. return null;
  47. }
  48. const getLabel = (id: (typeof desktopMenuMessageIds)[keyof typeof desktopMenuMessageIds]) =>
  49. getDesktopMenuMessage(options.getLocale(), id);
  50. const showTrayWindow = () => {
  51. void Promise.resolve(options.showCurrentWindow()).catch((error) => {
  52. options.logger.warn("[desktop-tray] failed to show current window", error);
  53. });
  54. };
  55. const executeTrayCommand = (command: DesktopCommandId) => {
  56. void Promise.resolve(options.showCurrentWindow())
  57. .then(() => options.executeDesktopCommand(command))
  58. .catch((error) => {
  59. options.logger.warn(`[desktop-tray] failed to execute tray command ${command}`, error);
  60. });
  61. };
  62. const rebuildContextMenu = () => {
  63. desktopTray?.setToolTip(getLabel(desktopMenuMessageIds.trayTooltip));
  64. desktopTray?.setContextMenu(
  65. Menu.buildFromTemplate([
  66. {
  67. label: getLabel(desktopMenuMessageIds.trayOpenZCode),
  68. click: showTrayWindow,
  69. },
  70. { type: "separator" },
  71. {
  72. label: getLabel(desktopMenuMessageIds.fileNewTask),
  73. click: () => executeTrayCommand(DesktopCommandIds.NewTask),
  74. },
  75. {
  76. label: getLabel(desktopMenuMessageIds.fileOpenWorkspace),
  77. click: () => executeTrayCommand(DesktopCommandIds.OpenWorkspace),
  78. },
  79. { type: "separator" },
  80. // 更新入口跟随产品身份:Preview(含生产后端的 Preview)禁用更新器,托盘也不能露出入口。
  81. ...(ZCODE_PRODUCT_FLAVOR === "production"
  82. ? [
  83. {
  84. label: getLabel(desktopMenuMessageIds.helpCheckForUpdates),
  85. click: () => executeTrayCommand(DesktopCommandIds.CheckForUpdates),
  86. },
  87. ]
  88. : []),
  89. {
  90. label: getLabel(desktopMenuMessageIds.helpAbout),
  91. click: () => executeTrayCommand(DesktopCommandIds.ShowAbout),
  92. },
  93. {
  94. // 托盘只提供日志诊断入口,避免右键菜单暴露清除全部数据操作;复用应用菜单导出命令。
  95. label: getLabel(desktopMenuMessageIds.helpExportLogs),
  96. click: () => executeTrayCommand(DesktopCommandIds.ExportLogs),
  97. },
  98. { type: "separator" },
  99. {
  100. label: getLabel(desktopMenuMessageIds.trayQuit),
  101. click: () => options.quitApp(),
  102. },
  103. ]),
  104. );
  105. };
  106. rebuildDesktopTrayContextMenu = rebuildContextMenu;
  107. desktopTray.on("click", showTrayWindow);
  108. desktopTray.on("double-click", showTrayWindow);
  109. rebuildContextMenu();
  110. return desktopTray;
  111. }
  112. export function updateWindowsDesktopTrayMenu() {
  113. rebuildDesktopTrayContextMenu?.();
  114. }