feat: add tray icon

This commit is contained in:
wicast 2025-07-08 11:00:47 +08:00
parent 802e1780b1
commit 0666d4677e
2 changed files with 79 additions and 0 deletions

View file

@ -5,6 +5,7 @@ var dev = process.argv[0].includes('node_modules'),
docsServer,
app = null,
launcher = null,
tray = null,
clientWindows = [],
serverProcess = null,
node = false
@ -221,6 +222,15 @@ function startLauncher() {
process.stderr.write = stderrWrite
if (process.log) process.log = processLog
})
tray = require('./tray')({
window: launcher,
openClient: openClient,
app: app,
startServer: startServerProcess,
stopServer: stopServerProcess
})
})
let processLog = process.log,

69
src/server/tray.js Normal file
View file

@ -0,0 +1,69 @@
var { Menu, Tray, ipcMain } = require('electron')
var tray = null,
contextMenu = null,
hideInTray = false,
isServerRunning = false
module.exports = function (options = {}) {
ipcMain.on('start', function (e, options) {
isServerRunning = true
resetMenu(isServerRunning)
})
ipcMain.on('stop', function (e, options) {
isServerRunning = false
resetMenu(isServerRunning)
})
tray = new Tray(__dirname + '/../assets/logo.png')
contextMenu = Menu.buildFromTemplate([
{
label: 'Open/Hide', click: () => {
if (hideInTray) {
options.window.show()
hideInTray = false
} else {
options.window.hide()
hideInTray = true
}
}
},
{
label: 'Start/Stop',
click: () => {
console.log(isServerRunning)
if (isServerRunning) {
ipcMain.emit('stop');
} else {
ipcMain.emit('start');
}
}
},
{
label: 'New Window', click: () => {
if (isServerRunning) {
options.openClient()
}
},
enabled: isServerRunning
},
{ type: 'separator' },
{ label: 'Quit', click: () => { void options.app.quit(); } },
]);
tray.setContextMenu(contextMenu)
tray.setToolTip('Open Stage Control')
return tray
}
function resetMenu(isServerRunning) {
contextMenu.items.forEach(item => {
if (item.label === 'New Window') {
item.enabled = isServerRunning
}
})
tray.setContextMenu(contextMenu)
}