ldc-showroom/electron/BrowserWinHandler.ts
2024-01-19 09:31:01 +01:00

121 lines
4.2 KiB
TypeScript

/* eslint-disable */
import { BrowserWindow, app, type BrowserWindowConstructorOptions } from 'electron';
import type serve from "electron-serve";
import { EventEmitter } from 'events';
export default class BrowserWinHandler {
private _eventEmitter: EventEmitter;
private allowRecreate: boolean;
private options: Electron.BrowserWindowConstructorOptions;
private browserWindow: BrowserWindow|null;
/**
*
* @param options browser window options
* @param allowRecreate
*/
constructor (options: BrowserWindowConstructorOptions, allowRecreate: boolean = true) {
this._eventEmitter = new EventEmitter()
this.allowRecreate = allowRecreate
this.options = options
this.browserWindow = null
this._createInstance()
}
_createInstance () {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
if (app.isReady()) this._create()
else {
app.once('ready', () => {
this._create()
})
}
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!this.allowRecreate) return
app.on('activate', () => this._recreate())
}
_create () {
this.browserWindow = new BrowserWindow({
...this.options,
webPreferences: {
...this.options.webPreferences,
// webSecurity: isProduction, // disable on dev to allow loading local resources
// nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
nodeIntegration: true,
webSecurity: false,
}
})
this.browserWindow.on('closed', () => {
// Dereference the window object
this.browserWindow = null
})
this._eventEmitter.emit('created');
this.browserWindow.webContents.on('before-input-event', (_, input) => {
if (input.type === 'keyDown' && input.key === 'F12' && this.browserWindow) {
this.browserWindow.webContents.openDevTools();
}
});
this.browserWindow.removeMenu();
this.browserWindow.setMenu(null);
}
_recreate () {
if (this.browserWindow === null) this._create()
}
onCreated (callback: (browserWindow: BrowserWindow|null) => void) {
if (this.browserWindow !== null) return callback(this.browserWindow);
this._eventEmitter.once('created', () => {
callback(this.browserWindow)
})
}
async loadMainPage(loadUrl: serve.loadURL) {
if (!this.browserWindow) return Promise.reject(new Error('The page could not be loaded before win \'created\' event'))
if (process.env.VITE_DEV_SERVER_URL) {
this.browserWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
this.browserWindow.webContents.openDevTools();
} else {
// A TESTER https://github.com/sindresorhus/electron-serve
// this.browserWindow.loadFile(path.join(publicPath, "index.html"), {
// hash: '/'
// });
// this.browserWindow.loadURL("app://./index.html#/");
loadUrl(this.browserWindow);
// this.browserWindow.loadURL(url.format({
// pathname: 'index.html', /* Attention here: origin is path.join(__dirname, 'index.html') */
// protocol: 'file'
// }));
}
}
/**
* Open dev tools if ti can be
*/
openDevTool() {
const browserWindow = this.browserWindow;
if (!browserWindow) {
throw new Error("No window is loaded");
}
browserWindow.webContents.openDevTools();
}
quit() {
const browserWindow = this.browserWindow;
if (!browserWindow) {
throw new Error("No window is loaded");
}
browserWindow.on('closed', () => {
this.browserWindow = null
});
browserWindow.close();
}
}