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

window-manager refactoring -- part 2

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-07-09 21:38:39 +03:00
parent cac4896517
commit 0e8187c2b0
2 changed files with 35 additions and 23 deletions

View File

@ -80,7 +80,7 @@ async function main() {
} }
// Events // Events
app.on("ready", main) app.on("ready", main);
app.on('window-all-closed', function () { app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar // On OS X it is common for applications and their menu bar
@ -93,8 +93,8 @@ app.on('window-all-closed', function () {
} }
}) })
app.on("activate", (event, hasVisibleWindows) => { app.on("activate", () => {
// todo: something logger.debug("app:activate");
}) })
app.on("will-quit", async (event) => { app.on("will-quit", async (event) => {

View File

@ -5,8 +5,17 @@ import type { ClusterId } from "../common/cluster-store";
import { clusterStore } from "../common/cluster-store"; import { clusterStore } from "../common/cluster-store";
export class WindowManager { export class WindowManager {
protected activeView: BrowserWindow;
protected views = new Map<ClusterId, BrowserWindow>(); protected views = new Map<ClusterId, BrowserWindow>();
protected disposers = this.bindReactions();
protected disposers = [
// auto-destroy view when cluster removed
reaction(() => clusterStore.removedClusters.toJS(), removedClusters => {
removedClusters.forEach(cluster => {
this.destroyView(cluster.id);
});
})
];
protected splashWindow = new BrowserWindow({ protected splashWindow = new BrowserWindow({
width: 500, width: 500,
@ -24,17 +33,6 @@ export class WindowManager {
defaultWidth: 1440, defaultWidth: 1440,
}); });
protected bindReactions() {
return [
// auto-destroy cluster-view when it's removed
reaction(() => clusterStore.removedClusters.toJS(), removedClusters => {
removedClusters.forEach(cluster => {
this.destroyView(cluster.id);
});
})
]
}
async showSplash() { async showSplash() {
await this.splashWindow.loadURL("static://splash.html") await this.splashWindow.loadURL("static://splash.html")
this.splashWindow.show(); this.splashWindow.show();
@ -44,20 +42,30 @@ export class WindowManager {
this.splashWindow.hide(); this.splashWindow.hide();
} }
protected async showView(clusterId: ClusterId) { // todo: smooth switching between windows/clusters
// todo: devtools + manage main-window size btw views
async showView(clusterId: ClusterId) {
const cluster = clusterStore.getById(clusterId); const cluster = clusterStore.getById(clusterId);
if (!cluster) { if (!cluster) {
throw new Error(`Can't load view for non-existing cluster="${clusterId}"`); throw new Error(`Can't load lens for non-existing cluster="${clusterId}"`);
} }
const activeView = this.activeView;
const view = this.getView(clusterId); const view = this.getView(clusterId);
const url = cluster.apiUrl.href; if (view !== activeView) {
if (view.webContents.getURL() !== url) { this.activeView = view;
await view.loadURL(url); if (activeView) {
view.setBounds(activeView.getBounds()); // update position from previous window
}
const url = cluster.apiUrl.href;
const isLoaded = url === view.webContents.getURL();
if (!isLoaded) {
await view.loadURL(url);
}
view.show();
} }
view.show();
} }
getView(clusterId: ClusterId) { protected getView(clusterId: ClusterId) {
let view = this.views.get(clusterId); let view = this.views.get(clusterId);
if (!view) { if (!view) {
view = new BrowserWindow({ view = new BrowserWindow({
@ -69,6 +77,7 @@ export class WindowManager {
backgroundColor: "#1e2124", backgroundColor: "#1e2124",
titleBarStyle: "hidden", titleBarStyle: "hidden",
webPreferences: { webPreferences: {
// partition: "lens-app", // todo: reuse session?
nodeIntegration: true, nodeIntegration: true,
}, },
}); });
@ -78,11 +87,12 @@ export class WindowManager {
shell.openExternal(url); shell.openExternal(url);
}); });
this.views.set(clusterId, view); this.views.set(clusterId, view);
this.windowState.manage(view);
} }
return view; return view;
} }
destroyView(clusterId: ClusterId) { protected destroyView(clusterId: ClusterId) {
const view = this.views.get(clusterId); const view = this.views.get(clusterId);
if (view) { if (view) {
view.destroy(); view.destroy();
@ -91,11 +101,13 @@ export class WindowManager {
} }
destroy() { destroy() {
this.windowState.unmanage();
this.disposers.forEach(dispose => dispose()); this.disposers.forEach(dispose => dispose());
this.disposers.length = 0; this.disposers.length = 0;
this.views.forEach(view => view.destroy()); this.views.forEach(view => view.destroy());
this.views.clear(); this.views.clear();
this.splashWindow.destroy(); this.splashWindow.destroy();
this.splashWindow = null; this.splashWindow = null;
this.activeView = null;
} }
} }