tray.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Menu, Tray, app } from "electron";
  2. import { ICON, readIniFile, writeIniFile } from "./utils";
  3. import { showMainWin, extra } from "../win/mainWin";
  4. import log from "electron-log/main";
  5. import { handeGet, handeSet } from "./store";
  6. export const initTray = () => {
  7. let appIcon = new Tray(ICON);
  8. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  9. let contextMenu = Menu.buildFromTemplate([
  10. {
  11. id: "open-win",
  12. label: `${isFocusOpenWin == 1 ? "关闭" : "开启"}主动弹窗`,
  13. click: () => {
  14. updateItem();
  15. },
  16. },
  17. {
  18. label: "打开弹窗",
  19. click: () => {
  20. showMainWin();
  21. },
  22. },
  23. {
  24. label: "退出弹窗",
  25. click: () => {
  26. extra.activeClose = true;
  27. setTimeout(() => {
  28. app.quit();
  29. });
  30. },
  31. },
  32. ]);
  33. appIcon.setToolTip("菁苗健康");
  34. appIcon.setContextMenu(contextMenu);
  35. appIcon.addListener("click", function () {
  36. showMainWin();
  37. });
  38. // 更新item
  39. const updateItem = async () => {
  40. let isFocusOpenWin: number = handeGet("isFocusOpenWin");
  41. const openMenuItem = contextMenu.getMenuItemById("open-win");
  42. if (isFocusOpenWin == 1) {
  43. isFocusOpenWin = 2;
  44. openMenuItem!.label = `开启主动弹窗`;
  45. } else {
  46. isFocusOpenWin = 1;
  47. openMenuItem!.label = `关闭主动弹窗`;
  48. }
  49. contextMenu = Menu.buildFromTemplate(contextMenu.items);
  50. appIcon.setContextMenu(contextMenu);
  51. handeSet("isFocusOpenWin", isFocusOpenWin);
  52. log.info("isFocusOpenWin==>", isFocusOpenWin);
  53. };
  54. return appIcon;
  55. };