feat: migrate from electron-webpack to electron-forge
Electron upgrade : 10.1.3 => 13.1.7 Angular upgrade : 10.1.3 => 12.1.2
This commit is contained in:
65
workspaces/electron-app/main/components/app.ts
Normal file
65
workspaces/electron-app/main/components/app.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { app, BrowserWindow, shell } from 'electron';
|
||||
import { Window } from './window';
|
||||
|
||||
export class App {
|
||||
private static _wrapper: Window;
|
||||
|
||||
public static launch() {
|
||||
app.on('window-all-closed', App.quit);
|
||||
app.on('activate', App.start);
|
||||
app.on('ready', App.start);
|
||||
|
||||
// Fix warning by applying electron new default value for this property
|
||||
// Further details : https://github.com/electron/electron/issues/18397
|
||||
app.allowRendererProcessReuse = true;
|
||||
|
||||
// Limit navigation and open external links in default browser
|
||||
app.on('web-contents-created', App.openExternalLinksInDefaultBrowser);
|
||||
}
|
||||
|
||||
public static get window(): BrowserWindow | any {
|
||||
return this._wrapper ? this._wrapper.window : null;
|
||||
}
|
||||
|
||||
private static start() {
|
||||
// On MacOS it is common to re-create a window from app even after all windows have been closed
|
||||
if (!App.window) {
|
||||
App._wrapper = new Window();
|
||||
}
|
||||
}
|
||||
|
||||
private static quit() {
|
||||
// On MacOS it is common for applications to stay open until the user explicitly quits
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
|
||||
private static openExternalLinksInDefaultBrowser = (
|
||||
event: Electron.Event,
|
||||
contents: Electron.WebContents
|
||||
) => {
|
||||
// Disabling creation of new windows
|
||||
contents.on(
|
||||
'new-window',
|
||||
(event: Electron.Event, navigationUrl: string) => {
|
||||
// Blocking this event from loading in current app
|
||||
event.preventDefault();
|
||||
// Telling the user platform to open this event's url in the default browser
|
||||
shell.openExternal(navigationUrl);
|
||||
}
|
||||
);
|
||||
|
||||
// Limiting navigation
|
||||
contents.on(
|
||||
'will-navigate',
|
||||
(event: Electron.Event, navigationUrl: string) => {
|
||||
const parsedUrl = new URL(navigationUrl);
|
||||
// Allowing local navigation only
|
||||
if (parsedUrl.origin !== 'http://localhost:4200') {
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
116
workspaces/electron-app/main/components/window.ts
Normal file
116
workspaces/electron-app/main/components/window.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { app, BrowserWindow, ipcMain, nativeImage } from "electron";
|
||||
import * as path from "path";
|
||||
import * as url from "url";
|
||||
import { AbstractService } from "../services/abstract-service";
|
||||
import { MultiplesService } from "../services/multiples-service";
|
||||
import { Logger } from "../utils/logger";
|
||||
|
||||
declare const global: any;
|
||||
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
|
||||
|
||||
export class Window {
|
||||
private _window: BrowserWindow | any;
|
||||
|
||||
constructor() {
|
||||
this.createWindow();
|
||||
this.loadRenderer();
|
||||
this.registerService(MultiplesService);
|
||||
}
|
||||
|
||||
private createWindow(): void {
|
||||
this._window = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
backgroundColor: "#FFFFFF",
|
||||
// FIXME
|
||||
// icon: this.loadIcon(),
|
||||
webPreferences: {
|
||||
// Default behavior in Electron since 5, that
|
||||
// limits the powers granted to remote content
|
||||
// except in e2e test when those powers are required by Spectron
|
||||
nodeIntegration: global.gConfig.isNodeIntegration,
|
||||
// Isolate window context to protect against prototype pollution
|
||||
// except in e2e test when that access is required by Spectron
|
||||
contextIsolation: global.gConfig.isContextIsolation,
|
||||
// Ensure that JS values can't unsafely cross between worlds
|
||||
// when using contextIsolation
|
||||
worldSafeExecuteJavaScript: global.gConfig.isContextIsolation,
|
||||
// Disable the remote module to enhance security
|
||||
// except in e2e test when that access is required by Spectron
|
||||
enableRemoteModule: global.gConfig.isEnableRemoteModule,
|
||||
// Use a preload script to enhance security
|
||||
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private loadIcon(): Electron.NativeImage {
|
||||
let iconObj = null;
|
||||
if (global.gConfig.isIconAvailable) {
|
||||
const iconPath = path.join(__dirname, "icons/icon.png");
|
||||
Logger.debug("Icon Path", iconPath);
|
||||
iconObj = nativeImage.createFromPath(iconPath);
|
||||
// Change dock icon on MacOS
|
||||
if (iconObj && process.platform === "darwin") {
|
||||
app.dock.setIcon(iconObj);
|
||||
}
|
||||
}
|
||||
return iconObj;
|
||||
}
|
||||
|
||||
private loadRenderer(): void {
|
||||
if (global.gConfig.config_id === "development") {
|
||||
// Dev mode, take advantage of the live reload by loading local URL
|
||||
this.window.loadURL(`http://localhost:4200`);
|
||||
} else {
|
||||
// Else mode, we simply load angular bundle
|
||||
const indexPath = url.format({
|
||||
pathname: path.join(__dirname, `../renderer/angular_window/index.html`),
|
||||
protocol: "file:",
|
||||
slashes: true,
|
||||
});
|
||||
this.window.loadURL(indexPath);
|
||||
}
|
||||
|
||||
if (global.gConfig.isOpenDevTools) {
|
||||
this.openDevTools();
|
||||
}
|
||||
|
||||
// When the window is closed`
|
||||
this._window.on("closed", () => {
|
||||
// Remove IPC Main listeners
|
||||
ipcMain.removeAllListeners();
|
||||
// Delete current reference
|
||||
delete this._window;
|
||||
});
|
||||
}
|
||||
|
||||
private openDevTools(): void {
|
||||
this._window.webContents.openDevTools();
|
||||
this._window.webContents.on("devtools-opened", () => {
|
||||
this._window.focus();
|
||||
setImmediate(() => {
|
||||
this._window.focus();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private registerService(AnyService: typeof AbstractService) {
|
||||
const service = new AnyService();
|
||||
ipcMain.on(service.receptionChannel(), async (event, ...args) => {
|
||||
// Handling input
|
||||
Logger.debug(`Received [${service.receptionChannel()}]`, args);
|
||||
const data = await service.process(...args);
|
||||
|
||||
// Handling output
|
||||
if (service.sendingChannel()) {
|
||||
Logger.debug(`Sent [${service.sendingChannel()}]`, data);
|
||||
this._window.webContents.send(service.sendingChannel(), ...data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public get window(): BrowserWindow | any {
|
||||
return this._window;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user