1
0

feat: 添加托盘项生成逻辑

This commit is contained in:
2025-06-10 16:02:19 +08:00
parent f8fa295513
commit 009d5e0d22
2 changed files with 47 additions and 0 deletions

View File

@@ -10,6 +10,10 @@ import {
electronApp, optimizer, is,
} from '@electron-toolkit/utils';
import {
initTrayItems,
} from './tray';
import icon from '../../resources/icon.png?asset';
import process from 'process';
@@ -66,6 +70,7 @@ app.whenReady().then(() => {
});
createWindow();
initTrayItems();
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the

42
src/main/tray.js Normal file
View File

@@ -0,0 +1,42 @@
import {
Tray, Menu, nativeImage,
} from 'electron';
/**
* @typedef MenuItem
* @type {Electron.MenuItem | Electron.MenuItemConstructorOptions}
*/
/**
* @typedef TrayItem
* @property {string} iconPath
* @property {MenuItem[]} menus
* @property {string} title
* @property {string} tooltip
*/
/**
* @desc 托盘项列表
* @type {TrayItem[]}
*/
const TRAY_ITEMS = [
];
/** 初始化托盘项 */
export function initTrayItems() {
TRAY_ITEMS.forEach((item) => {
let { iconPath, menus } = item;
let icon = nativeImage.createFromPath(iconPath);
let tray = new Tray(icon);
if (Array.isArray(menus) && menus.length > 0) {
tray.setContextMenu(Menu.buildFromTemplate(menus));
}
item.title && tray.setTitle(item.title);
item.tooltip && tray.setToolTip(item.tooltip);
});
}