refactor: better save and load
cargo devel CI / cargo CI (push) Successful in 2m46s Details

This commit is contained in:
Christoph J. Scherr 2024-08-13 17:15:18 +02:00
parent 2aa186f99a
commit 8f51a5ff44
3 changed files with 40 additions and 25 deletions

View File

@ -20,9 +20,8 @@ eframe = { version = "0.28.1", default-features = true, features = [
"glow", # Use the glow rendering backend. Alternative: "wgpu".
"persistence", # Enable restoring app state when restarting the app.
] }
log = "0.4"
env_logger = "0.10"
serde = "1.0.207"
libpt = { version = "0.6.0", features = ["cli"] }
clap = { version = "4.5.15", features = ["derive"] }
image = "0.25.2"
rand = "0.8.5"

View File

@ -1,19 +1,26 @@
use clap::Parser;
use egui::IconData;
use libpt::cli::args::HELP_TEMPLATE;
use libpt::cli::args::{VerbosityLevel, HELP_TEMPLATE};
pub const TITLE: &str = "Rollator";
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
/// Placeholder comment that will show in the help in the CLI
///
/// amogus
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, Parser)]
#[command(help_template = HELP_TEMPLATE, author, version)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct RollatorApp {
// Example stuff:
#[clap(skip)]
label: String,
#[serde(skip)] // This how you opt-out of serialization of a field
#[serde(skip)]
#[clap(skip)]
value: f32,
#[serde(skip)]
#[command(flatten)]
pub(crate) verbosity: VerbosityLevel,
}
impl Default for RollatorApp {
@ -21,35 +28,31 @@ impl Default for RollatorApp {
Self {
label: TITLE.to_owned(),
value: 2.7,
verbosity: VerbosityLevel::INFO,
}
}
}
impl RollatorApp {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customize the look and feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
pub fn restore_state(&mut self, cc: &eframe::CreationContext<'_>) {
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
}
let old: Self = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
self.label = old.label;
self.value = old.value;
}
}
/// Called once before the first frame.
#[allow(unused)] // can be used for starting with wasm
pub fn new() -> Self {
Default::default()
}
pub fn new_with_cli(cc: &eframe::CreationContext<'_>) -> Self {
let app = Self::parse();
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
}
app
pub fn new_with_cli() -> Self {
Self::parse()
}
}
@ -76,6 +79,11 @@ impl eframe::App for RollatorApp {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
});
ui.menu_button("AAAA", |ui| {
if ui.button("Quit").clicked() {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
});
ui.add_space(16.0);
}

View File

@ -6,7 +6,12 @@ mod app;
// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let mut app = app::RollatorApp::new_with_cli();
let _logger = libpt::log::Logger::builder()
.set_level(app.verbosity.level())
.build()
.expect("could not init logging");
let native_options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
@ -18,7 +23,10 @@ fn main() -> eframe::Result {
eframe::run_native(
app::TITLE,
native_options,
Box::new(|cc| Ok(Box::new(app::RollatorApp::new_with_cli(cc)))),
Box::new(|cc| {
app.restore_state(cc);
Ok(Box::new(app))
}),
)
}