From e1109fff156350894164e4310dd320a9bdd96371 Mon Sep 17 00:00:00 2001 From: veeso Date: Fri, 16 Jul 2021 15:32:39 +0200 Subject: [PATCH] From now on, if you try to leave setup without making any change, you won't be prompted whether to save configuration or not --- CHANGELOG.md | 1 + src/ui/activities/setup/actions.rs | 20 ++++++++++++++++++-- src/ui/activities/setup/mod.rs | 24 ++++++++++++++++++++++++ src/ui/activities/setup/update.rs | 17 +++++++++++------ src/ui/activities/setup/view/mod.rs | 4 +++- 5 files changed, 57 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8faa6ed..d1fb530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Released on FIXME: ?? - Replaced all `...` with `…` in texts - Check if remote host is valid in authentication form - Check if port number is valid in authentication form + - From now on, if you try to leave setup without making any change, you won't be prompted whether to save configuration or not - Bugfix: - Fixed broken input cursor when typing UTF8 characters (tui-realm 0.3.2) - Fixed save bookmark dialog: you could switch out from dialog with `` diff --git a/src/ui/activities/setup/actions.rs b/src/ui/activities/setup/actions.rs index 7b283de..7bf2b3f 100644 --- a/src/ui/activities/setup/actions.rs +++ b/src/ui/activities/setup/actions.rs @@ -36,12 +36,28 @@ use tuirealm::tui::style::Color; use tuirealm::{Payload, Value}; impl SetupActivity { + /// ### action_on_esc + /// + /// On , if there are changes in the configuration, the quit dialog must be shown, otherwise + /// we can exit without any problem + pub(super) fn action_on_esc(&mut self) { + if self.config_changed() { + self.mount_quit(); + } else { + self.exit_reason = Some(super::ExitReason::Quit); + } + } + /// ### action_save_all /// - /// Save all configurations. If current tab can load values, they will be loaded, otherwise they'll just be saved + /// Save all configurations. If current tab can load values, they will be loaded, otherwise they'll just be saved. + /// Once all the configuration has been changed, set config_changed to false pub(super) fn action_save_all(&mut self) -> Result<(), String> { self.action_save_config()?; - self.action_save_theme() + self.action_save_theme()?; + // Set config changed to false + self.set_config_changed(false); + Ok(()) } /// ### action_save_config diff --git a/src/ui/activities/setup/mod.rs b/src/ui/activities/setup/mod.rs index f93cf75..1a07b39 100644 --- a/src/ui/activities/setup/mod.rs +++ b/src/ui/activities/setup/mod.rs @@ -100,6 +100,9 @@ const COMPONENT_COLOR_TRANSFER_STATUS_SORTING: &str = "COMPONENT_COLOR_TRANSFER_ const COMPONENT_COLOR_TRANSFER_STATUS_HIDDEN: &str = "COMPONENT_COLOR_TRANSFER_STATUS_HIDDEN"; const COMPONENT_COLOR_TRANSFER_STATUS_SYNC: &str = "COMPONENT_COLOR_TRANSFER_STATUS_SYNC"; +// -- store +const STORE_CONFIG_CHANGED: &str = "SETUP_CONFIG_CHANGED"; + /// ### ViewLayout /// /// Current view layout @@ -167,6 +170,25 @@ impl SetupActivity { fn theme_provider(&mut self) -> &mut ThemeProvider { self.context_mut().theme_provider_mut() } + + /// ### config_changed + /// + /// Returns whether config has changed + fn config_changed(&self) -> bool { + self.context() + .store() + .get_boolean(STORE_CONFIG_CHANGED) + .unwrap_or(false) + } + + /// ### set_config_changed + /// + /// Set value for config changed key into the store + fn set_config_changed(&mut self, changed: bool) { + self.context_mut() + .store_mut() + .set_boolean(STORE_CONFIG_CHANGED, changed); + } } impl Activity for SetupActivity { @@ -180,6 +202,8 @@ impl Activity for SetupActivity { self.context = Some(context); // Clear terminal self.context.as_mut().unwrap().clear_screen(); + // Set config changed to false + self.set_config_changed(false); // Put raw mode on enabled if let Err(err) = enable_raw_mode() { error!("Failed to enter raw mode: {}", err); diff --git a/src/ui/activities/setup/update.rs b/src/ui/activities/setup/update.rs index 53c92a7..0f70114 100644 --- a/src/ui/activities/setup/update.rs +++ b/src/ui/activities/setup/update.rs @@ -182,6 +182,12 @@ impl SetupActivity { None } (COMPONENT_RADIO_SAVE, _) => None, + // Detect config changed + (_, Msg::OnChange(_)) => { + // An input field has changed value; report config changed + self.set_config_changed(true); + None + } // Show help (_, &MSG_KEY_CTRL_H) => { // Show help @@ -211,8 +217,7 @@ impl SetupActivity { } // (_, &MSG_KEY_ESC) => { - // Mount quit prompt - self.mount_quit(); + self.action_on_esc(); None } (_, _) => None, // Nothing to do @@ -380,8 +385,7 @@ impl SetupActivity { } // (_, &MSG_KEY_ESC) => { - // Mount quit prompt - self.mount_quit(); + self.action_on_esc(); None } (_, _) => None, // Nothing to do @@ -614,6 +618,8 @@ impl SetupActivity { (component, Msg::OnChange(Payload::One(Value::Str(color)))) => { if let Some(color) = parse_color(color) { self.action_save_color(component, color); + // Set unsaved changes to true + self.set_config_changed(true); } None } @@ -698,8 +704,7 @@ impl SetupActivity { } // (_, &MSG_KEY_ESC) => { - // Mount quit prompt - self.mount_quit(); + self.action_on_esc(); None } (_, _) => None, // Nothing to do diff --git a/src/ui/activities/setup/view/mod.rs b/src/ui/activities/setup/view/mod.rs index 1288913..a4b6784 100644 --- a/src/ui/activities/setup/view/mod.rs +++ b/src/ui/activities/setup/view/mod.rs @@ -111,7 +111,9 @@ impl SetupActivity { .with_inverted_color(Color::Black) .with_borders(Borders::ALL, BorderType::Rounded, Color::LightRed) .with_options( - Some(String::from("Exit setup?")), + Some(String::from( + "There are unsaved changes! Save changes before leaving?", + )), vec![ String::from("Save"), String::from("Don't save"),