Main: Rearrange/refactor the main process' logic

This commit removes duplicated handler logic related to the app's
'ready' event. In addition, it rearranges the template used to build the
app's menu in order to make it a bit easier to understand.
This commit is contained in:
Svallinn 2021-06-07 01:38:06 +01:00
parent 5a2c1f401f
commit 94fcacc308
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 134 additions and 231 deletions

View File

@ -3,10 +3,11 @@ import {
ipcMain, powerSaveBlocker, screen, shell
} from 'electron'
import Datastore from 'nedb'
import path from 'path'
if (process.argv.includes('--version')) {
console.log(`v${app.getVersion()}`)
app.exit(0)
app.exit()
} else {
runApp()
}
@ -28,7 +29,6 @@ function runApp() {
// disable electron warning
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug')
let mainWindow
@ -57,15 +57,16 @@ function runApp() {
app.setAsDefaultProtocolClient('freetube')
}
// TODO: Uncomment if needed
// only allow single instance of application
if (!isDev) {
// Only allow single instance of the application
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
}
if (gotTheLock) {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow && typeof (commandLine) !== 'undefined') {
app.on('second-instance', (_, commandLine, __) => {
// Someone tried to run a second instance, we should focus our window
if (mainWindow && typeof commandLine !== 'undefined') {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
@ -75,78 +76,13 @@ function runApp() {
}
}
})
app.on('ready', (event, commandLine, workingDirectory) => {
settingsDb.find({
$or: [
{ _id: 'disableSmoothScrolling' },
{ _id: 'useProxy' },
{ _id: 'proxyProtocol' },
{ _id: 'proxyHostname' },
{ _id: 'proxyPort' }
]
}, function (err, doc) {
if (err) {
app.exit(0)
return
}
let disableSmoothScrolling = false
let useProxy = false
let proxyProtocol = 'socks5'
let proxyHostname = '127.0.0.1'
let proxyPort = '9050'
if (typeof doc === 'object' && doc.length > 0) {
doc.forEach((dbItem) => {
switch (dbItem._id) {
case 'disableSmoothScrolling':
disableSmoothScrolling = dbItem.value
break
case 'useProxy':
useProxy = dbItem.value
break
case 'proxyProtocol':
proxyProtocol = dbItem.value
break
case 'proxyHostname':
proxyHostname = dbItem.value
break
case 'proxyPort':
proxyPort = dbItem.value
break
}
})
}
if (disableSmoothScrolling) {
app.commandLine.appendSwitch('disable-smooth-scrolling')
} else {
app.commandLine.appendSwitch('enable-smooth-scrolling')
}
const proxyUrl = `${proxyProtocol}://${proxyHostname}:${proxyPort}`
createWindow(useProxy, proxyUrl)
if (isDev) {
installDevTools()
}
if (isDebug) {
mainWindow.webContents.openDevTools()
}
})
})
} else {
app.quit()
}
} else {
require('electron-debug')({
showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true')
})
}
app.on('ready', () => {
app.on('ready', (_, __) => {
settingsDb.find({
$or: [
{ _id: 'disableSmoothScrolling' },
@ -157,7 +93,7 @@ function runApp() {
]
}, function (err, doc) {
if (err) {
app.exit(0)
app.exit()
return
}
@ -208,7 +144,6 @@ function runApp() {
}
})
})
}
async function installDevTools() {
try {
@ -522,19 +457,16 @@ function runApp() {
mainWindow.webContents.send('change-view', data)
}
const template = [{
label: 'File',
submenu: [
function setMenu() {
const template = [
{
role: 'quit'
}
]
label: 'File',
submenu: [{ role: 'quit' }]
},
{
label: 'Edit',
submenu: [{
role: 'cut'
},
submenu: [
{ role: 'cut' },
{
role: 'copy',
accelerator: 'CmdOrCtrl+C',
@ -545,62 +477,37 @@ function runApp() {
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
{ role: 'pasteandmatchstyle' },
{ role: 'delete' },
{ role: 'selectall' }
]
},
{
label: 'View',
submenu: [{
role: 'reload'
},
submenu: [
{ role: 'reload' },
{
role: 'forcereload',
accelerator: 'CmdOrCtrl+Shift+R'
},
{
role: 'toggledevtools'
},
{
type: 'separator'
},
{
role: 'resetzoom'
},
{
role: 'zoomin'
},
{
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
}
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
role: 'window',
submenu: [{
role: 'minimize'
},
{
role: 'close'
}
submenu: [
{ role: 'minimize' },
{ role: 'close' }
]
}
]
function setMenu () {
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
@ -617,15 +524,11 @@ function runApp() {
]
})
template.push({
role: 'window'
})
template.push({
role: 'help'
})
template.push({ role: 'services' })
template.push(
{ role: 'window' },
{ role: 'help' },
{ role: 'services' }
)
}
const menu = Menu.buildFromTemplate(template)