增加最小化到系统托盘功能

This commit is contained in:
Allen 2025-04-30 11:18:54 +08:00
parent 0ad394850c
commit c70c4f1a5b
2 changed files with 36 additions and 8 deletions

BIN
assets/tray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@ -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