From 44e03d8e9ac5bb90b32215eeb57297e8c23b0cb1 Mon Sep 17 00:00:00 2001 From: zlm Date: Wed, 10 Jun 2026 14:54:55 +0800 Subject: [PATCH] fix: native tray menu, direct node spawn, proper process kill --- desktop/src/main.js | 112 ++++++++++---------------------------------- 1 file changed, 26 insertions(+), 86 deletions(-) diff --git a/desktop/src/main.js b/desktop/src/main.js index 3afd7e7..d757df7 100644 --- a/desktop/src/main.js +++ b/desktop/src/main.js @@ -1,107 +1,40 @@ -const { app, Tray, BrowserWindow, nativeImage } = require('electron') +const { app, Tray, Menu, nativeImage } = require('electron') const { spawn, exec } = require('child_process') const path = require('path') const PROJECT_DIR = path.resolve(__dirname, '../../project') +const TSX = path.join(PROJECT_DIR, 'node_modules', 'tsx', 'dist', 'cli.mjs') let backend = null let tray = null let isRunning = false -let popup = null const ICON = nativeImage.createFromBuffer(Buffer.from( 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVR4nO3OIQEAAAgDMHJg6Ul1iHEzMb/q2UsqAQEBAQEBAQEBAQEBAQGBdOABvyXoW2Q1L3wAAAAASUVORK5CYII=', 'base64' )) -function togglePopup() { - if (popup && !popup.isDestroyed() && popup.isVisible()) { - popup.hide() - return - } - createPopup() +function updateMenu() { + tray.setContextMenu(Menu.buildFromTemplate([ + { label: '打开页面', click: () => exec('start http://localhost:3000') }, + { type: 'separator' }, + { label: '启动服务', enabled: !isRunning, click: startBackend }, + { label: '停止服务', enabled: isRunning, click: stopBackend }, + { type: 'separator' }, + { label: '退出', click: () => { stopBackend(); app.quit() } }, + ])) } -function createPopup() { - if (popup && !popup.isDestroyed()) popup.close() - - const bounds = tray.getBounds() - popup = new BrowserWindow({ - width: 180, - height: 175, - x: Math.round(bounds.x + bounds.width / 2 - 90), - y: Math.round(bounds.y - 175), - frame: false, - resizable: false, - skipTaskbar: true, - alwaysOnTop: true, - show: false, - transparent: false, - focusable: true, - webPreferences: { nodeIntegration: true, contextIsolation: false }, - }) - - popup.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(popupHTML())}`) - - popup.once('ready-to-show', () => popup.show()) - - popup.on('blur', () => { - setTimeout(() => { if (popup && !popup.isDestroyed()) popup.hide() }, 100) - }) -} - -function popupHTML() { - const s = !isRunning - return ` - -
打开页面
-
-
启动服务
-
停止服务
-
-
退出
-` -} - -app.whenReady().then(() => { - tray = new Tray(ICON.resize({ width: 16, height: 16 })) - tray.setToolTip('质量追溯系统') - tray.on('click', togglePopup) - startBackend() -}) - -const { ipcMain } = require('electron') -ipcMain.on('menu-open', () => exec('start http://localhost:3000')) -ipcMain.on('menu-start', () => { if (!isRunning) startBackend() }) -ipcMain.on('menu-stop', () => { if (isRunning) stopBackend() }) -ipcMain.on('menu-quit', () => { stopBackend(); app.quit() }) - function startBackend() { if (backend) return - backend = spawn('npm.cmd', ['run', 'start'], { - cwd: PROJECT_DIR, stdio: 'ignore', env: { ...process.env }, - shell: true, windowsHide: true, + backend = spawn('node', [TSX, 'src/index.ts'], { + cwd: PROJECT_DIR, + stdio: 'ignore', + env: { ...process.env, PORT: process.env.PORT || '3000' }, + windowsHide: true, }) isRunning = true - if (popup && !popup.isDestroyed()) popup.webContents.reload() - backend.on('exit', () => { - backend = null - isRunning = false - if (popup && !popup.isDestroyed()) popup.webContents.reload() - }) + updateMenu() + backend.on('exit', () => { backend = null; isRunning = false; updateMenu() }) } function stopBackend() { @@ -109,8 +42,15 @@ function stopBackend() { backend.kill('SIGTERM') backend = null isRunning = false - if (popup && !popup.isDestroyed()) popup.webContents.reload() + updateMenu() } +app.whenReady().then(() => { + tray = new Tray(ICON.resize({ width: 16, height: 16 })) + tray.setToolTip('质量追溯系统') + updateMenu() + startBackend() +}) + app.on('window-all-closed', () => {}) app.on('before-quit', () => stopBackend())