Save both theme and config at the same time

This commit is contained in:
veeso
2021-07-15 12:47:47 +02:00
parent 8277c80860
commit 61f6901767
2 changed files with 52 additions and 16 deletions

View File

@@ -27,7 +27,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
// Locals // Locals
use super::SetupActivity; use super::{SetupActivity, ViewLayout};
// Ext // Ext
use crate::config::themes::Theme; use crate::config::themes::Theme;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
@@ -36,25 +36,55 @@ use tuirealm::tui::style::Color;
use tuirealm::{Payload, Value}; use tuirealm::{Payload, Value};
impl SetupActivity { impl SetupActivity {
/// ### action_save_all
///
/// Save all configurations. If current tab can load values, they will be loaded, otherwise they'll just be saved
pub(super) fn action_save_all(&mut self) -> Result<(), String> {
self.action_save_config()?;
self.action_save_theme()
}
/// ### action_save_config /// ### action_save_config
/// ///
/// Save configuration /// Save configuration
pub(super) fn action_save_config(&mut self) -> Result<(), String> { fn action_save_config(&mut self) -> Result<(), String> {
// Collect input values // Collect input values if in setup form
if self.layout == ViewLayout::SetupForm {
self.collect_input_values(); self.collect_input_values();
}
self.save_config() self.save_config()
} }
/// ### action_save_theme /// ### action_save_theme
/// ///
/// Save configuration /// Save configuration
pub(super) fn action_save_theme(&mut self) -> Result<(), String> { fn action_save_theme(&mut self) -> Result<(), String> {
// save theme // Collect input values if in theme form
if self.layout == ViewLayout::Theme {
self.collect_styles() self.collect_styles()
.map_err(|e| format!("'{}' has an invalid color", e))?; .map_err(|e| format!("'{}' has an invalid color", e))?;
}
// save theme
self.save_theme() self.save_theme()
} }
/// ### action_change_tab
///
/// Change view tab and load input values in order not to lose them
pub(super) fn action_change_tab(&mut self, new_tab: ViewLayout) -> Result<(), String> {
// load values for current tab first
match self.layout {
ViewLayout::SetupForm => self.collect_input_values(),
ViewLayout::Theme => self
.collect_styles()
.map_err(|e| format!("'{}' has an invalid color", e))?,
_ => {}
}
// Update view
self.init(new_tab);
Ok(())
}
/// ### action_reset_config /// ### action_reset_config
/// ///
/// Reset configuration input fields /// Reset configuration input fields

View File

@@ -141,7 +141,7 @@ impl SetupActivity {
// Exit // Exit
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save changes // Save changes
if let Err(err) = self.action_save_config() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
// Exit // Exit
@@ -170,7 +170,7 @@ impl SetupActivity {
// Save popup // Save popup
(COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save config // Save config
if let Err(err) = self.action_save_config() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
self.umount_save_popup(); self.umount_save_popup();
@@ -190,7 +190,9 @@ impl SetupActivity {
} }
(_, &MSG_KEY_TAB) => { (_, &MSG_KEY_TAB) => {
// Change view // Change view
self.init(ViewLayout::SshKeys); if let Err(err) = self.action_change_tab(ViewLayout::SshKeys) {
self.mount_error(err.as_str());
}
None None
} }
// <CTRL+R> Revert changes // <CTRL+R> Revert changes
@@ -234,7 +236,7 @@ impl SetupActivity {
// Exit // Exit
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save changes // Save changes
if let Err(err) = self.action_save_config() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
// Exit // Exit
@@ -279,7 +281,7 @@ impl SetupActivity {
// Save popup // Save popup
(COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save config // Save config
if let Err(err) = self.action_save_config() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
self.umount_save_popup(); self.umount_save_popup();
@@ -357,7 +359,9 @@ impl SetupActivity {
} }
(_, &MSG_KEY_TAB) => { (_, &MSG_KEY_TAB) => {
// Change view // Change view
self.init(ViewLayout::Theme); if let Err(err) = self.action_change_tab(ViewLayout::Theme) {
self.mount_error(err.as_str());
}
None None
} }
// <CTRL+R> Revert changes // <CTRL+R> Revert changes
@@ -623,7 +627,7 @@ impl SetupActivity {
// Exit // Exit
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save changes // Save changes
if let Err(err) = self.action_save_theme() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
// Exit // Exit
@@ -652,7 +656,7 @@ impl SetupActivity {
// Save popup // Save popup
(COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => { (COMPONENT_RADIO_SAVE, Msg::OnSubmit(Payload::One(Value::Usize(0)))) => {
// Save config // Save config
if let Err(err) = self.action_save_theme() { if let Err(err) = self.action_save_all() {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
self.umount_save_popup(); self.umount_save_popup();
@@ -673,7 +677,9 @@ impl SetupActivity {
} }
(_, &MSG_KEY_TAB) => { (_, &MSG_KEY_TAB) => {
// Change view // Change view
self.init(ViewLayout::SetupForm); if let Err(err) = self.action_change_tab(ViewLayout::SetupForm) {
self.mount_error(err.as_str());
}
None None
} }
// <CTRL+R> Revert changes // <CTRL+R> Revert changes