egui: get rid of unnecessary persistence of langid, separate into Config struct

This commit is contained in:
Alain Emilia Anna Zscheile 2024-10-01 13:31:18 +02:00
parent 2f672d41c2
commit fa81cd360a
3 changed files with 31 additions and 38 deletions

View File

@ -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<Overlay>,
#[serde(skip)]
arc_mutex_maybe_invoker: Arc<Mutex<Option<Invoker<SpecctraMesadata>>>>,
#[serde(skip)]
maybe_activity: Option<ActivityStepperWithStatus>,
#[serde(skip)]
content_channel: (Sender<String>, Receiver<String>),
#[serde(skip)]
history_channel: (Sender<String>, Receiver<String>),
#[serde(skip)]
viewport: Viewport,
#[serde(skip)]
menu_bar: MenuBar,
#[serde(skip)]
status_bar: StatusBar,
#[serde(skip)]
error_dialog: ErrorDialog,
#[serde(skip)]
maybe_layers: Option<Layers>,
#[serde(skip)]
maybe_design: Option<SpecctraDesign>,
#[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.

View File

@ -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 {}
}
}

View File

@ -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::<eframe::web_sys::HtmlCanvasElement>()
.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;