cleaned up electron.main, optimized code

This commit is contained in:
Elvanos 2023-09-13 00:13:51 +02:00
parent 122d9f11f4
commit 2e722bd8af
6 changed files with 157 additions and 61 deletions

View file

@ -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)

View file

@ -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()
}
})
}

View file

@ -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))
}
}

View file

@ -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
})
}

View file

@ -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)
}

View file

@ -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 (_) {}
}