refactor: custom popup menu with CSS styles, fix start/stop state
This commit is contained in:
1 parent
08974f0352
commit
ab8d7b61a2
1 file changed
+85
-31
+85
-31
@@ -1,4 +1,4 @@
|
|||||||
const { app, Tray, Menu, nativeImage } = require('electron')
|
const { app, Tray, BrowserWindow, nativeImage } = require('electron')
|
||||||
const { spawn, exec } = require('child_process')
|
const { spawn, exec } = require('child_process')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
@@ -6,57 +6,111 @@ const PROJECT_DIR = path.resolve(__dirname, '../../project')
|
|||||||
let backend = null
|
let backend = null
|
||||||
let tray = null
|
let tray = null
|
||||||
let isRunning = false
|
let isRunning = false
|
||||||
|
let popup = null
|
||||||
|
|
||||||
const ICON = nativeImage.createFromBuffer(Buffer.from(
|
const ICON = nativeImage.createFromBuffer(Buffer.from(
|
||||||
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVR4nO3OIQEAAAgDMHJg6Ul1iHEzMb/q2UsqAQEBAQEBAQEBAQEBAQGBdOABvyXoW2Q1L3wAAAAASUVORK5CYII=',
|
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVR4nO3OIQEAAAgDMHJg6Ul1iHEzMb/q2UsqAQEBAQEBAQEBAQEBAQGBdOABvyXoW2Q1L3wAAAAASUVORK5CYII=',
|
||||||
'base64'
|
'base64'
|
||||||
))
|
))
|
||||||
|
|
||||||
function updateMenu() {
|
function togglePopup() {
|
||||||
tray.setContextMenu(Menu.buildFromTemplate([
|
if (popup && !popup.isDestroyed() && popup.isVisible()) {
|
||||||
{ label: '打开页面', click: () => exec('start http://localhost:3000') },
|
popup.hide()
|
||||||
{ type: 'separator' },
|
return
|
||||||
{ label: '启动服务', enabled: !isRunning, click: startBackend },
|
}
|
||||||
{ label: '停止服务', enabled: isRunning, click: stopBackend },
|
createPopup()
|
||||||
{ type: 'separator' },
|
|
||||||
{ label: '退出', click: () => { stopBackend(); app.quit() } },
|
|
||||||
]))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function startBackend() {
|
function createPopup() {
|
||||||
if (backend) return
|
if (popup && !popup.isDestroyed()) popup.close()
|
||||||
isRunning = true
|
|
||||||
updateMenu()
|
|
||||||
|
|
||||||
backend = spawn('npm.cmd', ['run', 'start'], {
|
const bounds = tray.getBounds()
|
||||||
cwd: PROJECT_DIR,
|
popup = new BrowserWindow({
|
||||||
stdio: 'ignore',
|
width: 180,
|
||||||
env: { ...process.env },
|
height: 175,
|
||||||
shell: true,
|
x: Math.round(bounds.x + bounds.width / 2 - 90),
|
||||||
windowsHide: true,
|
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 },
|
||||||
})
|
})
|
||||||
|
|
||||||
backend.on('exit', () => {
|
popup.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(popupHTML())}`)
|
||||||
backend = null
|
|
||||||
isRunning = false
|
popup.once('ready-to-show', () => popup.show())
|
||||||
updateMenu()
|
|
||||||
|
popup.on('blur', () => {
|
||||||
|
setTimeout(() => { if (popup && !popup.isDestroyed()) popup.hide() }, 100)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function stopBackend() {
|
function popupHTML() {
|
||||||
if (!backend) return
|
const s = !isRunning
|
||||||
isRunning = false
|
return `<!DOCTYPE html><html><head><meta charset="utf-8">
|
||||||
updateMenu()
|
<style>
|
||||||
backend.kill('SIGTERM')
|
*{margin:0;padding:0;box-sizing:border-box;font-family:'Microsoft YaHei','Segoe UI',sans-serif}
|
||||||
backend = null
|
body{background:#fff;border:1px solid #c0c0c0;border-radius:4px;overflow:hidden}
|
||||||
|
.item{padding:8px 16px;cursor:pointer;font-size:13px;color:#333;user-select:none}
|
||||||
|
.item:hover{background:#e5f3ff}
|
||||||
|
.item.disabled{color:#c0c0c0;cursor:default}
|
||||||
|
.item.disabled:hover{background:#fff}
|
||||||
|
.sep{height:1px;background:#e0e0e0;margin:2px 8px}
|
||||||
|
</style></head><body>
|
||||||
|
<div class="item" onclick="openPage()">打开页面</div>
|
||||||
|
<div class="sep"></div>
|
||||||
|
<div class="item${s?'':' disabled'}" onclick="startBackend()">启动服务</div>
|
||||||
|
<div class="item${s?' disabled':''}" onclick="stopBackend()">停止服务</div>
|
||||||
|
<div class="sep"></div>
|
||||||
|
<div class="item" onclick="quit()">退出</div>
|
||||||
|
<script>
|
||||||
|
const {ipcRenderer} = require('electron')
|
||||||
|
function openPage(){ipcRenderer.send('menu-open')}
|
||||||
|
function startBackend(){ipcRenderer.send('menu-start')}
|
||||||
|
function stopBackend(){ipcRenderer.send('menu-stop')}
|
||||||
|
function quit(){ipcRenderer.send('menu-quit')}
|
||||||
|
</script></body></html>`
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
tray = new Tray(ICON.resize({ width: 16, height: 16 }))
|
tray = new Tray(ICON.resize({ width: 16, height: 16 }))
|
||||||
tray.setToolTip('质量追溯系统')
|
tray.setToolTip('质量追溯系统')
|
||||||
updateMenu()
|
tray.on('click', togglePopup)
|
||||||
startBackend()
|
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,
|
||||||
|
})
|
||||||
|
isRunning = true
|
||||||
|
if (popup && !popup.isDestroyed()) popup.webContents.reload()
|
||||||
|
backend.on('exit', () => {
|
||||||
|
backend = null
|
||||||
|
isRunning = false
|
||||||
|
if (popup && !popup.isDestroyed()) popup.webContents.reload()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopBackend() {
|
||||||
|
if (!backend) return
|
||||||
|
backend.kill('SIGTERM')
|
||||||
|
backend = null
|
||||||
|
isRunning = false
|
||||||
|
if (popup && !popup.isDestroyed()) popup.webContents.reload()
|
||||||
|
}
|
||||||
|
|
||||||
app.on('window-all-closed', () => {})
|
app.on('window-all-closed', () => {})
|
||||||
app.on('before-quit', () => stopBackend())
|
app.on('before-quit', () => stopBackend())
|
||||||
Reference in new issue
Block a user