"use strict"; const {app, BrowserWindow, ipcMain, globalShortcut} = require("electron"); const path = require("path"); const {isDevelopment} = require("./Util"); class WindowManager { mainWindow; windows; constructor(parent) { this.app = parent; this.mainWindow = null; this.windows = {}; app.whenReady().then(() => this.init()); ipcMain.on("WindowManager::openFileDialog", () => this.app.openFile()); ipcMain.on("WindowManager::openFile", (_, path) => this.app.openFile(path)); ipcMain.handle("WindowManager::resize", (_, height) => this.windows.settings.setSize(800, height + (process.platform === "win32" ? 50 : 0), true)); ipcMain.handle("WindowManager::presentFullscreen", (_, fullscreen) => this.windows.main.setFullScreen(fullscreen)); } init() { this.mainWindow = WindowManager._CreateWindow({ fullscreen : false, fullscreenable : true }); this.windows.settings = WindowManager._CreateWindow({ height : 300, resizable : false, fullscreenable : false, parent : this.mainWindow, show : false }, this.mainWindow, false); // on windows and linux we need to hide the main menu in the settings // window, because it would look odd. if (["win32", "linux"].includes(process.platform)) { this.windows.settings.removeMenu(); } this.windows.settings.on("close", event => { event.preventDefault(); this.windows.settings.hide(); }); if (isDevelopment()) { globalShortcut.register("CommandOrControl+I", () => { this.mainWindow.toggleDevTools(); this.windows.settings.toggleDevTools(); }); globalShortcut.register("CommandOrControl+R", () => { this.mainWindow.reload(); }); } if (isDevelopment()) { this.mainWindow.loadURL("http://localhost:3000"); this.windows.settings.loadURL("http://localhost:3000/#/settings"); } else { this.mainWindow.loadURL(`file://${__dirname}/ui/build/index.html`); this.windows.settings.loadURL(`file://${__dirname}/ui/build/index.html#/settings`); } } static _CreateWindow(options = null, parent = null, show = true) { const windowOptions = { width : 800, height : 600, show : false, devTools : isDevelopment(), titleBarStyle : "hiddenInset", autoHideMenuBar : process.platform === "win32", webPreferences : { contextIsolation : true, preload : path.join(__dirname, "..", "contextAPI.js") }, parent, ...options }; if (!parent) delete windowOptions.parent; const window = new BrowserWindow(windowOptions); if (show) window.once("ready-to-show", () => window.show()); return window; } } module.exports = WindowManager;