From fa81cd360a2786cb41dbe847cfa36e4647fd422d Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Tue, 1 Oct 2024 13:31:18 +0200 Subject: [PATCH] egui: get rid of unnecessary persistence of langid, separate into Config struct --- src/bin/topola-egui/app.rs | 39 +++++++++-------------------------- src/bin/topola-egui/config.rs | 10 +++++++++ src/bin/topola-egui/main.rs | 20 ++++++++++-------- 3 files changed, 31 insertions(+), 38 deletions(-) create mode 100644 src/bin/topola-egui/config.rs diff --git a/src/bin/topola-egui/app.rs b/src/bin/topola-egui/app.rs index 7f7493b..5c362c7 100644 --- a/src/bin/topola-egui/app.rs +++ b/src/bin/topola-egui/app.rs @@ -16,6 +16,7 @@ use topola::{ use crate::{ activity::{ActivityStatus, ActivityStepperWithStatus}, + config::Config, error_dialog::ErrorDialog, file_receiver::FileReceiver, layers::Layers, @@ -26,52 +27,36 @@ use crate::{ viewport::Viewport, }; -/// Deserialize/Serialize is needed to persist app state between restarts. -#[derive(Serialize, Deserialize)] -#[serde(default)] pub struct App { + config: Config, translator: Translator, - #[serde(skip)] maybe_overlay: Option, - #[serde(skip)] arc_mutex_maybe_invoker: Arc>>>, - #[serde(skip)] maybe_activity: Option, - #[serde(skip)] content_channel: (Sender, Receiver), - - #[serde(skip)] history_channel: (Sender, Receiver), - #[serde(skip)] viewport: Viewport, - #[serde(skip)] menu_bar: MenuBar, - - #[serde(skip)] status_bar: StatusBar, - #[serde(skip)] error_dialog: ErrorDialog, - #[serde(skip)] maybe_layers: Option, - - #[serde(skip)] maybe_design: Option, - #[serde(skip)] update_counter: f32, } impl Default for App { fn default() -> Self { Self { + config: Config::default(), translator: Translator::new(langid!("en-US")), maybe_overlay: None, arc_mutex_maybe_invoker: Arc::new(Mutex::new(None)), @@ -92,19 +77,15 @@ impl Default for App { impl App { /// Called once on start. pub fn new(cc: &eframe::CreationContext<'_>, langid: LanguageIdentifier) -> Self { - // Load previous app state if one exists. - if let Some(storage) = cc.storage { - let this = Self { - translator: Translator::new(langid), - ..eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default() - }; - return this; - } - - Self { + let mut this = Self { translator: Translator::new(langid), ..Default::default() + }; + // Load previous app state if one exists. + if let Some(storage) = cc.storage { + this.config = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default() } + this } fn advance_state_by_dt(&mut self, dt: f32) { @@ -196,7 +177,7 @@ impl App { impl eframe::App for App { /// Called to save state before shutdown. fn save(&mut self, storage: &mut dyn eframe::Storage) { - eframe::set_value(storage, eframe::APP_KEY, self); + eframe::set_value(storage, eframe::APP_KEY, &self.config); } /// Called each time the UI has to be repainted. diff --git a/src/bin/topola-egui/config.rs b/src/bin/topola-egui/config.rs new file mode 100644 index 0000000..58ad7d9 --- /dev/null +++ b/src/bin/topola-egui/config.rs @@ -0,0 +1,10 @@ +#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] +#[serde(default)] +pub struct Config { +} + +impl Default for Config { + fn default() -> Self { + Self {} + } +} diff --git a/src/bin/topola-egui/main.rs b/src/bin/topola-egui/main.rs index af5c335..ab32be0 100644 --- a/src/bin/topola-egui/main.rs +++ b/src/bin/topola-egui/main.rs @@ -3,6 +3,7 @@ mod action; mod activity; mod app; +mod config; mod error_dialog; mod file_receiver; mod file_sender; @@ -16,18 +17,19 @@ mod viewport; use app::App; use sys_locale::get_locale; -use unic_langid::langid; +use unic_langid::{LanguageIdentifier, langid}; + +fn get_langid() -> LanguageIdentifier { + get_locale() + .and_then(|langname| langname.parse().ok()) + .unwrap_or(langid!("en-US")) +} // Build to native. #[cfg(not(target_arch = "wasm32"))] fn main() -> eframe::Result<()> { env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). - - let langid = if let Some(langname) = get_locale() { - langname.parse().unwrap_or(langid!("en-US")) - } else { - langid!("en-US") - }; + let langid = get_langid(); let native_options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() @@ -49,7 +51,6 @@ fn main() { // Redirect `log` message to `console.log`: eframe::WebLogger::init(log::LevelFilter::Debug).ok(); - let web_options = eframe::WebOptions::default(); wasm_bindgen_futures::spawn_local(async { @@ -64,11 +65,12 @@ fn main() { .dyn_into::() .expect("topola-egui was not a HtmlCanvasElement"); + let langid = get_langid(); let start_result = eframe::WebRunner::new() .start( canvas, web_options, - Box::new(|cc| Ok(Box::new(App::new(cc, langid!("en-US"))))), + Box::new(|cc| Ok(Box::new(App::new(cc, langid)))), ) .await;