tray.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const {app, Tray, Menu} = require('electron');
  2. const path = require('path');
  3. const window = require('../../../helper/window');
  4. const tray = {};
  5. tray.openApp = function () {
  6. if (appIcon) {
  7. // Set tray inactive
  8. trayInactive = true;
  9. // Remove tray
  10. appIcon.destroy();
  11. // Re create the main window on dashboard page and close the invisible window
  12. mainWindow = window.new(path.join(__dirname, '../../renderer/page/dashboard/dashboard.html'));
  13. }
  14. };
  15. tray.closeApp = function () {
  16. if (appIcon) {
  17. appIcon.destroy();
  18. app.quit();
  19. }
  20. }
  21. tray.active = function () {
  22. // Set tray active
  23. trayInactive = false;
  24. // Create tray
  25. appIcon = new Tray(path.join(__dirname, '../asset/tray.png'));
  26. appIcon.setToolTip('Electronotes');
  27. appIcon.setContextMenu(menu);
  28. // Hide app
  29. mainWindow.close();
  30. mainWindow = null;
  31. // Left click => open app
  32. appIcon.on('click', (event) => {
  33. tray.openApp();
  34. });
  35. };
  36. module.exports = tray;
  37. /* --- Var and event --- */
  38. let appIcon = null;
  39. app.on('quit', () => {
  40. if (appIcon) {
  41. appIcon.destroy();
  42. }
  43. });
  44. const template = [
  45. {
  46. label: 'Ouvrir',
  47. click: tray.openApp,
  48. },
  49. {
  50. label: 'Quitter',
  51. click: tray.closeApp
  52. }
  53. ];
  54. const menu = Menu.buildFromTemplate(template);