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
Iku-turso a0a181702d
Rename interface for honesty
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-06-15 08:40:17 +03:00

74 lines
2.1 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 { LensWindow, SendToViewArgs } from "./lens-window-injection-token";
import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window-for.injectable";
import createElectronWindowForInjectable from "./create-electron-window-for.injectable";
export interface ElectronWindow {
show: () => void;
close: () => void;
send: (args: SendToViewArgs) => void;
}
export interface LensWindowConfiguration {
id: string;
title: string;
defaultHeight: number;
defaultWidth: number;
getContentSource: () => ContentSource;
resizable: boolean;
windowFrameUtilitiesAreShown: boolean;
centered: boolean;
titleBarStyle?: ElectronWindowTitleBarStyle;
beforeOpen?: () => Promise<void>;
onFocus?: () => void;
onBlur?: () => void;
onDomReady?: () => void;
}
const createLensWindowInjectable = getInjectable({
id: "create-lens-window",
instantiate: (di) => {
const createElectronWindowFor = di.inject(createElectronWindowForInjectable);
return (configuration: LensWindowConfiguration): LensWindow => {
let browserWindow: ElectronWindow | undefined;
const createElectronWindow = createElectronWindowFor({
...configuration,
onClose: () => browserWindow = undefined,
});
return {
get visible() {
return !!browserWindow;
},
show: async () => {
if (!browserWindow) {
browserWindow = await createElectronWindow();
}
browserWindow.show();
},
close: () => {
browserWindow?.close();
browserWindow = undefined;
},
send: (args: SendToViewArgs) => {
if (!browserWindow) {
throw new Error(`Tried to send message to window "${configuration.id}" but the window was closed`);
}
return browserWindow.send(args);
},
};
};
},
});
export default createLensWindowInjectable;