main: use functional methods
This commit is contained in:
1 parent
99e95970bd
commit
d1c3d4fc58
3 files changed
+92
-98
No files matched your search
+86
-81
@@ -1,85 +1,90 @@
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import Constants from './utils/Constants'
|
||||
import Utils from './utils'
|
||||
import { join } from 'path'
|
||||
import * as electronRemote from '@electron/remote/main'
|
||||
|
||||
export default class MainRunner {
|
||||
static async createMainWindow (mainWindow) {
|
||||
mainWindow = new BrowserWindow({
|
||||
title: Constants.APP_NAME,
|
||||
icon: Utils.getAppIcon(),
|
||||
show: false,
|
||||
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
||||
height: 650,
|
||||
useContentSize: true,
|
||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||
})
|
||||
|
||||
mainWindow.setMenu(null)
|
||||
|
||||
mainWindow.on('close', (event) => {
|
||||
event.preventDefault()
|
||||
MainRunner.exitApp(mainWindow)
|
||||
})
|
||||
|
||||
mainWindow.on('show', () => {
|
||||
mainWindow.focus()
|
||||
})
|
||||
|
||||
mainWindow.webContents.on('did-frame-finish-load', () => {
|
||||
if (Constants.IS_DEV_ENV) {
|
||||
mainWindow.webContents.openDevTools()
|
||||
}
|
||||
})
|
||||
|
||||
mainWindow.once('ready-to-show', () => {
|
||||
mainWindow.setAlwaysOnTop(true)
|
||||
mainWindow.show()
|
||||
mainWindow.setAlwaysOnTop(false)
|
||||
})
|
||||
|
||||
await mainWindow.loadURL(`${Constants.APP_INDEX_URL}`)
|
||||
|
||||
return mainWindow
|
||||
}
|
||||
|
||||
static createErrorWindow (errorWindow, mainWindow, details) {
|
||||
if (!Constants.IS_DEV_ENV) {
|
||||
mainWindow?.hide()
|
||||
}
|
||||
|
||||
errorWindow = new BrowserWindow({
|
||||
title: Constants.APP_NAME,
|
||||
icon: Utils.getAppIcon(),
|
||||
show: false,
|
||||
resizable: Constants.IS_DEV_ENV,
|
||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||
})
|
||||
|
||||
errorWindow.setMenu(null)
|
||||
errorWindow.loadURL(`${Constants.APP_INDEX_URL}#/error`).then(() => true)
|
||||
|
||||
errorWindow.on('ready-to-show', () => {
|
||||
if (!Constants.IS_DEV_ENV && mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.destroy()
|
||||
}
|
||||
errorWindow.show()
|
||||
errorWindow.focus()
|
||||
})
|
||||
|
||||
errorWindow.webContents.on('did-frame-finish-load', () => {
|
||||
if (Constants.IS_DEV_ENV) {
|
||||
errorWindow.webContents.openDevTools()
|
||||
}
|
||||
})
|
||||
|
||||
return errorWindow
|
||||
}
|
||||
|
||||
static exitApp (mainWindow) {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.hide()
|
||||
}
|
||||
mainWindow = null
|
||||
app.exit()
|
||||
}
|
||||
const getAppIcon = () => {
|
||||
return join(__static, Constants.APP_ICON)
|
||||
}
|
||||
|
||||
const exitApp = (mainWindow) => {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.hide()
|
||||
}
|
||||
mainWindow = null
|
||||
app.exit()
|
||||
}
|
||||
|
||||
export const createMainWindow = async (mainWindow) => {
|
||||
mainWindow = new BrowserWindow({
|
||||
title: Constants.APP_NAME,
|
||||
icon: getAppIcon(),
|
||||
show: false,
|
||||
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
||||
height: 650,
|
||||
useContentSize: true,
|
||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||
})
|
||||
|
||||
mainWindow.setMenu(null)
|
||||
|
||||
mainWindow.on('close', (event) => {
|
||||
event.preventDefault()
|
||||
exitApp(mainWindow)
|
||||
})
|
||||
|
||||
mainWindow.on('show', () => {
|
||||
mainWindow.focus()
|
||||
})
|
||||
|
||||
mainWindow.webContents.on('did-frame-finish-load', () => {
|
||||
if (Constants.IS_DEV_ENV) {
|
||||
mainWindow.webContents.openDevTools()
|
||||
}
|
||||
|
||||
electronRemote.initialize()
|
||||
})
|
||||
|
||||
mainWindow.once('ready-to-show', () => {
|
||||
mainWindow.setAlwaysOnTop(true)
|
||||
mainWindow.show()
|
||||
mainWindow.setAlwaysOnTop(false)
|
||||
})
|
||||
|
||||
await mainWindow.loadURL(`${Constants.APP_INDEX_URL}`)
|
||||
|
||||
return mainWindow
|
||||
}
|
||||
|
||||
export const createErrorWindow = (errorWindow, mainWindow, details) => {
|
||||
if (!Constants.IS_DEV_ENV) {
|
||||
mainWindow?.hide()
|
||||
}
|
||||
|
||||
errorWindow = new BrowserWindow({
|
||||
title: Constants.APP_NAME,
|
||||
icon: getAppIcon(),
|
||||
show: false,
|
||||
resizable: Constants.IS_DEV_ENV,
|
||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||
})
|
||||
|
||||
errorWindow.setMenu(null)
|
||||
errorWindow.loadURL(`${Constants.APP_INDEX_URL}#/error`).then(() => true)
|
||||
|
||||
errorWindow.on('ready-to-show', () => {
|
||||
if (!Constants.IS_DEV_ENV && mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.destroy()
|
||||
}
|
||||
errorWindow.show()
|
||||
errorWindow.focus()
|
||||
})
|
||||
|
||||
errorWindow.webContents.on('did-frame-finish-load', () => {
|
||||
if (Constants.IS_DEV_ENV) {
|
||||
errorWindow.webContents.openDevTools()
|
||||
}
|
||||
})
|
||||
|
||||
return errorWindow
|
||||
}
|
||||
+6
-9
@@ -1,28 +1,25 @@
|
||||
import 'v8-compile-cache'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import MainRunner from './MainRunner'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import Constants from './utils/Constants'
|
||||
import * as electronRemote from '@electron/remote/main'
|
||||
import { createErrorWindow, createMainWindow } from './MainRunner'
|
||||
|
||||
let mainWindow
|
||||
let errorWindow
|
||||
|
||||
app.on('ready', () => {
|
||||
electronRemote.initialize()
|
||||
|
||||
if (!Constants.IS_DEV_ENV) {
|
||||
global.__static = path.join(path.dirname(fileURLToPath(import.meta.url)), '/static')
|
||||
global.__static = join(dirname(fileURLToPath(import.meta.url)), '/static')
|
||||
.replace(/\\/g, '\\\\')
|
||||
}
|
||||
|
||||
mainWindow = MainRunner.createMainWindow(mainWindow)
|
||||
mainWindow = createMainWindow(mainWindow)
|
||||
})
|
||||
|
||||
app.on('activate', () => {
|
||||
if (!mainWindow) {
|
||||
mainWindow = MainRunner.createMainWindow(mainWindow)
|
||||
mainWindow = createMainWindow(mainWindow)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -36,5 +33,5 @@ app.on('window-all-closed', () => {
|
||||
})
|
||||
|
||||
app.on('render-process-gone', (ev, webContents, details) => {
|
||||
errorWindow = MainRunner.createErrorWindow(errorWindow, mainWindow, details)
|
||||
errorWindow = createErrorWindow(errorWindow, mainWindow, details)
|
||||
})
|
||||
@@ -1,8 +0,0 @@
|
||||
import path from 'path'
|
||||
import Constants from './Constants'
|
||||
|
||||
export default class Utils {
|
||||
static getAppIcon () {
|
||||
return path.join(__static, Constants.APP_ICON)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user