WinTray.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * 系统托盘
  3. */
  4. import { app, Tray, shell, ipcMain, Menu, MenuItem, type MenuItemConstructorOptions } from "electron"
  5. import GlobalConfig from "./GlobalConfig"
  6. import WinMain from "./WinMain"
  7. class WinTray {
  8. /** 托盘实例 */
  9. private static TRAY_INST: Tray | null = null
  10. /** 托盘菜单 */
  11. private static TRAY_MENU: (MenuItemConstructorOptions | MenuItem)[] = []
  12. /** 初始化菜单 */
  13. static initTrayMenu() {
  14. this.TRAY_MENU = [
  15. {
  16. label: "显示",
  17. click: () => WinMain.show("tray=>show", true)
  18. },
  19. {
  20. label: "隐藏",
  21. click: () => WinMain.getWinInst()?.hide()
  22. },
  23. {
  24. label: "控制台",
  25. click: () => {
  26. WinMain.show("tray=>open")
  27. WinMain.openDevTools()
  28. }
  29. },
  30. {
  31. label: "本地日志",
  32. click: () => {
  33. const logsPath = GlobalConfig.getLocalLogsPath()
  34. logsPath && shell.openPath(logsPath)
  35. }
  36. },
  37. {
  38. role: "quit",
  39. label: "关闭客户端"
  40. }
  41. ]
  42. }
  43. /** 托盘-创建 */
  44. static create() {
  45. const contextMenu = Menu.buildFromTemplate(this.TRAY_MENU)
  46. if (GlobalConfig.IS_MACOS) {
  47. app.dock?.setIcon(GlobalConfig.getAppLogo(true))
  48. app.dock?.setMenu(contextMenu)
  49. return
  50. }
  51. // 声明托盘对象
  52. this.TRAY_INST = new Tray(GlobalConfig.getAppLogo())
  53. // 悬停提示内容
  54. this.TRAY_INST.setToolTip(GlobalConfig.getAppTitle())
  55. // 右键菜单
  56. this.TRAY_INST.setContextMenu(contextMenu)
  57. // 双击图标打开窗口
  58. this.TRAY_INST.on("double-click", () => WinMain.show("tray=>double-click", true))
  59. }
  60. /** 销毁托盘 */
  61. static destroy() {
  62. this.TRAY_INST?.destroy()
  63. this.TRAY_INST = null
  64. app.dock?.hide()
  65. }
  66. /** 监听相关事件 */
  67. static ipcListening() {
  68. // xxxx
  69. ipcMain.on(`xxxx`, () => {
  70. // ...
  71. })
  72. }
  73. }
  74. export default WinTray