tray.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const {app, Tray, Menu} = require('electron');
  3. const path = require('path');
  4. const helper = require('./helper');
  5. const config = require('../config');
  6. exports.setup = function () {
  7. const cfg = config.get('tray');
  8. // 托盘图标
  9. let iconPath = path.join(app.getAppPath(), cfg.icon);
  10. // 托盘菜单功能列表
  11. let trayMenuTemplate = [
  12. {
  13. label: '显示',
  14. click: function () {
  15. MAIN_WINDOW.show();
  16. }
  17. },
  18. {
  19. label: '退出',
  20. click: function () {
  21. helper.appQuit();
  22. }
  23. }
  24. ]
  25. // 点击关闭,最小化到托盘
  26. MAIN_WINDOW.on('close', (event) => {
  27. if (!CAN_QUIT) {
  28. MAIN_WINDOW.hide();
  29. MAIN_WINDOW.setSkipTaskbar(true);
  30. event.preventDefault();
  31. }
  32. });
  33. MAIN_WINDOW.show();
  34. APP_TRAY = new Tray(iconPath);
  35. APP_TRAY.setToolTip(cfg.title); // 托盘标题
  36. const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
  37. APP_TRAY.setContextMenu(contextMenu);
  38. // 监听 显示/隐藏
  39. APP_TRAY.on('click', function(){
  40. if (MAIN_WINDOW.isVisible()) {
  41. MAIN_WINDOW.hide();
  42. MAIN_WINDOW.setSkipTaskbar(false);
  43. } else {
  44. MAIN_WINDOW.show();
  45. MAIN_WINDOW.setSkipTaskbar(true);
  46. }
  47. });
  48. return APP_TRAY;
  49. }
  50. exports = module.exports;