From b5ad10ad6d6eb2dab7eff0db23bdbd923e03e39f Mon Sep 17 00:00:00 2001 From: Uncle Stretch Date: Tue, 3 Dec 2024 20:57:35 +0300 Subject: [PATCH] make wallet mod code prettier Signed-off-by: Uncle Stretch --- src/components/explorer/mod.rs | 57 +++++----- src/components/wallet/accounts.rs | 1 + src/components/wallet/add_account.rs | 6 +- src/components/wallet/mod.rs | 143 +++++++++--------------- src/components/wallet/rename_account.rs | 2 +- 5 files changed, 88 insertions(+), 121 deletions(-) diff --git a/src/components/explorer/mod.rs b/src/components/explorer/mod.rs index 48367af..1348272 100644 --- a/src/components/explorer/mod.rs +++ b/src/components/explorer/mod.rs @@ -104,35 +104,34 @@ impl Component for Explorer { } fn handle_key_event(&mut self, key: KeyEvent) -> Result> { - if self.is_active { - match key.code { - KeyCode::Esc => { - self.is_active = false; - self.current_tab = CurrentTab::Nothing; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - return Ok(Some(Action::SetActiveScreen(Mode::Menu))); - }, - KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { - self.move_right(); - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - KeyCode::Char('h') | KeyCode::Left => { - self.move_left(); - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - _ => { - for component in self.components.iter_mut() { - component.handle_key_event(key)?; - } - }, - } - } + if !self.is_active { return Ok(None); } + match key.code { + KeyCode::Esc => { + self.is_active = false; + self.current_tab = CurrentTab::Nothing; + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + return Ok(Some(Action::SetActiveScreen(Mode::Menu))); + }, + KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { + self.move_right(); + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + KeyCode::Char('h') | KeyCode::Left => { + self.move_left(); + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + _ => { + for component in self.components.iter_mut() { + component.handle_key_event(key)?; + } + }, + } Ok(None) } diff --git a/src/components/wallet/accounts.rs b/src/components/wallet/accounts.rs index bc60e75..dc9ead3 100644 --- a/src/components/wallet/accounts.rs +++ b/src/components/wallet/accounts.rs @@ -69,6 +69,7 @@ impl Accounts { .to_ss58check_with_version(Ss58AddressFormat::custom(1996)); self.wallet_keys.push((name, address, secret_seed, pair_signer)); + self.last_row(); self.save_to_file(); } diff --git a/src/components/wallet/add_account.rs b/src/components/wallet/add_account.rs index 5b61b9d..7f2f441 100644 --- a/src/components/wallet/add_account.rs +++ b/src/components/wallet/add_account.rs @@ -66,10 +66,12 @@ impl AddAccount { impl PartialComponent for AddAccount { fn set_active(&mut self, current_tab: CurrentTab) { - self.name = Input::new(String::new()); match current_tab { CurrentTab::AddAccount => self.is_active = true, - _ => self.is_active = false, + _ => { + self.is_active = false; + self.name = Input::new(String::new()); + }, }; } } diff --git a/src/components/wallet/mod.rs b/src/components/wallet/mod.rs index 630cfaf..f6e3003 100644 --- a/src/components/wallet/mod.rs +++ b/src/components/wallet/mod.rs @@ -81,10 +81,6 @@ impl Wallet { } } -impl PartialComponent for Wallet { - fn set_active(&mut self, _current_tab: CurrentTab) {} -} - impl Component for Wallet { fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { for component in self.components.iter_mut() { @@ -103,101 +99,70 @@ impl Component for Wallet { } fn handle_key_event(&mut self, key: KeyEvent) -> Result> { - if self.is_active { - if self.current_tab == CurrentTab::AddAccount { - match key.code { - KeyCode::Esc => { - self.current_tab = CurrentTab::Accounts; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - _ => { - for component in self.components.iter_mut() { - component.handle_key_event(key)?; - } + if !self.is_active { return Ok(None) } + + match self.current_tab { + // block the default key handle for popups + CurrentTab::AddAccount | CurrentTab::RenameAccount => match key.code { + KeyCode::Esc => { + self.current_tab = CurrentTab::Accounts; + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + _ => { + for component in self.components.iter_mut() { + component.handle_key_event(key)?; } } - } else if self.current_tab == CurrentTab::RenameAccount { - match key.code { - KeyCode::Esc => { - self.current_tab = CurrentTab::Accounts; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - _ => { - for component in self.components.iter_mut() { - component.handle_key_event(key)?; - } + }, + _ => match key.code { + KeyCode::Esc => { + self.is_active = false; + self.current_tab = CurrentTab::Nothing; + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); } - } - } else { - match key.code { - KeyCode::Esc => { - self.is_active = false; - self.current_tab = CurrentTab::Nothing; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - return Ok(Some(Action::SetActiveScreen(Mode::Menu))); - }, - KeyCode::Char('W') => { - self.current_tab = CurrentTab::AddAccount; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { - self.move_right(); - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - KeyCode::Char('h') | KeyCode::Left => { - self.move_left(); - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - }, - _ => { - for component in self.components.iter_mut() { - component.handle_key_event(key)?; - } - }, - } + return Ok(Some(Action::SetActiveScreen(Mode::Menu))); + }, + KeyCode::Char('W') => { + self.current_tab = CurrentTab::AddAccount; + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { + self.move_right(); + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + KeyCode::Char('h') | KeyCode::Left => { + self.move_left(); + for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); + } + }, + _ => { + for component in self.components.iter_mut() { + component.handle_key_event(key)?; + } + }, } - } + } Ok(None) } fn update(&mut self, action: Action) -> Result> { - if let Action::SetActiveScreen(Mode::Wallet) = action { - self.is_active = true; - self.current_tab = CurrentTab::Accounts; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - } - if let Action::NewAccount(_) = action { - self.current_tab = CurrentTab::Accounts; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - } - if let Action::UpdateAccountName(_) = action { - self.current_tab = CurrentTab::Accounts; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } - } - if let Action::RenameAccount(_) = action { - self.current_tab = CurrentTab::RenameAccount; - for component in self.components.iter_mut() { - component.set_active(self.current_tab.clone()); - } + match action { + Action::SetActiveScreen(Mode::Wallet) => self.is_active = true, + Action::UpdateAccountName(_) | Action::NewAccount(_) => + self.current_tab = CurrentTab::Accounts, + Action::RenameAccount(_) => self.current_tab = CurrentTab::RenameAccount, + _ => {} } for component in self.components.iter_mut() { + component.set_active(self.current_tab.clone()); component.update(action.clone())?; } Ok(None) diff --git a/src/components/wallet/rename_account.rs b/src/components/wallet/rename_account.rs index 1202d9d..1948dd7 100644 --- a/src/components/wallet/rename_account.rs +++ b/src/components/wallet/rename_account.rs @@ -73,9 +73,9 @@ impl PartialComponent for RenameAccount { _ => { self.is_active = false; self.old_name = String::new(); + self.name = Input::new(String::new()); } }; - self.name = Input::new(String::new()); } }