mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Roman <ixrock@gmail.com> Co-authored-by: Sebastian Malton <sebastian@malton.name> Co-authored-by: Sebastian Malton <smalton@mirantis.com> Co-authored-by: Lauri Nevala <lauri.nevala@gmail.com> Co-authored-by: Alex Andreev <alex.andreev.email@gmail.com>
97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
import type { ClusterId } from "../common/cluster-store";
|
|
import { BrowserWindow, dialog, ipcMain, shell, WebContents, webContents } from "electron"
|
|
import windowStateKeeper from "electron-window-state"
|
|
import { observable } from "mobx";
|
|
import { initMenu } from "./menu";
|
|
|
|
export class WindowManager {
|
|
protected mainView: BrowserWindow;
|
|
protected splashWindow: BrowserWindow;
|
|
protected windowState: windowStateKeeper.State;
|
|
|
|
@observable activeClusterId: ClusterId;
|
|
|
|
constructor(protected proxyPort: number) {
|
|
// Manage main window size and position with state persistence
|
|
this.windowState = windowStateKeeper({
|
|
defaultHeight: 900,
|
|
defaultWidth: 1440,
|
|
});
|
|
|
|
const { width, height, x, y } = this.windowState;
|
|
this.mainView = new BrowserWindow({
|
|
x, y, width, height,
|
|
show: false,
|
|
minWidth: 900,
|
|
minHeight: 760,
|
|
titleBarStyle: "hidden",
|
|
backgroundColor: "#1e2124",
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
nodeIntegrationInSubFrames: true,
|
|
enableRemoteModule: true,
|
|
},
|
|
});
|
|
this.windowState.manage(this.mainView);
|
|
|
|
// open external links in default browser (target=_blank, window.open)
|
|
this.mainView.webContents.on("new-window", (event, url) => {
|
|
event.preventDefault();
|
|
shell.openExternal(url);
|
|
});
|
|
|
|
// track visible cluster from ui
|
|
ipcMain.on("cluster-view:change", (event, clusterId: ClusterId) => {
|
|
this.activeClusterId = clusterId;
|
|
});
|
|
|
|
// load & show app
|
|
this.showMain();
|
|
initMenu(this);
|
|
}
|
|
|
|
navigate({ url, channel, frameId }: { url: string, channel: string, frameId?: number }) {
|
|
if (frameId) {
|
|
this.mainView.webContents.sendToFrame(frameId, channel, url);
|
|
} else {
|
|
this.mainView.webContents.send(channel, url);
|
|
}
|
|
}
|
|
|
|
async showMain() {
|
|
try {
|
|
await this.showSplash();
|
|
await this.mainView.loadURL(`http://localhost:${this.proxyPort}`)
|
|
this.mainView.show();
|
|
this.splashWindow.close();
|
|
} catch (err) {
|
|
dialog.showErrorBox("ERROR!", err.toString())
|
|
}
|
|
}
|
|
|
|
async showSplash() {
|
|
if (!this.splashWindow) {
|
|
this.splashWindow = new BrowserWindow({
|
|
width: 500,
|
|
height: 300,
|
|
backgroundColor: "#1e2124",
|
|
center: true,
|
|
frame: false,
|
|
resizable: false,
|
|
show: false,
|
|
webPreferences: {
|
|
nodeIntegration: true
|
|
}
|
|
});
|
|
await this.splashWindow.loadURL("static://splash.html");
|
|
}
|
|
this.splashWindow.show();
|
|
}
|
|
|
|
destroy() {
|
|
this.windowState.unmanage();
|
|
this.splashWindow.destroy();
|
|
this.mainView.destroy();
|
|
}
|
|
}
|