trayManage.ts 929 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { app, Menu, Tray } from "electron";
  2. import { t } from "i18next";
  3. import { hideWindow, showWindow } from "./windowManage";
  4. let appTray: Tray;
  5. export const createTray = () => {
  6. const trayMenu = Menu.buildFromTemplate([
  7. {
  8. label: t("system.showWindow"),
  9. click: showWindow,
  10. },
  11. {
  12. label: t("system.hideWindow"),
  13. click: hideWindow,
  14. },
  15. {
  16. label: t("system.toggleDevTools"),
  17. role: "toggleDevTools",
  18. },
  19. {
  20. label: t("system.quit"),
  21. click: () => {
  22. global.forceQuit = true;
  23. app.quit();
  24. },
  25. },
  26. ]);
  27. appTray = new Tray(global.pathConfig.trayIcon);
  28. appTray.setToolTip(app.getName());
  29. appTray.setIgnoreDoubleClickEvents(true);
  30. appTray.on("click", showWindow);
  31. appTray.setContextMenu(trayMenu);
  32. };
  33. export const destroyTray = () => {
  34. if (!appTray || appTray.isDestroyed()) return;
  35. appTray.destroy();
  36. appTray = null;
  37. };