index.js 1.7 KB

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