From 2e722bd8afec8a0bbe6642d302fc91043e5a4af7 Mon Sep 17 00:00:00 2001 From: Elvanos Date: Wed, 13 Sep 2023 00:13:51 +0200 Subject: [PATCH] cleaned up electron.main, optimized code --- src-electron/electron-main.ts | 81 +++++-------------- src-electron/mainScripts/appManagement.ts | 37 +++++++++ src-electron/mainScripts/fixAppName.ts | 27 +++++++ .../mainScripts/mainWindowCreation.ts | 42 ++++++++++ src-electron/mainScripts/tweaks.ts | 16 ++++ .../windowsDevToolsExtensionsFix.ts | 15 ++++ 6 files changed, 157 insertions(+), 61 deletions(-) create mode 100644 src-electron/mainScripts/appManagement.ts create mode 100644 src-electron/mainScripts/fixAppName.ts create mode 100644 src-electron/mainScripts/mainWindowCreation.ts create mode 100644 src-electron/mainScripts/tweaks.ts create mode 100644 src-electron/mainScripts/windowsDevToolsExtensionsFix.ts diff --git a/src-electron/electron-main.ts b/src-electron/electron-main.ts index 952e8d8..4ac8ddb 100644 --- a/src-electron/electron-main.ts +++ b/src-electron/electron-main.ts @@ -1,69 +1,28 @@ -import { app, BrowserWindow, Menu, nativeTheme } from 'electron' -import { initialize, enable } from '@electron/remote/main' -import path from 'path' -import os from 'os' +import { fixAppName } from 'src-electron/mainScripts/fixAppName' +import { windowsDevToolsExtensionsFix } from 'src-electron/mainScripts/windowsDevToolsExtensionsFix' +import { startApp, openAppWindowManager, closeAppManager } from 'app/src-electron/mainScripts/appManagement' +import { tweakMenuRemover, tweakRetriveOS } from 'src-electron/mainScripts/tweaks' -// needed in case process is undefined under Linux -const platform = process.platform || os.platform() +/** + * Determines what platform the app is running on + * - Needed in case process is undefined under Linux + */ +const platform = tweakRetriveOS() -try { - if (platform === 'win32' && nativeTheme.shouldUseDarkColors === true) { - require('fs').unlinkSync( - path.join(app.getPath('userData'), 'DevTools Extensions') - ) - } -} catch (_) {} +// Fix app name and connected pathing to it +fixAppName() -initialize() +// Fix Windows-only DevTools-bug concerning dark mode +windowsDevToolsExtensionsFix(platform) -let mainWindow: BrowserWindow | undefined - -function createWindow () { - /** - * Initial window options - */ - mainWindow = new BrowserWindow({ - useContentSize: true, - frame: false, - icon: path.resolve(__dirname, 'icons/icon.png'), // tray icon - webPreferences: { - sandbox: false, - contextIsolation: true, - // More info: https://v2.quasar.dev/quasar-cli-vite/developing-electron-apps/electron-preload-script - preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD) - } - }) - - enable(mainWindow.webContents) - - mainWindow.setMenu(null) - mainWindow.maximize() - - mainWindow.loadURL(process.env.APP_URL) - - if (process.env.DEBUGGING) { - // if on DEV or Production with debug enabled - mainWindow.webContents.openDevTools() - } - - mainWindow.on('closed', () => { - mainWindow = undefined - }) -} +// Start a singular app instance +startApp() // Performance improvement tweak -Menu.setApplicationMenu(null) +tweakMenuRemover() -app.whenReady().then(createWindow) +// Set up manager for opening a singular app window +openAppWindowManager() -app.on('window-all-closed', () => { - if (platform !== 'darwin') { - app.quit() - } -}) - -app.on('activate', () => { - if (mainWindow === undefined) { - createWindow() - } -}) +// Set up manager for closing app instance +closeAppManager(platform) diff --git a/src-electron/mainScripts/appManagement.ts b/src-electron/mainScripts/appManagement.ts new file mode 100644 index 0000000..4e2db2a --- /dev/null +++ b/src-electron/mainScripts/appManagement.ts @@ -0,0 +1,37 @@ +import { initialize } from '@electron/remote/main' +import { mainWindowCreation } from 'app/src-electron/mainScripts/mainWindowCreation' +import { app } from 'electron' + +/** + * Starts the app's Electron instance + */ +export const startApp = () => { + initialize() +} + +/** + * Opens the singular app's window and make sure it is the only one + */ +export const openAppWindowManager = () => { + // Create the app window in the normal way + app.whenReady().then(mainWindowCreation) + + // Create the app window, if it still doesn't exist yet + app.on('activate', () => { + if (app.requestSingleInstanceLock()) { + mainWindowCreation() + } + }) +} + +/** + * Closes the app's Electron instance when all windows are closed + */ +export const closeAppManager = (platform: string) => { + // Close app if we are on anything that isn't Mac + app.on('window-all-closed', () => { + if (platform !== 'darwin') { + app.quit() + } + }) +} diff --git a/src-electron/mainScripts/fixAppName.ts b/src-electron/mainScripts/fixAppName.ts new file mode 100644 index 0000000..6cb3340 --- /dev/null +++ b/src-electron/mainScripts/fixAppName.ts @@ -0,0 +1,27 @@ +import { app } from 'electron' +import path from 'path' +import packageJSON from '../../package.json' assert {type: 'json'} + +/** + * Determines if the app name will have "-dev" affix at the end for the appData. + */ +const determineAppName = () => { + if (process.env.DEBUGGING) { + return `${packageJSON.name}-dev` + } + + return packageJSON.name +} + +/** + * Fix the name and pathing of the app. + * - This function exists mostly due to dev-mode returning "Electron" instead of the app name. + */ +export const fixAppName = () => { + const appName = determineAppName() + if (appName) { + app.setName(appName) + const appData = app.getPath('appData') + app.setPath('userData', path.join(appData, appName)) + } +} diff --git a/src-electron/mainScripts/mainWindowCreation.ts b/src-electron/mainScripts/mainWindowCreation.ts new file mode 100644 index 0000000..9117592 --- /dev/null +++ b/src-electron/mainScripts/mainWindowCreation.ts @@ -0,0 +1,42 @@ +import { BrowserWindow } from 'electron' +import { enable } from '@electron/remote/main' +import path from 'path' + +/** + * Creates the main app window + */ +export const mainWindowCreation = () => { + /** + * Initial window options + */ + let appWindow: BrowserWindow | undefined = new BrowserWindow({ + useContentSize: true, + frame: false, + icon: path.resolve(__dirname, '../icons/icon.png'), + webPreferences: { + sandbox: false, + contextIsolation: true, + preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD) + } + }) + + // Enable actual webContents inside the created window + enable(appWindow.webContents) + + // Set the current window as empty and maximize it + appWindow.setMenu(null) + appWindow.maximize() + + // Load the basic app URL + appWindow.loadURL(process.env.APP_URL) + + // Open DevTools by default if the app is running in Dev mode or Production with debug enabled + if (process.env.DEBUGGING) { + appWindow.webContents.openDevTools() + } + + // Make sure the app window properly closes when it is closed in any way, shape or form + appWindow.on('closed', () => { + appWindow = undefined + }) +} diff --git a/src-electron/mainScripts/tweaks.ts b/src-electron/mainScripts/tweaks.ts new file mode 100644 index 0000000..960375d --- /dev/null +++ b/src-electron/mainScripts/tweaks.ts @@ -0,0 +1,16 @@ +import { Menu } from 'electron' +import os from 'os' + +/** + * Gets the current OS string-indetifier + */ +export const tweakRetriveOS = () => { + return process.platform || os.platform() +} + +/** + * Removes the default menu from all current and future windows the current app + */ +export const tweakMenuRemover = () => { + Menu.setApplicationMenu(null) +} diff --git a/src-electron/mainScripts/windowsDevToolsExtensionsFix.ts b/src-electron/mainScripts/windowsDevToolsExtensionsFix.ts new file mode 100644 index 0000000..376679e --- /dev/null +++ b/src-electron/mainScripts/windowsDevToolsExtensionsFix.ts @@ -0,0 +1,15 @@ +import { app, nativeTheme } from 'electron' +import path from 'path' + +/** + *Fix Windows related-bug with DevTools extensions and dark mode + */ +export const windowsDevToolsExtensionsFix = (platform: string) => { + try { + if (platform === 'win32' && nativeTheme.shouldUseDarkColors === true) { + require('fs').unlinkSync( + path.join(app.getPath('userData'), 'DevTools Extensions') + ) + } + } catch (_) {} +}