Archived
feat: Switch between connect/disconnect based on current state
This commit is contained in:
+21
-18
@@ -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<string, any>) {
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user