diff --git a/assets/tray.png b/assets/tray.png new file mode 100644 index 0000000..73fd5c9 Binary files /dev/null and b/assets/tray.png differ diff --git a/src/main.ts b/src/main.ts index 5bd0910..fa6d0a1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, globalShortcut, ipcMain } from 'electron'; +import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray } from 'electron'; import path from 'node:path'; import started from 'electron-squirrel-startup'; @@ -50,13 +50,6 @@ const createWindow = () => { callback(true); }); - // and load the index.html of the app. - if (MAIN_WINDOW_VITE_DEV_SERVER_URL) { - win.loadURL(`${MAIN_WINDOW_VITE_DEV_SERVER_URL}/login.html`); - } else { - win.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/login.html`)); - } - // production a bad jabe const handleDevTools = () => { if (win.webContents.isDevToolsOpened()) { @@ -66,6 +59,41 @@ const createWindow = () => { } }; globalShortcut.register("CommandOrControl+Shift+I", handleDevTools); + + // and load the login.html of the app. + if (MAIN_WINDOW_VITE_DEV_SERVER_URL) { + win.loadURL(`${MAIN_WINDOW_VITE_DEV_SERVER_URL}/login.html`); + } else { + win.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/login.html`)); + } + + // 最小化到系统托盘 + win.on('minimize', () => { + win.hide(); // 隐藏窗口 + }); + + // 监听窗口关闭(用户点击右上角X) + win.on('close', (event) => { + event.preventDefault(); + win.hide(); + }); + + // 创建托盘图标 + const tray = new Tray(path.join(__dirname, `../../assets/tray.png`)); // 你的托盘图标路径(需要是小图片) + + const contextMenu = Menu.buildFromTemplate([ + { label: '显示窗口', click: () => win.show() }, + { label: '退出程序', click: () => app.exit() } + ]); + + tray.setToolTip('中国电信-工作台'); + tray.setContextMenu(contextMenu); + + // 点击托盘图标也可以恢复窗口(可选) + tray.on('click', () => { + win.show(); + }); + }; // This method will be called when Electron has finished