From 7e94abb3b49cd59aa19712ef2c597ffae352e10d Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Wed, 9 Jun 2021 05:57:35 +0100 Subject: [PATCH] Main+App+Store: Implement basic setting sync between Electron windows The app utilizes the Electron IPC to communicate settings' updates to the other existing windows. This is still at a fairly rudimentary stage, since some settings are not syncing at all, while other settings have related side effects that are not currently being propagated to the remaining windows. An example of this would be the 'uiScale' setting, in which the value is properly synced, but the app's actual scaling isn't. --- src/main/index.js | 10 ++++++++++ src/renderer/App.js | 4 +++- src/renderer/store/modules/settings.js | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/index.js b/src/main/index.js index ebca3a07..a5c0c534 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -387,6 +387,16 @@ function runApp() { createWindow(false, '', false) }) + ipcMain.on('syncSetting', (event, setting) => { + const otherWindows = openedWindows.filter((window) => { + return window.webContents.id !== event.sender.id + }) + + for (const window of otherWindows) { + window.webContents.send('syncSetting', setting) + } + }) + app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() diff --git a/src/renderer/App.js b/src/renderer/App.js index 7337b17a..11af0ef6 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -80,6 +80,7 @@ export default Vue.extend({ }, mounted: function () { this.grabUserSettings().then(() => { + this.setUpListenerToSyncSettings() this.grabAllProfiles(this.$t('Profile.All Channels')).then(async () => { this.grabHistory() this.grabAllPlaylists() @@ -404,7 +405,8 @@ export default Vue.extend({ 'grabAllPlaylists', 'getRegionData', 'getYoutubeUrlInfo', - 'getLocale' + 'getLocale', + 'setUpListenerToSyncSettings' ]) } }) diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index c69e5657..d8d32555 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -121,6 +121,13 @@ for (const settingId of Object.keys(state)) { (err, _) => { if (!err) { commit(mutationId, value) + if (getters.getUsingElectron) { + const { ipcRenderer } = require('electron') + // Propagate setting values to all other existing windows + ipcRenderer.send('syncSetting', { + _id: settingId, value: value + }) + } } } ) @@ -190,6 +197,18 @@ Object.assign(actions, { } ) }) + }, + + setUpListenerToSyncSettings: ({ commit, getters }) => { + if (getters.getUsingElectron) { + const { ipcRenderer } = require('electron') + ipcRenderer.on('syncSetting', (_, setting) => { + const capitalizedSettingId = + setting._id.replace(/^\w/, (c) => c.toUpperCase()) + + commit('set' + capitalizedSettingId, setting.value) + }) + } } })