From 77e743060ff45381d2d8b743dbf5af0b8c564ebe Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Wed, 16 Jun 2021 05:30:01 +0100 Subject: [PATCH] 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. --- _scripts/dev-runner.js | 12 +++++- src/main/index.js | 38 ++++++++++++++----- .../theme-settings/theme-settings.js | 16 ++++---- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/_scripts/dev-runner.js b/_scripts/dev-runner.js index 2581c3a1..ca8ed0e9 100644 --- a/_scripts/dev-runner.js +++ b/_scripts/dev-runner.js @@ -23,6 +23,10 @@ if (remoteDebugging) { 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) { return new Promise((resolve, reject) => { if (pid) { @@ -50,7 +54,13 @@ async function restartElectron() { 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) }) } diff --git a/src/main/index.js b/src/main/index.js index c44040c4..d22b6ed6 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -294,16 +294,36 @@ function runApp() { } }) - ipcMain.on('disableSmoothScrolling', () => { - app.commandLine.appendSwitch('disable-smooth-scrolling') - mainWindow.close() - createWindow() - }) + ipcMain.on('relaunchRequest', () => { + if (isDev) { + app.exit(parseInt(process.env.FREETUBE_RELAUNCH_EXIT_CODE)) + return + } - ipcMain.on('enableSmoothScrolling', () => { - app.commandLine.appendSwitch('enable-smooth-scrolling') - mainWindow.close() - createWindow() + // The AppImage and Windows portable formats must be accounted for + // because `process.execPath` points at the temporarily extracted + // executables, not the executables themselves + // + // 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) => { diff --git a/src/renderer/components/theme-settings/theme-settings.js b/src/renderer/components/theme-settings/theme-settings.js index 45e01f5f..f5e79187 100644 --- a/src/renderer/components/theme-settings/theme-settings.js +++ b/src/renderer/components/theme-settings/theme-settings.js @@ -158,16 +158,14 @@ export default Vue.extend({ return } - this.updateDisableSmoothScrolling(this.disableSmoothScrollingToggleValue) + this.updateDisableSmoothScrolling( + this.disableSmoothScrollingToggleValue + ).then(() => { + // FIXME: No electron safeguard + const { ipcRenderer } = require('electron') - // FIXME: No electron safeguard - const { ipcRenderer } = require('electron') - - if (this.disableSmoothScrollingToggleValue) { - ipcRenderer.send('disableSmoothScrolling') - } else { - ipcRenderer.send('enableSmoothScrolling') - } + ipcRenderer.send('relaunchRequest') + }) }, updateMainColor: function (color) {