1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/start-main-application/lens-window/application-window/create-lens-window.injectable.ts
Janne Savolainen 55d1471c10
Consolidate code for application window
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-05-11 08:28:08 +03:00

90 lines
2.5 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { SendToViewArgs } from "./lens-window-injection-token";
import createElectronWindowForInjectable from "./create-electron-window-for.injectable";
export interface LensWindow {
show: () => void;
close: () => void;
send: (args: SendToViewArgs) => void;
}
interface LensWindowConfiguration {
id: string;
title: string;
defaultHeight: number;
defaultWidth: number;
getContentUrl: () => string;
resizable: boolean;
windowFrameUtilitiesAreShown: boolean;
centered: boolean;
beforeOpen?: () => Promise<void>;
onFocus?: () => void;
onBlur?: () => void;
onDomReady?: () => void;
}
const createLensWindowInjectable = getInjectable({
id: "create-lens-window",
instantiate:
(di) =>
(configuration: LensWindowConfiguration) => {
let browserWindow: LensWindow = null;
const createElectronWindow = di.inject(createElectronWindowForInjectable)(
{
id: configuration.id,
title: configuration.title,
defaultHeight: configuration.defaultHeight,
defaultWidth: configuration.defaultWidth,
getContentUrl: configuration.getContentUrl,
resizable: configuration.resizable,
windowFrameUtilitiesAreShown: configuration.windowFrameUtilitiesAreShown,
centered: configuration.centered,
onFocus: configuration.onFocus,
onBlur: configuration.onBlur,
onDomReady: configuration.onDomReady,
beforeOpen: configuration.beforeOpen,
onClose: () => {
browserWindow = null;
},
},
);
return {
get visible() {
return !!this.browserWindow;
},
show: async () => {
if (!browserWindow) {
browserWindow = await createElectronWindow();
}
browserWindow.show();
},
close: () => {
browserWindow?.close();
browserWindow = null;
},
send: async (args: SendToViewArgs) => {
if (!browserWindow) {
browserWindow = await createElectronWindow();
}
return browserWindow.send(args);
},
};
},
});
export default createLensWindowInjectable;