ldc-showroom/electron/BrowserWinHandler.ts

122 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2024-01-08 16:43:46 +01:00
/* eslint-disable */
import { BrowserWindow, app, type BrowserWindowConstructorOptions } from 'electron';
2024-01-19 09:31:01 +01:00
import type serve from "electron-serve";
import { EventEmitter } from 'events';
2024-01-08 16:43:46 +01:00
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) {
2024-01-08 16:43:46 +01:00
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({
2024-01-08 16:43:46 +01:00
...this.options,
webPreferences: {
...this.options.webPreferences,
2024-01-18 11:19:17 +01:00
// webSecurity: isProduction, // disable on dev to allow loading local resources
// nodeIntegration: false,
contextIsolation: true,
nodeIntegrationInWorker: true,
nodeIntegration: true,
webSecurity: false,
2024-01-08 16:43:46 +01:00
}
})
2024-01-08 16:43:46 +01:00
this.browserWindow.on('closed', () => {
// Dereference the window object
this.browserWindow = null
2024-01-08 16:43:46 +01:00
})
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);
2024-01-08 16:43:46 +01:00
}
_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)
})
}
2024-01-19 09:31:01 +01:00
async loadMainPage(loadUrl: serve.loadURL) {
2024-01-08 16:43:46 +01:00
if (!this.browserWindow) return Promise.reject(new Error('The page could not be loaded before win \'created\' event'))
2024-01-18 11:19:17 +01:00
if (process.env.VITE_DEV_SERVER_URL) {
this.browserWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
this.browserWindow.webContents.openDevTools();
} else {
2024-01-19 09:31:01 +01:00
// A TESTER https://github.com/sindresorhus/electron-serve
// this.browserWindow.loadFile(path.join(publicPath, "index.html"), {
// hash: '/'
// });
// this.browserWindow.loadURL("app://./index.html#/");
2024-01-19 09:31:01 +01:00
loadUrl(this.browserWindow);
// this.browserWindow.loadURL(url.format({
// pathname: 'index.html', /* Attention here: origin is path.join(__dirname, 'index.html') */
// protocol: 'file'
// }));
2024-01-18 11:19:17 +01:00
}
2024-01-08 16:43:46 +01:00
}
/**
* 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();
}
2024-01-08 16:43:46 +01:00
}