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 { app, BrowserWindow } from 'electron'
|
||||||
import Constants from './utils/Constants'
|
import Constants from './utils/Constants'
|
||||||
import Utils from './utils'
|
import { join } from 'path'
|
||||||
|
import * as electronRemote from '@electron/remote/main'
|
||||||
|
|
||||||
export default class MainRunner {
|
const getAppIcon = () => {
|
||||||
static async createMainWindow (mainWindow) {
|
return join(__static, Constants.APP_ICON)
|
||||||
mainWindow = new BrowserWindow({
|
}
|
||||||
title: Constants.APP_NAME,
|
|
||||||
icon: Utils.getAppIcon(),
|
const exitApp = (mainWindow) => {
|
||||||
show: false,
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||||
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
mainWindow.hide()
|
||||||
height: 650,
|
}
|
||||||
useContentSize: true,
|
mainWindow = null
|
||||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
app.exit()
|
||||||
})
|
}
|
||||||
|
|
||||||
mainWindow.setMenu(null)
|
export const createMainWindow = async (mainWindow) => {
|
||||||
|
mainWindow = new BrowserWindow({
|
||||||
mainWindow.on('close', (event) => {
|
title: Constants.APP_NAME,
|
||||||
event.preventDefault()
|
icon: getAppIcon(),
|
||||||
MainRunner.exitApp(mainWindow)
|
show: false,
|
||||||
})
|
width: Constants.IS_DEV_ENV ? 1500 : 1200,
|
||||||
|
height: 650,
|
||||||
mainWindow.on('show', () => {
|
useContentSize: true,
|
||||||
mainWindow.focus()
|
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.webContents.on('did-frame-finish-load', () => {
|
mainWindow.setMenu(null)
|
||||||
if (Constants.IS_DEV_ENV) {
|
|
||||||
mainWindow.webContents.openDevTools()
|
mainWindow.on('close', (event) => {
|
||||||
}
|
event.preventDefault()
|
||||||
})
|
exitApp(mainWindow)
|
||||||
|
})
|
||||||
mainWindow.once('ready-to-show', () => {
|
|
||||||
mainWindow.setAlwaysOnTop(true)
|
mainWindow.on('show', () => {
|
||||||
mainWindow.show()
|
mainWindow.focus()
|
||||||
mainWindow.setAlwaysOnTop(false)
|
})
|
||||||
})
|
|
||||||
|
mainWindow.webContents.on('did-frame-finish-load', () => {
|
||||||
await mainWindow.loadURL(`${Constants.APP_INDEX_URL}`)
|
if (Constants.IS_DEV_ENV) {
|
||||||
|
mainWindow.webContents.openDevTools()
|
||||||
return mainWindow
|
}
|
||||||
}
|
|
||||||
|
electronRemote.initialize()
|
||||||
static createErrorWindow (errorWindow, mainWindow, details) {
|
})
|
||||||
if (!Constants.IS_DEV_ENV) {
|
|
||||||
mainWindow?.hide()
|
mainWindow.once('ready-to-show', () => {
|
||||||
}
|
mainWindow.setAlwaysOnTop(true)
|
||||||
|
mainWindow.show()
|
||||||
errorWindow = new BrowserWindow({
|
mainWindow.setAlwaysOnTop(false)
|
||||||
title: Constants.APP_NAME,
|
})
|
||||||
icon: Utils.getAppIcon(),
|
|
||||||
show: false,
|
await mainWindow.loadURL(`${Constants.APP_INDEX_URL}`)
|
||||||
resizable: Constants.IS_DEV_ENV,
|
|
||||||
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
return mainWindow
|
||||||
})
|
}
|
||||||
|
|
||||||
errorWindow.setMenu(null)
|
export const createErrorWindow = (errorWindow, mainWindow, details) => {
|
||||||
errorWindow.loadURL(`${Constants.APP_INDEX_URL}#/error`).then(() => true)
|
if (!Constants.IS_DEV_ENV) {
|
||||||
|
mainWindow?.hide()
|
||||||
errorWindow.on('ready-to-show', () => {
|
}
|
||||||
if (!Constants.IS_DEV_ENV && mainWindow && !mainWindow.isDestroyed()) {
|
|
||||||
mainWindow.destroy()
|
errorWindow = new BrowserWindow({
|
||||||
}
|
title: Constants.APP_NAME,
|
||||||
errorWindow.show()
|
icon: getAppIcon(),
|
||||||
errorWindow.focus()
|
show: false,
|
||||||
})
|
resizable: Constants.IS_DEV_ENV,
|
||||||
|
webPreferences: Constants.DEFAULT_WEB_PREFERENCES
|
||||||
errorWindow.webContents.on('did-frame-finish-load', () => {
|
})
|
||||||
if (Constants.IS_DEV_ENV) {
|
|
||||||
errorWindow.webContents.openDevTools()
|
errorWindow.setMenu(null)
|
||||||
}
|
errorWindow.loadURL(`${Constants.APP_INDEX_URL}#/error`).then(() => true)
|
||||||
})
|
|
||||||
|
errorWindow.on('ready-to-show', () => {
|
||||||
return errorWindow
|
if (!Constants.IS_DEV_ENV && mainWindow && !mainWindow.isDestroyed()) {
|
||||||
}
|
mainWindow.destroy()
|
||||||
|
}
|
||||||
static exitApp (mainWindow) {
|
errorWindow.show()
|
||||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
errorWindow.focus()
|
||||||
mainWindow.hide()
|
})
|
||||||
}
|
|
||||||
mainWindow = null
|
errorWindow.webContents.on('did-frame-finish-load', () => {
|
||||||
app.exit()
|
if (Constants.IS_DEV_ENV) {
|
||||||
}
|
errorWindow.webContents.openDevTools()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return errorWindow
|
||||||
}
|
}
|
||||||
+6
-9
@@ -1,28 +1,25 @@
|
|||||||
import 'v8-compile-cache'
|
import 'v8-compile-cache'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import path from 'path'
|
import { join, dirname } from 'path'
|
||||||
import MainRunner from './MainRunner'
|
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import Constants from './utils/Constants'
|
import Constants from './utils/Constants'
|
||||||
import * as electronRemote from '@electron/remote/main'
|
import { createErrorWindow, createMainWindow } from './MainRunner'
|
||||||
|
|
||||||
let mainWindow
|
let mainWindow
|
||||||
let errorWindow
|
let errorWindow
|
||||||
|
|
||||||
app.on('ready', () => {
|
app.on('ready', () => {
|
||||||
electronRemote.initialize()
|
|
||||||
|
|
||||||
if (!Constants.IS_DEV_ENV) {
|
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, '\\\\')
|
.replace(/\\/g, '\\\\')
|
||||||
}
|
}
|
||||||
|
|
||||||
mainWindow = MainRunner.createMainWindow(mainWindow)
|
mainWindow = createMainWindow(mainWindow)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('activate', () => {
|
app.on('activate', () => {
|
||||||
if (!mainWindow) {
|
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) => {
|
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