diff --git a/src/common/user-store.ts b/src/common/user-store.ts index f7d3ade005..1a81c981f7 100644 --- a/src/common/user-store.ts +++ b/src/common/user-store.ts @@ -27,6 +27,7 @@ export interface UserPreferences { downloadKubectlBinaries?: boolean; downloadBinariesPath?: string; kubectlBinariesPath?: string; + trayEnabled?: boolean; } export class UserStore extends BaseStore { @@ -59,6 +60,7 @@ export class UserStore extends BaseStore { colorTheme: UserStore.defaultTheme, downloadMirror: "default", downloadKubectlBinaries: true, // Download kubectl binaries matching cluster version + trayEnabled: true, }; get isNewVersion() { diff --git a/src/main/menu.ts b/src/main/menu.ts index 0f12192c0b..9fab6947f1 100644 --- a/src/main/menu.ts +++ b/src/main/menu.ts @@ -9,7 +9,7 @@ import { clusterSettingsURL } from "../renderer/components/+cluster-settings/clu import logger from "./logger"; export function initMenu(windowManager: WindowManager) { - autorun(() => buildMenu(windowManager), { + return autorun(() => buildMenu(windowManager), { delay: 100 }); } diff --git a/src/main/tray.ts b/src/main/tray.ts index 1e635a2fdb..b3d4782c25 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -21,10 +21,14 @@ export const trayIcon = isDevelopment : path.resolve(__static, "logo.svg") // electron-builder's extraResources export function initTray(windowManager: WindowManager) { - return autorun(() => { + const dispose = autorun(() => { const menu = createTrayMenu(windowManager); buildTray(menu); }) + return () => { + tray?.destroy(); + dispose(); + } } export async function buildTray(menu: Menu) { @@ -36,10 +40,7 @@ export async function buildTray(menu: Menu) { height: iconSize }); - if (tray) { - tray.destroy(); // remove old tray on update - } - + tray?.destroy(); // remove previous tray first tray = new Tray(icon) tray.setToolTip(packageInfo.description) tray.setIgnoreDoubleClickEvents(true); diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 01a4b1d898..f5235e5771 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -1,8 +1,9 @@ import type { ClusterId } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store"; +import { userStore } from "../common/user-store"; +import { observable, reaction } from "mobx"; import { BrowserWindow, dialog, ipcMain, shell, webContents } from "electron" import windowStateKeeper from "electron-window-state" -import { observable } from "mobx"; import { initMenu } from "./menu"; import { initTray } from "./tray"; @@ -10,6 +11,7 @@ export class WindowManager { public mainView: BrowserWindow; protected splashWindow: BrowserWindow; protected windowState: windowStateKeeper.State; + protected disposers: Record = {}; @observable activeClusterId: ClusterId; @@ -52,9 +54,17 @@ export class WindowManager { this.initMenus(); } - async initMenus() { - initMenu(this); - initTray(this); + protected async initMenus() { + this.disposers.menuAutoUpdater = initMenu(this); + this.disposers.trayAutoBind = reaction(() => userStore.preferences.trayEnabled, isEnabled => { + if (isEnabled) { + this.disposers.trayAutoUpdater = initTray(this); + } else { + this.disposers?.trayAutoUpdater(); + } + }, { + fireImmediately: true + }); } bringToTop() { @@ -120,5 +130,9 @@ export class WindowManager { this.windowState.unmanage(); this.splashWindow.destroy(); this.mainView.destroy(); + Object.entries(this.disposers).forEach(([name, dispose]) => { + dispose(); + delete this.disposers[name] + }); } } diff --git a/src/renderer/components/+preferences/preferences.tsx b/src/renderer/components/+preferences/preferences.tsx index a1b86c19cd..8b98750dac 100644 --- a/src/renderer/components/+preferences/preferences.tsx +++ b/src/renderer/components/+preferences/preferences.tsx @@ -194,6 +194,16 @@ export class Preferences extends React.Component { Telemetry & usage data is collected to continuously improve the Lens experience. + +

Tray icon

+ Enable tray icon} + value={preferences.trayEnabled} + onChange={v => preferences.trayEnabled = v} + /> + + Adds OS-level tray icon and menu to get quick access to Lens + );