SettingsIO trait, save/load settings to/from file
This commit is contained in:
@@ -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