Files
pqs-9100_tool_client/electron/service/os/tray.ts

81 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-10-16 20:01:57 +08:00
import { Tray, Menu } from 'electron';
import path from 'path';
import { isDev, getBaseDir } from 'ee-core/ps';
import { logger } from 'ee-core/log';
import { app as electronApp } from 'electron';
import { getMainWindow, getCloseAndQuit, setCloseAndQuit } from 'ee-core/electron';
/**
*
* @class
*/
class TrayService {
tray: Tray | null;
config: {
title: string;
icon: string;
}
constructor() {
this.tray = null;
this.config = {
title: 'electron-egg',
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();
}
}
]
// 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);
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
}