diff --git a/src/main/tray.ts b/src/main/tray.ts index b081f6d055..f77d7dddd9 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -80,9 +80,10 @@ export function createTrayMenu(windowManager: WindowManager): Menu { return Menu.buildFromTemplate([ { label: "About Lens", - click() { + async click() { // note: argument[1] (browserWindow) not available when app is not focused / hidden - windowManager.runInContextWindow(showAbout); + const browserWindow = await windowManager.ensureMainWindow(); + showAbout(browserWindow); }, }, { @@ -119,16 +120,15 @@ export function createTrayMenu(windowManager: WindowManager): Menu { }, { label: "Check for updates", - click() { - windowManager.runInContextWindow(async window => { - const result = await AppUpdater.checkForUpdates(); - if (!result) { - dialog.showMessageBoxSync(window, { - message: "No updates available", - type: "info", - }) - } - }) + async click() { + const result = await AppUpdater.checkForUpdates(); + const browserWindow = await windowManager.ensureMainWindow(); + if (!result) { + dialog.showMessageBoxSync(browserWindow, { + message: "No updates available", + type: "info", + }) + } }, }, ]); diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 3f5a4916e6..c0b6bba2db 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -2,7 +2,7 @@ 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 { app, BrowserWindow, dialog, ipcMain, shell, webContents } from "electron" +import { BrowserWindow, dialog, ipcMain, shell, webContents } from "electron" import windowStateKeeper from "electron-window-state" import { initMenu } from "./menu"; import { initTray } from "./tray"; @@ -10,7 +10,6 @@ import { initTray } from "./tray"; export class WindowManager { protected mainWindow: BrowserWindow; protected splashWindow: BrowserWindow; - protected trayWindow: BrowserWindow; protected windowState: windowStateKeeper.State; protected disposers: Record = {}; @@ -63,14 +62,13 @@ export class WindowManager { this.windowState.unmanage(); this.mainWindow = null; this.splashWindow = null; - this.trayWindow = null; }) } try { if (showSplash) await this.showSplash(); await this.mainWindow.loadURL(this.mainUrl); this.mainWindow.show(); - this.splashWindow.hide() + this.splashWindow?.hide(); } catch (err) { dialog.showErrorBox("ERROR!", err.toString()) } @@ -83,12 +81,9 @@ export class WindowManager { protected async initTray() { this.disposers.trayAutoBind = reaction(() => userStore.preferences.trayEnabled, async isEnabled => { if (isEnabled) { - this.ensureTrayWindow(); this.disposers.trayAutoUpdater = await initTray(this); } else if (this.disposers.trayAutoUpdater) { this.disposers.trayAutoUpdater(); - this.trayWindow.destroy(); - this.trayWindow = null; } }, { fireImmediately: true @@ -102,45 +97,10 @@ export class WindowManager { }); } - async ensureMainWindow({ bringToTop = true, showSplash = true } = {}) { - if (!this.mainWindow) { - await this.initMainWindow(showSplash); - } - if (bringToTop) { - this.mainWindow.show(); - } else { - this.mainWindow.hide(); - } - } - - ensureTrayWindow() { - if (!this.trayWindow) { - this.trayWindow = new BrowserWindow({ - show: false, - transparent: true, - titleBarStyle: "hidden", - trafficLightPosition: { - x: -10000, - y: -10000, - } - }); - } - } - - async runInContextWindow(callback: (window: BrowserWindow) => any | Promise) { - const isMainVisible = this.mainWindow?.isVisible(); // is open, but might be not on the top - if (isMainVisible) { - this.mainWindow.show(); - await callback(this.mainWindow); - } else { - this.ensureTrayWindow(); - this.mainWindow?.hide(); - this.trayWindow.show(); - await callback(this.trayWindow); - this.trayWindow.hide(); - this.mainWindow?.show(); - app.hide(); - } + async ensureMainWindow(): Promise { + if (!this.mainWindow) await this.initMainWindow(); + this.mainWindow.show(); + return this.mainWindow; } sendToView({ channel, frameId, data = [] }: { channel: string, frameId?: number, data?: any[] }) {