SettingsIO trait, save/load settings to/from file
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}));
|
||||
|
||||
|
||||
@@ -1,7 +1,56 @@
|
||||
use crate::testbed::{Testbed, TestbedUpdateParams};
|
||||
use dash_frontend::frontend;
|
||||
use dash_frontend::{
|
||||
frontend,
|
||||
settings::{self, SettingsIO},
|
||||
};
|
||||
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 {
|
||||
layout: RcLayout,
|
||||
frontend: frontend::RcFrontend,
|
||||
@@ -9,8 +58,10 @@ pub struct TestbedDashboard {
|
||||
|
||||
impl TestbedDashboard {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
let settings = SimpleSettingsIO::new();
|
||||
|
||||
let (frontend, layout) = frontend::Frontend::new(frontend::InitParams {
|
||||
settings: Default::default(),
|
||||
settings: Box::new(settings),
|
||||
})?;
|
||||
Ok(Self { frontend, layout })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user