tray.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Tray, Menu, dialog, BrowserWindow } from 'electron';
  2. import { join } from 'path';
  3. import { getBaseDir } from 'ee-core/ps';
  4. import { logger } from 'ee-core/log';
  5. import { app as electronApp } from 'electron';
  6. import { getMainWindow, getCloseAndQuit, setCloseAndQuit } from 'ee-core/electron';
  7. /**
  8. * 托盘
  9. * @class
  10. */
  11. class TrayService {
  12. constructor() {
  13. this.tray = null;
  14. this.quitConfirming = false;
  15. this.config = {
  16. title: 'electron-egg',
  17. icon: '/public/images/tray.png'
  18. }
  19. }
  20. /**
  21. * 创建托盘
  22. */
  23. create () {
  24. logger.info('[tray] load');
  25. const cfg = this.config;
  26. const mainWindow = getMainWindow();
  27. // tray icon
  28. const iconPath = join(getBaseDir(), cfg.icon);
  29. /**
  30. * 托盘退出前确认。
  31. *
  32. * <p>托盘点击发生在主进程,此时主窗口可能已经收进托盘(不可见),
  33. * 没法可靠地在渲染层弹 Ant Design 确认框,所以用系统原生对话框。
  34. */
  35. const quitWithConfirm = async () => {
  36. if (this.quitConfirming) return;
  37. this.quitConfirming = true;
  38. try {
  39. const options = {
  40. type: 'warning',
  41. title: '退出程序',
  42. message: '确认退出数剑?',
  43. detail: '退出程序会关闭所有窗口。',
  44. buttons: ['取消', '退出'],
  45. defaultId: 0,
  46. cancelId: 0,
  47. noLink: true,
  48. };
  49. // 有聚焦窗口就挂成它的模态;托盘菜单打开时通常没有聚焦窗口,用独立对话框
  50. const focused = BrowserWindow.getFocusedWindow();
  51. const parent = focused && !focused.isDestroyed() ? focused : undefined;
  52. const result = parent
  53. ? await dialog.showMessageBox(parent, options)
  54. : await dialog.showMessageBox(options);
  55. if (result.response === 1) {
  56. electronApp.quit();
  57. }
  58. } catch (e) {
  59. logger.error('[tray] 退出确认失败:', e instanceof Error ? e.message : String(e));
  60. } finally {
  61. this.quitConfirming = false;
  62. }
  63. };
  64. // 托盘菜单功能列表
  65. // 智能工作台不放入口:顶栏「智能」开关是唯一入口(案件/账号生命周期也只在主窗口)
  66. const trayMenuTemplate = [
  67. {
  68. label: '显示主窗口',
  69. click: function () {
  70. if (mainWindow.isMinimized()) {
  71. mainWindow.restore();
  72. }
  73. mainWindow.show();
  74. mainWindow.focus();
  75. }
  76. },
  77. {
  78. label: '退出程序',
  79. click: () => {
  80. void quitWithConfirm();
  81. }
  82. }
  83. ]
  84. // 设置一个标识,点击关闭,最小化到托盘
  85. setCloseAndQuit(false);
  86. mainWindow.on('close', (event) => {
  87. if (getCloseAndQuit()) {
  88. return;
  89. }
  90. mainWindow.hide();
  91. event.preventDefault();
  92. });
  93. // 实例化托盘
  94. this.tray = new Tray(iconPath);
  95. this.tray.setToolTip(cfg.title);
  96. const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
  97. this.tray.setContextMenu(contextMenu);
  98. // 左键单击的时候能够显示主窗口
  99. this.tray.on('click', () => {
  100. mainWindow.show()
  101. })
  102. }
  103. }
  104. TrayService.toString = () => '[class TrayService]';
  105. export const trayService = new TrayService();