diff --git a/src/main.ts b/src/main.ts index 6934f6c..de26315 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, ipcMain, Menu, nativeImage, shell, Tray } from 'electron'; +import { app, BrowserWindow, ipcMain, Menu, MenuItemConstructorOptions, nativeImage, shell, Tray } from 'electron'; import path from 'node:path'; import started from 'electron-squirrel-startup'; import { APP_NAME, DEFAULT_STATUS, GATEWAY_HOST, MANAGEMENT_URL, POLL_INTERVAL_MS, WINDOW_HEIGHT, WINDOW_WIDTH } from './config'; @@ -84,6 +84,25 @@ function updateTray() { if (!tray) return; const label = cachedStatus.state === 'connected' ? `${APP_NAME}: Connected` : APP_NAME; tray.setToolTip(label); + + const template: MenuItemConstructorOptions[] = []; + const connected = cachedStatus.state === "connected"; + const busy = ['connecting', 'disconnecting'].includes(cachedStatus.state); + + template.push({ + label: connected ? 'Disconnect' : 'Connect', + click: connected ? disconnect : connect, + enabled: !busy + }); + + template.push( + { + label: 'Logout', + click: logout + }, + { role: 'quit' } + ); + tray.setContextMenu(Menu.buildFromTemplate(template)); } function setStatus(status: Record) { @@ -206,23 +225,7 @@ function showWindow() { function createTray() { const icon = nativeImage.createFromPath(resourcePath('VynteIcon.png')).resize({ width: 18, height: 18 }); tray = new Tray(icon); - const contextMenu = Menu.buildFromTemplate([ - { - label: 'Connect', - click: connect, - }, - { - label: 'Disconnect', - click: disconnect, - }, - { - label: 'Logout', - click: logout - }, - { role: 'quit' } - ]); - tray.setContextMenu(contextMenu); - tray.setToolTip(APP_NAME); + updateTray(); tray.on('click', showWindow); }