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