| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-18 20:27:42 |
| 4 | * @LastEditTime: 2025-01-20 10:34:52 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { app, Tray, Menu, BrowserWindow } from 'electron'; |
| 9 | import { getAssetPath } from '../util/index.js'; |
| 10 | |
| 11 | export class SystemTray { |
| 12 | private tray: Tray | null = null; |
| 13 | |
| 14 | constructor(private mainWindow: BrowserWindow) {} |
| 15 | |
| 16 | create(): Tray { |
| 17 | if (this.tray) return this.tray; |
| 18 | |
| 19 | const icoPath = getAssetPath('favicon.ico'); |
| 20 | this.tray = new Tray(icoPath); |
| 21 | this.setupTrayMenu(); |
| 22 | this.setupTrayEvents(); |
| 23 | |
| 24 | return this.tray; |
| 25 | } |
| 26 | |
| 27 | // 设置托盘菜单 |
| 28 | private setupTrayMenu() { |
| 29 | if (!this.tray) return; |
| 30 | |
| 31 | const contextMenu = Menu.buildFromTemplate([ |
| 32 | { |
| 33 | label: '显示', |
| 34 | click: () => this.mainWindow.show(), |
| 35 | }, |
| 36 | { |
| 37 | label: '最小化', |
| 38 | click: () => this.mainWindow.hide(), |
| 39 | }, |
| 40 | { |
| 41 | type: 'separator', |
| 42 | }, |
| 43 | { |
| 44 | label: '退出', |
| 45 | click: () => app.quit(), |
| 46 | }, |
| 47 | ]); |
| 48 | |
| 49 | this.tray.setToolTip('哎哟赚AiToEarn'); |
| 50 | this.tray.setContextMenu(contextMenu); |
| 51 | } |
| 52 | |
| 53 | // 设置托盘事件 |
| 54 | private setupTrayEvents() { |
| 55 | if (!this.tray) return; |
| 56 | |
| 57 | this.tray.on('click', () => { |
| 58 | const win = this.mainWindow; |
| 59 | if (win) { |
| 60 | win.show(); |
| 61 | win.focus(); |
| 62 | } |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | // 销毁托盘 |
| 67 | destroy() { |
| 68 | if (this.tray) { |
| 69 | this.tray.destroy(); |
| 70 | this.tray = null; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 |