feat: 添加托盘项生成逻辑
This commit is contained in:
@@ -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
42
src/main/tray.js
Normal 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);
|
||||
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user