From 68e86c07789ee4bf6b590ea1861e1d5146eddcf0 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sat, 25 Mar 2023 13:46:58 -0400 Subject: [PATCH] Use profile filename instead of profile name to detect profile change --- backend/src/api/general.rs | 22 ++++++++++++++++++++++ backend/src/api/handler.rs | 2 ++ backend/src/main.rs | 4 ++++ backend/src/settings/general.rs | 4 ++-- src/backend.ts | 4 ++++ src/consts.ts | 2 ++ src/index.tsx | 18 ++++++++++-------- 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/backend/src/api/general.rs b/backend/src/api/general.rs index 25aaa59..ff9afce 100644 --- a/backend/src/api/general.rs +++ b/backend/src/api/general.rs @@ -140,6 +140,28 @@ pub fn get_name( } } +/// Generate get current settings name +pub fn get_path( + sender: Sender, +) -> impl AsyncCallable { + let sender = Arc::new(Mutex::new(sender)); // Sender is not Sync; this is required for safety + let getter = move || { + let sender2 = sender.clone(); + move || { + let (tx, rx) = mpsc::channel(); + let callback = move |name: std::path::PathBuf| tx.send(name).expect("get_path callback send failed"); + sender2.lock().unwrap().send(ApiMessage::General(GeneralMessage::GetPath(Box::new(callback)))).expect("get_name send failed"); + rx.recv().expect("get_path callback recv failed") + } + }; + super::async_utils::AsyncIshGetter { + set_get: getter, + trans_getter: |result| { + vec![result.to_string_lossy().to_string().into()] + } + } +} + /// Generate wait for all locks to be available web method pub fn lock_unlock_all( sender: Sender, diff --git a/backend/src/api/handler.rs b/backend/src/api/handler.rs index fc5f0ed..b9dc25f 100644 --- a/backend/src/api/handler.rs +++ b/backend/src/api/handler.rs @@ -194,6 +194,7 @@ pub enum GeneralMessage { SetPersistent(bool), GetPersistent(Callback), GetCurrentProfileName(Callback), + GetPath(Callback), ApplyNow, } @@ -204,6 +205,7 @@ impl GeneralMessage { Self::SetPersistent(val) => *settings.persistent() = val, Self::GetPersistent(cb) => cb(*settings.persistent()), Self::GetCurrentProfileName(cb) => cb(settings.get_name().to_owned()), + Self::GetPath(cb) => cb(settings.get_path().to_owned()), Self::ApplyNow => {}, } dirty diff --git a/backend/src/main.rs b/backend/src/main.rs index 6adbc46..5dd0557 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -215,6 +215,10 @@ fn main() -> Result<(), ()> { "GENERAL_get_name", api::general::get_name(api_sender.clone()) ) + .register_async( + "GENERAL_get_path", + api::general::get_path(api_sender.clone()) + ) .register_async( "GENERAL_wait_for_unlocks", api::general::lock_unlock_all(api_sender.clone()) diff --git a/backend/src/settings/general.rs b/backend/src/settings/general.rs index 159c6b6..8da9489 100644 --- a/backend/src/settings/general.rs +++ b/backend/src/settings/general.rs @@ -140,7 +140,7 @@ impl Settings { } pub fn load_file(&mut self, filename: PathBuf, name: String, system_defaults: bool) -> Result { - let json_path = crate::utility::settings_dir().join(filename); + let json_path = crate::utility::settings_dir().join(&filename); if json_path.exists() { let settings_json = SettingsJson::open(&json_path).map_err(|e| SettingError { msg: e.to_string(), @@ -176,7 +176,7 @@ impl Settings { } *self.general.persistent() = false; } - self.general.path(json_path); + self.general.path(filename); Ok(*self.general.persistent()) } diff --git a/src/backend.ts b/src/backend.ts index 669519c..8333a62 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -245,6 +245,10 @@ export async function getGeneralSettingsName(): Promise { return (await call_backend("GENERAL_get_name", []))[0]; } +export async function getGeneralSettingsPath(): Promise { + return (await call_backend("GENERAL_get_path", []))[0]; +} + export async function waitForComplete(): Promise { return (await call_backend("GENERAL_wait_for_unlocks", []))[0]; } diff --git a/src/consts.ts b/src/consts.ts index 0efe070..38e1496 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -27,3 +27,5 @@ export const SLOW_MEMORY_GPU = "GPU_slow_memory"; export const PERSISTENT_GEN = "GENERAL_persistent"; export const NAME_GEN = "GENERAL_name"; +export const PATH_GEN = "GENERAL_path"; + diff --git a/src/index.tsx b/src/index.tsx index 09f9cfe..9350e10 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -21,6 +21,7 @@ import { } from "decky-frontend-lib"; import { VFC, useState } from "react"; import { GiDrill, GiTimeBomb, GiTimeTrap, GiDynamite } from "react-icons/gi"; +import { HiRefresh, HiTrash } from "react-icons/hi"; //import * as python from "./python"; import * as backend from "./backend"; @@ -54,6 +55,7 @@ import { PERSISTENT_GEN, NAME_GEN, + PATH_GEN, } from "./consts"; import { set_value, get_value } from "usdpl-front"; import { Debug } from "./components/debug"; @@ -142,6 +144,7 @@ const reload = function() { backend.resolve(backend.getGeneralPersistent(), (value: boolean) => { set_value(PERSISTENT_GEN, value) }); backend.resolve(backend.getGeneralSettingsName(), (name: string) => { set_value(NAME_GEN, name) }); + backend.resolve(backend.getGeneralSettingsPath(), (path: string) => { set_value(PATH_GEN, path) }); backend.resolve(backend.getInfo(), (info: string) => { set_value(BACKEND_INFO, info) }); backend.resolve(backend.getDriverProviderName("gpu"), (driver: string) => { set_value(DRIVER_INFO, driver) }); @@ -151,7 +154,6 @@ const reload = function() { (async function(){ await backend.initBackend(); usdplReady = true; - set_value(NAME_GEN, "Default"); reload(); // technically this is only a load // register Steam callbacks @@ -186,11 +188,11 @@ const periodicals = function() { backend.resolve(backend.getBatteryChargeNow(), (rate: number) => { set_value(CHARGE_NOW_BATT, rate) }); backend.resolve(backend.getBatteryChargeFull(), (rate: number) => { set_value(CHARGE_FULL_BATT, rate) }); - backend.resolve(backend.getGeneralPersistent(), (value: boolean) => { set_value(PERSISTENT_GEN, value) }); - backend.resolve(backend.getGeneralSettingsName(), (name: string) => { - const oldValue = get_value(NAME_GEN); - set_value(NAME_GEN, name); - if (name != oldValue) { + backend.resolve(backend.getGeneralSettingsPath(), (path: string) => { + const oldValue = get_value(PATH_GEN); + set_value(PATH_GEN, path); + if (path != oldValue) { + backend.log(backend.LogLevel.Info, "Frontend values reload triggered by path change: " + oldValue + " -> " + path); reload(); } }); @@ -272,7 +274,7 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => { backend.forceApplySettings(); }} > - {tr("Reapply settings")} + {tr("Reapply settings")} @@ -293,7 +295,7 @@ const Content: VFC<{ serverAPI: ServerAPI }> = ({}) => { ); }} > - {tr("Defaults")} + {tr("Defaults")}