diff --git a/src/main/index.js b/src/main/index.js index 42eb172..7d234f3 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -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 diff --git a/src/main/tray.js b/src/main/tray.js new file mode 100644 index 0000000..04ecd60 --- /dev/null +++ b/src/main/tray.js @@ -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); + + }); +}