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

Wait for LensApp to report that the view has loaded before showing main window (#2597)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-07 08:39:38 -04:00 committed by GitHub
parent 88de530913
commit f3b38d36c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 19 deletions

View File

@ -188,7 +188,7 @@ app.on("ready", async () => {
installDeveloperTools(); installDeveloperTools();
if (!startHidden) { if (!startHidden) {
windowManager.initMainWindow(); windowManager.ensureMainWindow();
} }
ipcMain.on(IpcRendererNavigationEvents.LOADED, () => { ipcMain.on(IpcRendererNavigationEvents.LOADED, () => {
@ -236,7 +236,7 @@ app.on("activate", (event, hasVisibleWindows) => {
logger.info("APP:ACTIVATE", { hasVisibleWindows }); logger.info("APP:ACTIVATE", { hasVisibleWindows });
if (!hasVisibleWindows) { if (!hasVisibleWindows) {
WindowManager.getInstance(false)?.initMainWindow(false); WindowManager.getInstance(false)?.ensureMainWindow(false);
} }
}); });

View File

@ -21,19 +21,23 @@
import type { ClusterId } from "../common/cluster-store"; import type { ClusterId } from "../common/cluster-store";
import { makeObservable, observable } from "mobx"; import { makeObservable, observable } from "mobx";
import { app, BrowserWindow, dialog, shell, webContents } from "electron"; import { app, BrowserWindow, dialog, ipcMain, shell, webContents } from "electron";
import windowStateKeeper from "electron-window-state"; import windowStateKeeper from "electron-window-state";
import { appEventBus } from "../common/event-bus"; import { appEventBus } from "../common/event-bus";
import { subscribeToBroadcast } from "../common/ipc"; import { subscribeToBroadcast } from "../common/ipc";
import { initMenu } from "./menu"; import { initMenu } from "./menu";
import { initTray } from "./tray"; import { initTray } from "./tray";
import { Singleton } from "../common/utils"; import { delay, Singleton } from "../common/utils";
import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames";
import { IpcRendererNavigationEvents } from "../renderer/navigation/events"; import { IpcRendererNavigationEvents } from "../renderer/navigation/events";
import logger from "./logger"; import logger from "./logger";
import { productName } from "../common/vars"; import { productName } from "../common/vars";
import { LensProxy } from "./proxy/lens-proxy"; import { LensProxy } from "./proxy/lens-proxy";
function isHideable(window: BrowserWindow | null): boolean {
return Boolean(window && !window.isDestroyed());
}
export class WindowManager extends Singleton { export class WindowManager extends Singleton {
protected mainWindow: BrowserWindow; protected mainWindow: BrowserWindow;
protected splashWindow: BrowserWindow; protected splashWindow: BrowserWindow;
@ -54,7 +58,7 @@ export class WindowManager extends Singleton {
return `http://localhost:${LensProxy.getInstance().port}`; return `http://localhost:${LensProxy.getInstance().port}`;
} }
async initMainWindow(showSplash = true) { private async initMainWindow(showSplash: boolean) {
// Manage main window size and position with state persistence // Manage main window size and position with state persistence
if (!this.windowState) { if (!this.windowState) {
this.windowState = windowStateKeeper({ this.windowState = windowStateKeeper({
@ -120,13 +124,8 @@ export class WindowManager extends Singleton {
if (showSplash) await this.showSplash(); if (showSplash) await this.showSplash();
logger.info(`[WINDOW-MANAGER]: Loading Main window from url: ${this.mainUrl} ...`); logger.info(`[WINDOW-MANAGER]: Loading Main window from url: ${this.mainUrl} ...`);
await this.mainWindow.loadURL(this.mainUrl); await this.mainWindow.loadURL(this.mainUrl);
this.mainWindow.show();
this.splashWindow?.close();
setTimeout(() => {
appEventBus.emit({ name: "app", action: "start" });
}, 1000);
} catch (error) { } catch (error) {
logger.error("Showing main window failed", { error }); logger.error("Loading main window failed", { error });
dialog.showErrorBox("ERROR!", error.toString()); dialog.showErrorBox("ERROR!", error.toString());
} }
} }
@ -146,9 +145,32 @@ export class WindowManager extends Singleton {
}); });
} }
async ensureMainWindow(): Promise<BrowserWindow> { async ensureMainWindow(showSplash = true): Promise<BrowserWindow> {
if (!this.mainWindow) await this.initMainWindow(); // This needs to be ready to hear the IPC message before the window is loaded
this.mainWindow.show(); let viewHasLoaded = Promise.resolve();
if (!this.mainWindow) {
viewHasLoaded = new Promise<void>(resolve => {
ipcMain.once(IpcRendererNavigationEvents.LOADED, () => resolve());
});
await this.initMainWindow(showSplash);
}
try {
await viewHasLoaded;
await delay(50); // wait just a bit longer to let the first round of rendering happen
logger.info("[WINDOW-MANAGER]: Main window has reported that it has loaded");
this.mainWindow.show();
this.splashWindow?.close();
this.splashWindow = undefined;
setTimeout(() => {
appEventBus.emit({ name: "app", action: "start" });
}, 1000);
} catch (error) {
logger.error(`Showing main window failed: ${error.stack || error}`);
dialog.showErrorBox("ERROR!", error.toString());
}
return this.mainWindow; return this.mainWindow;
} }
@ -206,8 +228,13 @@ export class WindowManager extends Singleton {
} }
hide() { hide() {
if (this.mainWindow && !this.mainWindow.isDestroyed()) this.mainWindow.hide(); if (isHideable(this.mainWindow)) {
if (this.splashWindow && !this.splashWindow.isDestroyed()) this.splashWindow.hide(); this.mainWindow.hide();
}
if (isHideable(this.splashWindow)) {
this.splashWindow.hide();
}
} }
destroy() { destroy() {

View File

@ -49,6 +49,9 @@ export class LensApp extends React.Component {
window.addEventListener("online", () => broadcastMessage("network:online")); window.addEventListener("online", () => broadcastMessage("network:online"));
registerIpcHandlers(); registerIpcHandlers();
}
componentDidMount() {
ipcRenderer.send(IpcRendererNavigationEvents.LOADED); ipcRenderer.send(IpcRendererNavigationEvents.LOADED);
} }
@ -57,11 +60,11 @@ export class LensApp extends React.Component {
<Router history={history}> <Router history={history}>
<ErrorBoundary> <ErrorBoundary>
<Switch> <Switch>
<Route component={ClusterManager}/> <Route component={ClusterManager} />
</Switch> </Switch>
</ErrorBoundary> </ErrorBoundary>
<Notifications/> <Notifications />
<ConfirmDialog/> <ConfirmDialog />
<CommandContainer /> <CommandContainer />
</Router> </Router>
); );