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

View File

@@ -25,10 +25,18 @@ pub struct Settings {
impl Settings { impl Settings {
pub fn save(&self) -> String { 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> { pub fn load(input: &str) -> anyhow::Result<Settings> {
Ok(serde_json::from_str::<Settings>(input)?) 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(); let rc_frontend = params.frontend.clone();
checkbox.on_toggle(Box::new(move |_common, e| { checkbox.on_toggle(Box::new(move |_common, e| {
let mut frontend = rc_frontend.borrow_mut(); 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 { if let Some(change_callback) = &change_callback {
change_callback(&mut frontend, e.checked); change_callback(&mut frontend, e.checked);
} }
frontend.settings.mark_as_dirty();
Ok(()) Ok(())
})); }));

View File

@@ -1,7 +1,56 @@
use crate::testbed::{Testbed, TestbedUpdateParams}; use crate::testbed::{Testbed, TestbedUpdateParams};
use dash_frontend::frontend; use dash_frontend::{
frontend,
settings::{self, SettingsIO},
};
use wgui::layout::RcLayout; use wgui::layout::RcLayout;
struct SimpleSettingsIO {
settings: settings::Settings,
}
impl SimpleSettingsIO {
fn new() -> Self {
let mut res = Self {
settings: settings::Settings::default(),
};
res.read_from_disk();
res
}
}
// just a simple impl of a config io for dashboard frontend
// use ~/.config later
impl settings::SettingsIO for SimpleSettingsIO {
fn get_mut(&mut self) -> &mut settings::Settings {
&mut self.settings
}
fn get(&self) -> &dash_frontend::settings::Settings {
&self.settings
}
fn save_to_disk(&mut self) {
log::info!("saving settings");
let data = self.settings.save();
std::fs::write("/tmp/testbed_settings.json", data).unwrap();
}
fn read_from_disk(&mut self) {
log::info!("loading settings");
if let Ok(res) = std::fs::read("/tmp/testbed_settings.json") {
let data = String::from_utf8(res).unwrap();
self.settings = settings::Settings::load(&data).unwrap();
}
}
fn mark_as_dirty(&mut self) {
// just save it, at least for now
// save_to_disk should be called later in time or at exit, not instantly
self.save_to_disk();
}
}
pub struct TestbedDashboard { pub struct TestbedDashboard {
layout: RcLayout, layout: RcLayout,
frontend: frontend::RcFrontend, frontend: frontend::RcFrontend,
@@ -9,8 +58,10 @@ pub struct TestbedDashboard {
impl TestbedDashboard { impl TestbedDashboard {
pub fn new() -> anyhow::Result<Self> { pub fn new() -> anyhow::Result<Self> {
let settings = SimpleSettingsIO::new();
let (frontend, layout) = frontend::Frontend::new(frontend::InitParams { let (frontend, layout) = frontend::Frontend::new(frontend::InitParams {
settings: Default::default(), settings: Box::new(settings),
})?; })?;
Ok(Self { frontend, layout }) Ok(Self { frontend, layout })
} }