index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const {Tray, Menu} = require('electron');
  2. const path = require('path');
  3. /**
  4. * 托盘插件
  5. * @class
  6. */
  7. class TrayAddon {
  8. constructor(app) {
  9. this.app = app;
  10. this.cfg = app.config.addons.tray;
  11. this.tray = null;
  12. }
  13. /**
  14. * 创建托盘
  15. */
  16. create () {
  17. // 开发环境,代码热更新开启时,会导致托盘中有残影
  18. if (process.env.EE_SERVER_ENV == 'local' && process.env.HOT_RELOAD == 'true') return;
  19. this.app.console.info('[addon:tray] load');
  20. const mainWindow = this.app.electron.mainWindow;
  21. // 托盘图标
  22. let iconPath = path.join(this.app.config.homeDir, this.cfg.icon);
  23. // 托盘菜单功能列表
  24. const self = this;
  25. let trayMenuTemplate = [
  26. {
  27. label: '显示',
  28. click: function () {
  29. mainWindow.show();
  30. }
  31. },
  32. {
  33. label: '退出',
  34. click: function () {
  35. self.app.appQuit();
  36. }
  37. }
  38. ]
  39. // 点击关闭,最小化到托盘
  40. mainWindow.on('close', (event) => {
  41. const extraObj = this.app.electron.extra;
  42. if (extraObj.closeWindow == true) {
  43. return;
  44. }
  45. mainWindow.hide();
  46. event.preventDefault();
  47. });
  48. // 实例化托盘
  49. this.tray = new Tray(iconPath);
  50. this.tray.setToolTip(this.cfg.title);
  51. const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
  52. this.tray.setContextMenu(contextMenu);
  53. }
  54. }
  55. TrayAddon.toString = () => '[class TrayAddon]';
  56. module.exports = TrayAddon;