From 0666d4677e599bdae084c402ae0170659171528d Mon Sep 17 00:00:00 2001 From: wicast Date: Tue, 8 Jul 2025 11:00:47 +0800 Subject: [PATCH] feat: add tray icon --- src/server/index.js | 10 +++++++ src/server/tray.js | 69 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/server/tray.js diff --git a/src/server/index.js b/src/server/index.js index ed1ef705..50e47f7b 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -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, diff --git a/src/server/tray.js b/src/server/tray.js new file mode 100644 index 00000000..cca4275c --- /dev/null +++ b/src/server/tray.js @@ -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) +} \ No newline at end of file