mirror of
https://github.com/Elvanos/fantasia-archive.git
synced 2024-06-17 01:34:53 +12:00
cleaned up electron.main, optimized code
This commit is contained in:
parent
122d9f11f4
commit
2e722bd8af
|
@ -1,69 +1,28 @@
|
||||||
import { app, BrowserWindow, Menu, nativeTheme } from 'electron'
|
import { fixAppName } from 'src-electron/mainScripts/fixAppName'
|
||||||
import { initialize, enable } from '@electron/remote/main'
|
import { windowsDevToolsExtensionsFix } from 'src-electron/mainScripts/windowsDevToolsExtensionsFix'
|
||||||
import path from 'path'
|
import { startApp, openAppWindowManager, closeAppManager } from 'app/src-electron/mainScripts/appManagement'
|
||||||
import os from 'os'
|
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 {
|
// Fix app name and connected pathing to it
|
||||||
if (platform === 'win32' && nativeTheme.shouldUseDarkColors === true) {
|
fixAppName()
|
||||||
require('fs').unlinkSync(
|
|
||||||
path.join(app.getPath('userData'), 'DevTools Extensions')
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
|
|
||||||
initialize()
|
// Fix Windows-only DevTools-bug concerning dark mode
|
||||||
|
windowsDevToolsExtensionsFix(platform)
|
||||||
|
|
||||||
let mainWindow: BrowserWindow | undefined
|
// Start a singular app instance
|
||||||
|
startApp()
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Performance improvement tweak
|
// 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', () => {
|
// Set up manager for closing app instance
|
||||||
if (platform !== 'darwin') {
|
closeAppManager(platform)
|
||||||
app.quit()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.on('activate', () => {
|
|
||||||
if (mainWindow === undefined) {
|
|
||||||
createWindow()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
37
src-electron/mainScripts/appManagement.ts
Normal file
37
src-electron/mainScripts/appManagement.ts
Normal 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()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
27
src-electron/mainScripts/fixAppName.ts
Normal file
27
src-electron/mainScripts/fixAppName.ts
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
42
src-electron/mainScripts/mainWindowCreation.ts
Normal file
42
src-electron/mainScripts/mainWindowCreation.ts
Normal 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
|
||||||
|
})
|
||||||
|
}
|
16
src-electron/mainScripts/tweaks.ts
Normal file
16
src-electron/mainScripts/tweaks.ts
Normal 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)
|
||||||
|
}
|
15
src-electron/mainScripts/windowsDevToolsExtensionsFix.ts
Normal file
15
src-electron/mainScripts/windowsDevToolsExtensionsFix.ts
Normal 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 (_) {}
|
||||||
|
}
|
Loading…
Reference in a new issue