From 2302d7055a11adefe15bdaa5d8f94324d2fe293f Mon Sep 17 00:00:00 2001 From: 0neGal Date: Wed, 13 Sep 2023 17:32:16 +0200 Subject: synchronize changes to settings from main process If the main process has changes to the settings, said settings will now also be sent to the renderer, making them synchronized. --- src/app/main.js | 9 +++++++++ src/index.js | 8 +++++++- src/modules/settings.js | 8 ++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/app/main.js b/src/app/main.js index 8775004..230bbf9 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -54,6 +54,15 @@ if (fs.existsSync("viper.json")) { setpath(); } +ipcRenderer.on("changed-settings", (e, new_settings) => { + // attempt to set `settings` to `new_settings` + try { + settings = { + ...settings, + ...new_settings + } + }catch(e) {} +}) // Show a toast message if no Internet connection has been detected. if (! navigator.onLine) { diff --git a/src/index.js b/src/index.js index ed0fb3a..301c622 100644 --- a/src/index.js +++ b/src/index.js @@ -130,7 +130,13 @@ function start() { } }); - ipcMain.on("save-settings", (event, obj) => {settings.save(obj)}); + ipcMain.on("save-settings", (event, obj) => { + settings.save(obj, false) + }); + + ipcMain.on("saved-settings", (event, obj) => { + send("changed-settings", obj); + }); // allows renderer to check for updates ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)}); diff --git a/src/modules/settings.js b/src/modules/settings.js index 3833e4f..e1f68c6 100644 --- a/src/modules/settings.js +++ b/src/modules/settings.js @@ -1,6 +1,6 @@ const fs = require("fs"); const path = require("path"); -const app = require("electron").app; +const { app, ipcMain } = require("electron"); const json = require("./json"); const lang = require("../lang"); @@ -58,7 +58,7 @@ if (fs.existsSync("viper.json")) { // // you can also pass a settings object to the function and it'll try and // merge it together with the already existing settings -settings.save = (obj = {}) => { +settings.save = (obj = {}, notify_renderer = true) => { // refuse to save if settings aren't valid if (invalid_settings) { return false; @@ -88,6 +88,10 @@ settings.save = (obj = {}) => { settings.gamepath, "ns_startup_args.txt" ), settings.nsargs); } + + if (notify_renderer) { + ipcMain.emit("saved-settings", settings_content); + } } module.exports = settings; -- cgit v1.2.3