Main+Renderer+Dev-Runner: Refactor smooth scrolling toggling

Smooth scrolling toggling is currently wonky on runtime, since,
most of the time, it doesn't toggle on and off properly.
In addition, now that we have multi-window support,
the current implementation was somewhat lacking.

This commit solves those issues by replacing
the existing smooth scrolling related channels with a
new generic `relaunchRequest` channel.
This commit is contained in:
Svallinn 2021-06-16 05:30:01 +01:00
parent bceab435b7
commit 77e743060f
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
3 changed files with 47 additions and 19 deletions

View File

@ -23,6 +23,10 @@ if (remoteDebugging) {
process.env.RENDERER_REMOTE_DEBUGGING = true process.env.RENDERER_REMOTE_DEBUGGING = true
} }
// Define exit code for relaunch and set it in the environment
const relaunchExitCode = 69
process.env.FREETUBE_RELAUNCH_EXIT_CODE = relaunchExitCode
async function killElectron(pid) { async function killElectron(pid) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (pid) { if (pid) {
@ -50,7 +54,13 @@ async function restartElectron() {
remoteDebugging ? '--remote-debugging-port=9223' : '', remoteDebugging ? '--remote-debugging-port=9223' : '',
]) ])
electronProcess.on('exit', (code, signal) => { electronProcess.on('exit', (code, _) => {
if (code === relaunchExitCode) {
electronProcess = null
restartElectron()
return
}
if (!manualRestart) process.exit(0) if (!manualRestart) process.exit(0)
}) })
} }

View File

@ -294,16 +294,36 @@ function runApp() {
} }
}) })
ipcMain.on('disableSmoothScrolling', () => { ipcMain.on('relaunchRequest', () => {
app.commandLine.appendSwitch('disable-smooth-scrolling') if (isDev) {
mainWindow.close() app.exit(parseInt(process.env.FREETUBE_RELAUNCH_EXIT_CODE))
createWindow() return
}) }
ipcMain.on('enableSmoothScrolling', () => { // The AppImage and Windows portable formats must be accounted for
app.commandLine.appendSwitch('enable-smooth-scrolling') // because `process.execPath` points at the temporarily extracted
mainWindow.close() // executables, not the executables themselves
createWindow() //
// It's possible to detect these formats and identify their
// executables' paths by checking the environmental variables
const { env: { APPIMAGE, PORTABLE_EXECUTABLE_FILE } } = process
if (!APPIMAGE) {
// If it's a Windows portable, PORTABLE_EXECUTABLE_FILE will
// hold a value.
// Otherwise, `process.execPath` should be used instead.
app.relaunch({
args: process.argv.slice(1),
execPath: PORTABLE_EXECUTABLE_FILE || process.execPath
})
} else {
// If it's an AppImage, things must be done the "hard way"
// `app.relaunch` doesn't work because of FUSE limitations
// Spawn a new process using the APPIMAGE env variable
cp.spawn(APPIMAGE, { detached: true, stdio: 'ignore' })
}
app.quit()
}) })
ipcMain.on('enableProxy', (_, url) => { ipcMain.on('enableProxy', (_, url) => {

View File

@ -158,16 +158,14 @@ export default Vue.extend({
return return
} }
this.updateDisableSmoothScrolling(this.disableSmoothScrollingToggleValue) this.updateDisableSmoothScrolling(
this.disableSmoothScrollingToggleValue
).then(() => {
// FIXME: No electron safeguard // FIXME: No electron safeguard
const { ipcRenderer } = require('electron') const { ipcRenderer } = require('electron')
if (this.disableSmoothScrollingToggleValue) { ipcRenderer.send('relaunchRequest')
ipcRenderer.send('disableSmoothScrolling') })
} else {
ipcRenderer.send('enableSmoothScrolling')
}
}, },
updateMainColor: function (color) { updateMainColor: function (color) {