From 009d5e0d22e77c81fc50882e7954b36e6d2d91e2 Mon Sep 17 00:00:00 2001 From: Frost-ZX Date: Tue, 10 Jun 2025 16:02:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=89=98=E7=9B=98?= =?UTF-8?q?=E9=A1=B9=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/index.js | 5 +++++ src/main/tray.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/tray.js 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); + + }); +}