增加最小化到系统托盘功能
This commit is contained in:
parent
0ad394850c
commit
c70c4f1a5b
BIN
assets/tray.png
Normal file
BIN
assets/tray.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.6 KiB |
44
src/main.ts
44
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user