From a5d5586dc367f9d8be3b193b392c414c74a43297 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 8 Jul 2025 16:58:02 +0200 Subject: [PATCH] tray: code lint, retina icon for mac, keep alive launcher and tray when closing if there's a client window --- .../images/{logo_16x16.png => logo_tray.png} | Bin resources/images/logo_tray@x2.png | Bin 0 -> 1688 bytes scripts/copy-assets.js | 2 + src/server/index.js | 24 +++- src/server/tray.js | 111 ++++++++++-------- 5 files changed, 85 insertions(+), 52 deletions(-) rename resources/images/{logo_16x16.png => logo_tray.png} (100%) create mode 100644 resources/images/logo_tray@x2.png diff --git a/resources/images/logo_16x16.png b/resources/images/logo_tray.png similarity index 100% rename from resources/images/logo_16x16.png rename to resources/images/logo_tray.png diff --git a/resources/images/logo_tray@x2.png b/resources/images/logo_tray@x2.png new file mode 100644 index 0000000000000000000000000000000000000000..c3c4f0c64225c3409ebabefe20c365e491489eca GIT binary patch literal 1688 zcmV;J250$+P)b%78FWQhbW?9;ba!ELWdL_~cP?peYja~^aAhuUa%Y?FJQ@H11|>;E zK~z|Ut(RSF99I>`fA`Mr&U(Ec_S!D79mny3x z@-VwoyLRGDn*YgzkvoZbzs-q1diqs}RHiP-K8BD9#EY z2Bj(-Pp8vHCHsnv2qY5Sn;l{AQ-W`xsI7=C%%k{CQ7az2df~$KT@mQ*?e$%`Hunth z3{bW9OF>o5rvND2m_;dPy>0Rc@nUOJ{m+MnhKdyvXh|mHev`L=U1gc(53;Fy7mc6Z zMQ!^I0@ZPZlmIBl!CabW{?Zv{&YfUp_yl%w)gKtJ4f)k8S4I{#B+${(l~9^|7w9Z| zx@G5=*w+6v!PtH4Ik5)j;yjmzjxhfIZ&9{g7N@1dp0Tmi*ewZ^JU0?;=tV=D4I5;q1}cn#Yu+>!B}PrdAUe)i~orcND0IS%>E6aboE$6p=wKCxt=PdY;Nl_i_D zyLHDlmYF>H$BK=w3cGR`Ir;)qr;d5+#^QA-RZ2Y^_vBL1CvoWZD3!7{bLl_WRz`YJQxeiGY|a z3%%@wmG%1`LaZy5Kn@_kFyjdW(fWICUi$p*gHkxQm4@wx8LWsfg+lM0q0QiGp zT0j5gdqzNy)I%x9jGObGTq#b-s(=~*G62)3-@{yThnCJqzJ*T@ePZK6N1T9;qi(x7iE3Z53FY8l`A?1I<8d zbHkoBBN;$z>-JhrcRrvrlnTv9v?+nVYDEK4Zey{{yjNZ|UjQu-a*x`IifS!q$xWwG zcIir>xTuQ$of8vhZg`|WK9+ZOCOaL;yJZppdL&M$e#@$f_jYiKCd-*AmUDAn(Lr%h zL+nYX&yBB&+Ikn0QY8KW?DiCe#9tjn56AI`Vn{9Eb~VqaftAl<+?>Z+%6M|=Yh2aK?tPg)_2FYKJs)mmXzYa<*}|`DD~F59d5(R>DI2U zMrmpL5b8-Fep}^_kO6gAS>j+h+i!&p36zmcCId?a`h--wK<)t5N@)zr`H@0000settings.options[x].launcher !== false).length / 2), node:true, color:'#151a24', id: 'launcher'}) require('@electron/remote/main').enable(launcher.webContents) - launcher.on('close', ()=>{ - process.stdout.write = stdoutWrite - process.stderr.write = stderrWrite - if (process.log) process.log = processLog - }) tray = require('./tray')({ window: launcher, @@ -231,6 +226,25 @@ function startLauncher() { stopServer: stopServerProcess }) + launcher.on('will-close', ()=>{ + if (clientWindows.length) { + launcher.hide() + e.preventDefault() + } + }) + + launcher.on('close', (e)=>{ + if (clientWindows.length) { + launcher.hide() + e.preventDefault() + return + } + process.stdout.write = stdoutWrite + process.stderr.write = stderrWrite + if (process.log) process.log = processLog + tray.destroy() + }) + }) let processLog = process.log, diff --git a/src/server/tray.js b/src/server/tray.js index 79e63542..bb49d4a6 100644 --- a/src/server/tray.js +++ b/src/server/tray.js @@ -1,72 +1,89 @@ -var { Menu, Tray, ipcMain } = require('electron') - -var tray = null, +var {Menu, Tray, ipcMain} = require('electron'), + settings = require('./settings'), + 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) - }) +module.exports = function(options = {}) { var icon = __dirname + '/../assets/logo.png' if (process.platform === 'darwin') { - var icon = __dirname + '/../assets/logo_16x16.png' + icon = __dirname + '/../assets/logo_tray.png' } + tray = new Tray(icon) contextMenu = Menu.buildFromTemplate([ { - label: 'Open/Hide', click: () => { - if (hideInTray) { - options.window.show() - hideInTray = false - } else { - options.window.hide() - hideInTray = true - } + label: 'Show launcher', + id: 'show', + click: ()=>{ + options.window.show() } }, { - label: 'Start/Stop', - click: () => { - if (isServerRunning) { - ipcMain.emit('stop'); - } else { - ipcMain.emit('start'); - } + label: 'Hide launcher', + id: 'hide', + click: ()=>{ + options.window.hide() } }, { - label: 'New Window', click: () => { + label: 'Start server', + id: 'start', + click: ()=>{ + ipcMain.emit('start') + } + }, + { + label: 'Stop server', + id: 'stop', + click: ()=>{ + ipcMain.emit('stop') + } + }, + { + label: 'New client window', + id: 'new', + click: ()=> { if (isServerRunning) { options.openClient() } - }, - enabled: isServerRunning + } }, - { type: 'separator' }, - { label: 'Quit', click: () => { void options.app.quit(); } }, - ]); + { + type: 'separator' + }, + { + label: 'Quit', + role: 'Quit' + }, + ]) + tray.setContextMenu(contextMenu) - tray.setToolTip('Open Stage Control') + tray.setToolTip(settings.infos.productName) + + function updateMenu(){ + contextMenu.getMenuItemById('start').visible = !isServerRunning + contextMenu.getMenuItemById('stop').visible = isServerRunning + contextMenu.getMenuItemById('show').visible = !options.window.isVisible() + contextMenu.getMenuItemById('hide').visible = options.window.isVisible() + contextMenu.getMenuItemById('new').visible = isServerRunning + tray.setContextMenu(contextMenu) + } + + ipcMain.on('start', function(e, options) { + isServerRunning = true + updateMenu() + }) + + ipcMain.on('stop', function(e, options) { + isServerRunning = false + updateMenu() + }) + + options.window.on('show', updateMenu) + options.window.on('hide', updateMenu) 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