1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

removed redundant tray window

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-10-12 13:44:12 +03:00
parent be6f0180e1
commit addc220b9f
2 changed files with 18 additions and 58 deletions

View File

@ -80,9 +80,10 @@ export function createTrayMenu(windowManager: WindowManager): Menu {
return Menu.buildFromTemplate([ return Menu.buildFromTemplate([
{ {
label: "About Lens", label: "About Lens",
click() { async click() {
// note: argument[1] (browserWindow) not available when app is not focused / hidden // 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", label: "Check for updates",
click() { async click() {
windowManager.runInContextWindow(async window => { const result = await AppUpdater.checkForUpdates();
const result = await AppUpdater.checkForUpdates(); const browserWindow = await windowManager.ensureMainWindow();
if (!result) { if (!result) {
dialog.showMessageBoxSync(window, { dialog.showMessageBoxSync(browserWindow, {
message: "No updates available", message: "No updates available",
type: "info", type: "info",
}) })
} }
})
}, },
}, },
]); ]);

View File

@ -2,7 +2,7 @@ import type { ClusterId } from "../common/cluster-store";
import { clusterStore } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store";
import { userStore } from "../common/user-store"; import { userStore } from "../common/user-store";
import { observable, reaction } from "mobx"; 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 windowStateKeeper from "electron-window-state"
import { initMenu } from "./menu"; import { initMenu } from "./menu";
import { initTray } from "./tray"; import { initTray } from "./tray";
@ -10,7 +10,6 @@ import { initTray } from "./tray";
export class WindowManager { export class WindowManager {
protected mainWindow: BrowserWindow; protected mainWindow: BrowserWindow;
protected splashWindow: BrowserWindow; protected splashWindow: BrowserWindow;
protected trayWindow: BrowserWindow;
protected windowState: windowStateKeeper.State; protected windowState: windowStateKeeper.State;
protected disposers: Record<string, Function> = {}; protected disposers: Record<string, Function> = {};
@ -63,14 +62,13 @@ export class WindowManager {
this.windowState.unmanage(); this.windowState.unmanage();
this.mainWindow = null; this.mainWindow = null;
this.splashWindow = null; this.splashWindow = null;
this.trayWindow = null;
}) })
} }
try { try {
if (showSplash) await this.showSplash(); if (showSplash) await this.showSplash();
await this.mainWindow.loadURL(this.mainUrl); await this.mainWindow.loadURL(this.mainUrl);
this.mainWindow.show(); this.mainWindow.show();
this.splashWindow.hide() this.splashWindow?.hide();
} catch (err) { } catch (err) {
dialog.showErrorBox("ERROR!", err.toString()) dialog.showErrorBox("ERROR!", err.toString())
} }
@ -83,12 +81,9 @@ export class WindowManager {
protected async initTray() { protected async initTray() {
this.disposers.trayAutoBind = reaction(() => userStore.preferences.trayEnabled, async isEnabled => { this.disposers.trayAutoBind = reaction(() => userStore.preferences.trayEnabled, async isEnabled => {
if (isEnabled) { if (isEnabled) {
this.ensureTrayWindow();
this.disposers.trayAutoUpdater = await initTray(this); this.disposers.trayAutoUpdater = await initTray(this);
} else if (this.disposers.trayAutoUpdater) { } else if (this.disposers.trayAutoUpdater) {
this.disposers.trayAutoUpdater(); this.disposers.trayAutoUpdater();
this.trayWindow.destroy();
this.trayWindow = null;
} }
}, { }, {
fireImmediately: true fireImmediately: true
@ -102,45 +97,10 @@ export class WindowManager {
}); });
} }
async ensureMainWindow({ bringToTop = true, showSplash = true } = {}) { async ensureMainWindow(): Promise<BrowserWindow> {
if (!this.mainWindow) { if (!this.mainWindow) await this.initMainWindow();
await this.initMainWindow(showSplash); this.mainWindow.show();
} return this.mainWindow;
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<any>) {
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();
}
} }
sendToView({ channel, frameId, data = [] }: { channel: string, frameId?: number, data?: any[] }) { sendToView({ channel, frameId, data = [] }: { channel: string, frameId?: number, data?: any[] }) {