tray.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. appIcon.destroy();
  8. // Re create the main window on dashboard page and close the invisible window
  9. const newWindow = window.new(path.join(__dirname, '../../renderer/page/dashboard/dashboard.html'));
  10. mainWindow.close();
  11. mainWindow = newWindow;
  12. }
  13. };
  14. tray.closeApp = function () {
  15. if (appIcon) {
  16. appIcon.destroy();
  17. app.quit();
  18. }
  19. }
  20. tray.active = function () {
  21. appIcon = new Tray(path.join(__dirname, '../asset/tray.png'));
  22. appIcon.setToolTip('Electronotes');
  23. appIcon.setContextMenu(menu);
  24. // Hide app
  25. const invisibleWindow = window.hidden(); // Create invisible window to avoid closing the app when all window are closed
  26. mainWindow.close();
  27. mainWindow = invisibleWindow;
  28. // Left click => open app
  29. appIcon.on('click', (event) => {
  30. tray.openApp();
  31. });
  32. };
  33. module.exports = tray;
  34. /* --- Var and event --- */
  35. let appIcon = null;
  36. app.on('quit', () => {
  37. if (appIcon) {
  38. appIcon.destroy();
  39. }
  40. });
  41. const template = [
  42. {
  43. label: 'Ouvrir',
  44. click: tray.openApp,
  45. },
  46. {
  47. label: 'Quitter',
  48. click: tray.closeApp
  49. }
  50. ];
  51. const menu = Menu.buildFromTemplate(template);