import {app as electronApp, Menu, Tray} from 'electron'; import path from 'path'; import {getBaseDir} from 'ee-core/ps'; import {logger} from 'ee-core/log'; import {getConfig} from 'ee-core/config'; import {getCloseAndQuit, getMainWindow, setCloseAndQuit} from 'ee-core/electron'; /** * 托盘 * @class */ class TrayService { tray: Tray | null; config: { title: string; icon: string; } constructor() { this.tray = null; this.config = { title: electronApp.name, icon: '/public/images/tray.png', } } /** * Create the tray icon */ create () { logger.info('[tray] load'); const cfg = this.config; const mainWindow = getMainWindow(); // tray icon const iconPath = path.join(getBaseDir(), cfg.icon); // Tray menu items const trayMenuTemplate = [ { label: '显示', click: function () { mainWindow.show(); } }, { label: '退出', click: function () { electronApp.quit(); } } ] setCloseAndQuit(true); // Set a flag to minimize to tray instead of closing // setCloseAndQuit(false); mainWindow.on('close', (event: any) => { if (getCloseAndQuit()) { return; } mainWindow.hide(); event.preventDefault(); }); // Initialize the tray this.tray = new Tray(iconPath); const { windowsOption } = getConfig(); if (windowsOption.title) { cfg.title = windowsOption.title; } this.tray.setToolTip(cfg.title); const contextMenu = Menu.buildFromTemplate(trayMenuTemplate); this.tray.setContextMenu(contextMenu); // Show the main window when the tray icon is clicked this.tray.on('click', () => { mainWindow.show() }) } } TrayService.toString = () => '[class TrayService]'; const trayService = new TrayService(); export { trayService }