SettingsIO trait, save/load settings to/from file

This commit is contained in:
Aleksander
2025-11-08 11:16:19 +01:00
parent 71898056f3
commit 6f41ffe59c
4 changed files with 71 additions and 9 deletions

View File

@@ -25,7 +25,7 @@ pub struct Frontend {
pub layout: RcLayout,
globals: WguiGlobals,
pub settings: settings::Settings,
pub settings: Box<dyn settings::SettingsIO>,
#[allow(dead_code)]
state: ParserState,
@@ -41,7 +41,7 @@ pub struct Frontend {
}
pub struct InitParams {
pub settings: settings::Settings,
pub settings: Box<dyn settings::SettingsIO>,
}
pub type RcFrontend = Rc<RefCell<Frontend>>;
@@ -150,7 +150,7 @@ impl Frontend {
let hours = now.hour();
let minutes = now.minute();
let text: String = if !self.settings.general.am_pm_clock {
let text: String = if !self.settings.get().general.am_pm_clock {
format!("{hours:02}:{minutes:02}")
} else {
let hours_ampm = (hours + 11) % 12 + 1;
@@ -172,7 +172,7 @@ impl Frontend {
anyhow::bail!("");
};
let (alpha1, alpha2) = if !self.settings.general.opaque_background {
let (alpha1, alpha2) = if !self.settings.get().general.opaque_background {
(0.8666, 0.9333)
} else {
(1.0, 1.0)
@@ -212,7 +212,7 @@ impl Frontend {
layout: &mut layout,
parent_id: widget_content.id,
frontend: rc_this,
settings: &mut self.settings,
settings: &mut self.settings.get_mut(),
};
let tab: Box<dyn Tab> = match tab_type {

View File

@@ -25,10 +25,18 @@ pub struct Settings {
impl Settings {
pub fn save(&self) -> String {
serde_json::to_string(&self).unwrap() /* want panic */
serde_json::to_string_pretty(&self).unwrap() /* want panic */
}
pub fn load(input: &str) -> anyhow::Result<Settings> {
Ok(serde_json::from_str::<Settings>(input)?)
}
}
pub trait SettingsIO {
fn get_mut(&mut self) -> &mut Settings;
fn get(&self) -> &Settings;
fn save_to_disk(&mut self);
fn read_from_disk(&mut self);
fn mark_as_dirty(&mut self);
}

View File

@@ -35,11 +35,14 @@ fn init_setting_checkbox(
let rc_frontend = params.frontend.clone();
checkbox.on_toggle(Box::new(move |_common, e| {
let mut frontend = rc_frontend.borrow_mut();
*fetch_callback(&mut frontend.settings) = e.checked;
*fetch_callback(frontend.settings.get_mut()) = e.checked;
if let Some(change_callback) = &change_callback {
change_callback(&mut frontend, e.checked);
}
frontend.settings.mark_as_dirty();
Ok(())
}));