From 7accc0c6093bcb85ad619a94319162345ff9b16f Mon Sep 17 00:00:00 2001 From: Leandro Coutinho Date: Sun, 13 Aug 2023 17:05:03 -0300 Subject: [PATCH] fix: make config appear when there is no local config `toggle_config` is initialized to false, and then passed to `.open(toggle_config)`, so the config does not appear. This change also fixes closing the config window when the user press Enter, see: https://github.com/emilk/egui/issues/805#issuecomment-1479864586 --- headlines/src/headlines.rs | 11 +++++------ headlines/src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/headlines/src/headlines.rs b/headlines/src/headlines.rs index d8e2227..e70dce1 100644 --- a/headlines/src/headlines.rs +++ b/headlines/src/headlines.rs @@ -47,7 +47,6 @@ impl Default for HeadlinesConfig { pub struct Headlines { pub articles: Vec, pub config: HeadlinesConfig, - pub api_key_initialized: bool, pub toggle_config: bool, pub toggle_about: bool, pub news_rx: Option>, @@ -64,7 +63,7 @@ impl Headlines { pub fn init(mut self, cc: &CreationContext) -> Self { if let Some(storage) = cc.storage { self.config = eframe::get_value(storage, APP_NAME).unwrap_or_default(); - self.api_key_initialized = !self.config.api_key.is_empty(); + self.toggle_config = self.config.api_key.is_empty(); } let api_key = self.config.api_key.to_string(); @@ -73,7 +72,6 @@ impl Headlines { #[allow(unused_mut)] let (mut news_tx, news_rx) = channel(); self.news_rx = Some(news_rx); - self.toggle_config = false; #[cfg(not(target_arch = "wasm32"))] { @@ -264,11 +262,11 @@ impl Headlines { toggle_config, config, app_tx, - api_key_initialized, .. } = self; + let mut show_config = *toggle_config; Window::new("App configuration") - .open(toggle_config) + .open(&mut show_config) .show(ctx, |ui| { ui.label("Enter you API_KEY for newsapi.org"); let text_input = ui.text_edit_singleline(&mut config.api_key); @@ -277,13 +275,14 @@ impl Headlines { tx.send(Msg::ApiKeySet(config.api_key.to_string())) .expect("Failed sending ApiKeySet event"); } - *api_key_initialized = true; + *toggle_config = false; tracing::info!("API_KEY set"); ui.close_menu(); } ui.label("Don't have the API_KEY? register at:"); ui.hyperlink("https://newsapi.org/register"); }); + *toggle_config &= show_config; }); } diff --git a/headlines/src/lib.rs b/headlines/src/lib.rs index c990794..24be50a 100644 --- a/headlines/src/lib.rs +++ b/headlines/src/lib.rs @@ -25,7 +25,7 @@ impl App for Headlines { return; } - if self.toggle_config || !self.api_key_initialized { + if self.toggle_config { self.render_config(ctx); } else { self.preload_articles();