Skip to content

Commit

Permalink
chore: move all the controls to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaRogachev committed Sep 30, 2024
1 parent 486eca9 commit 046f3c1
Show file tree
Hide file tree
Showing 21 changed files with 842 additions and 892 deletions.
1 change: 1 addition & 0 deletions src/core/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Token {
}

impl Token {
#[allow(dead_code)]
pub fn new(name: &str, symbol: &str, address: &str, decimals: u16) -> Self {
Self {
name: name.to_string(),
Expand Down
48 changes: 24 additions & 24 deletions src/tui/popups/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::{
};

use crate::{core::chain, service::crypto::Crypto};
use crate::tui::{app::AppScreen, widgets::{buttons, options}};
use crate::tui::{app::AppScreen, widgets::controls};

const TITLE: &str = "Active Networks";
const TESTNET_WARNING: &str = "Testnet mode";
Expand All @@ -22,40 +22,40 @@ pub struct Popup {
is_testnet: bool,
update_required: bool,
has_changes: bool,
mainnet_options: options::CheckOptions<chain::Chain>,
testnet_options: options::CheckOptions<chain::Chain>,
back_button: buttons::Button,
restore_button: buttons::Button,
save_button: buttons::Button,
mainnet_check_list: controls::CheckList<chain::Chain>,
testnet_check_list: controls::CheckList<chain::Chain>,
back_button: controls::Button,
restore_button: controls::Button,
save_button: controls::Button,
}

impl Popup {
pub fn new(crypto: Arc<Mutex<Crypto>>) -> Self {
let mainnet_options = chain::MAINNET_CHAINS.iter().map(|chain| {
let name = chain.get_display_name();
let hotkey = name.chars().next();
(chain.clone(), options::CheckBox::new(name, false, hotkey))
(chain.clone(), controls::CheckBox::new(name, false, hotkey))
}).collect();
let mainnet_options = options::CheckOptions::new(mainnet_options);
let mainnet_check_list = controls::CheckList::new(mainnet_options);

let testnet_options = chain::TESTNET_CHAINS.iter().map(|chain| {
let name = chain.get_display_name();
let hotkey = name.chars().next();
(chain.clone(), options::CheckBox::new(name, false, hotkey).warning())
(chain.clone(), controls::CheckBox::new(name, false, hotkey).warning())
}).collect();
let testnet_options = options::CheckOptions::new(testnet_options);
let testnet_check_list = controls::CheckList::new(testnet_options);

let back_button = buttons::Button::new("Back", Some('b'));
let restore_button = buttons::Button::new("Restore", Some('r')).disable();
let save_button = buttons::Button::new("Save", Some('s')).disable();
let back_button = controls::Button::new("Back", Some('b'));
let restore_button = controls::Button::new("Restore", Some('r')).disable();
let save_button = controls::Button::new("Save", Some('s')).disable();

Self {
crypto,
is_testnet: false,
update_required: true,
has_changes: false,
mainnet_options,
testnet_options,
mainnet_check_list,
testnet_check_list,
back_button,
restore_button,
save_button
Expand All @@ -67,14 +67,14 @@ impl Popup {
let active_networks = crypto.get_active_networks();
self.is_testnet = crypto.in_testnet();

self.mainnet_options.toggle_by_keys(&active_networks);
self.testnet_options.toggle_by_keys(&active_networks);
self.mainnet_check_list.toggle_by_keys(&active_networks);
self.testnet_check_list.toggle_by_keys(&active_networks);
}

async fn save_options(&mut self) {
let mut crypto = self.crypto.lock().await;
let mut all_networks = self.mainnet_options.get_checked_keys();
all_networks.extend(self.testnet_options.get_checked_keys());
let mut all_networks = self.mainnet_check_list.get_checked_keys();
all_networks.extend(self.testnet_check_list.get_checked_keys());
crypto.save_active_networks(all_networks)
.await.expect("Failed to save active networks");
}
Expand All @@ -83,12 +83,12 @@ impl Popup {
#[async_trait::async_trait]
impl AppScreen for Popup {
async fn handle_event(&mut self, event: Event) -> anyhow::Result<bool> {
if let Some(_) = self.mainnet_options.handle_event(&event) {
if let Some(_) = self.mainnet_check_list.handle_event(&event) {
self.has_changes = true;
return Ok(false);
}

if let Some(_) = self.testnet_options.handle_event(&event) {
if let Some(_) = self.testnet_check_list.handle_event(&event) {
self.has_changes = true;
return Ok(false);
}
Expand Down Expand Up @@ -136,7 +136,7 @@ impl AppScreen for Popup {
Constraint::Length(1), // Margin
Constraint::Length(WARNING_HEIGHT),
Constraint::Fill(0), // Fill height for network options
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(inner_area);

Expand All @@ -156,8 +156,8 @@ impl AppScreen for Popup {
Constraint::Percentage(10),
])
.split(content_layout[2]);
self.mainnet_options.render(frame, options_layout[1]);
self.testnet_options.render(frame, options_layout[2]);
self.mainnet_check_list.render(frame, options_layout[1]);
self.testnet_check_list.render(frame, options_layout[2]);

let buttons_layout = Layout::default()
.direction(Direction::Horizontal)
Expand Down
12 changes: 6 additions & 6 deletions src/tui/popups/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use ratatui::{
Frame
};

use crate::tui::{widgets::buttons, app::AppScreen};
use crate::tui::{widgets::controls, app::AppScreen};

const ACCOUNT_HEIGHT: u16 = 2;

const TITLE: &str = "Receive Crypto";

pub struct Popup {
address: web3::types::Address,
back_button: buttons::Button,
copy_button: buttons::Button
back_button: controls::Button,
copy_button: controls::Button
}

impl Popup {
pub fn new(address: web3::types::Address) -> Self {
let back_button = buttons::Button::new("Back", Some('b'));
let copy_button = buttons::Button::new("Copy To Clipboard", Some('c'));
let back_button = controls::Button::new("Back", Some('b'));
let copy_button = controls::Button::new("Copy To Clipboard", Some('c'));

Self {
address,
Expand Down Expand Up @@ -77,7 +77,7 @@ impl AppScreen for Popup {
Constraint::Length(1), // Margin
Constraint::Length(ACCOUNT_HEIGHT),
Constraint::Fill(0), // Fill height for QR code
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(inner_area);

Expand Down
28 changes: 14 additions & 14 deletions src/tui/screens/account_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ratatui::{

use crate::core::seed_phrase::{SeedPhrase, WordCount};
use crate::tui::app::{AppCommand, AppScreen};
use crate::tui::widgets::{buttons, mnemonic};
use crate::tui::widgets::{controls, mnemonic};

const MAX_CREATE_WIDTH: u16 = 80;
const INTRO_HEIGHT: u16 = 1;
Expand All @@ -20,24 +20,24 @@ pub struct Screen {
command_tx: mpsc::Sender<AppCommand>,
seed_phrase: SeedPhrase,

word_cnt_switch: buttons::MultiSwitch,
word_cnt_switch: controls::MultiSwitch,
mnemonic_words: mnemonic::MnemonicWords,
back_button: buttons::Button,
reveal_button: buttons::SwapButton,
secure_button: buttons::Button,
back_button: controls::Button,
reveal_button: controls::SwapButton,
secure_button: controls::Button,
}

impl Screen {
pub fn new(command_tx: mpsc::Sender<AppCommand>, seed_phrase: SeedPhrase) -> Self {
let word_cnt_switch = buttons::MultiSwitch::new(vec![
buttons::Button::new("12 words", Some('1')), buttons::Button::new("24 words", Some('2'))]);
let word_cnt_switch = controls::MultiSwitch::new(vec![
controls::Button::new("12 words", Some('1')), controls::Button::new("24 words", Some('2'))]);
let mnemonic_words = mnemonic::MnemonicWords::new(seed_phrase.get_words_zeroizing());
let back_button = buttons::Button::new("Back", Some('b'));
let reveal_button = buttons::SwapButton::new(
buttons::Button::new("Reveal", Some('r')).warning(),
buttons::Button::new("Hide", Some('h')).primary(),
let back_button = controls::Button::new("Back", Some('b'));
let reveal_button = controls::SwapButton::new(
controls::Button::new("Reveal", Some('r')).warning(),
controls::Button::new("Hide", Some('h')).primary(),
);
let secure_button = buttons::Button::new("Secure", Some('s'));
let secure_button = controls::Button::new("Secure", Some('s'));

Self {
command_tx,
Expand Down Expand Up @@ -97,9 +97,9 @@ impl AppScreen for Screen {
.direction(Direction::Vertical)
.constraints([
Constraint::Length(INTRO_HEIGHT),
Constraint::Length(buttons::SWITCH_HEIGHT),
Constraint::Length(controls::SWITCH_HEIGHT),
Constraint::Length(mnemonic::MNEMONIC_HEIGHT),
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(centered_area);

Expand Down
12 changes: 6 additions & 6 deletions src/tui/screens/account_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ratatui::{
};

use crate::service::session::Session;
use crate::tui::{widgets::{buttons, ascii}, app::{AppCommand, AppScreen}};
use crate::tui::{widgets::{controls, ascii}, app::{AppCommand, AppScreen}};

const MAX_DELETE_WIDTH: u16 = 80;
const SKULL_HEIGHT: u16 = 20;
Expand All @@ -20,14 +20,14 @@ pub struct Screen {
command_tx: mpsc::Sender<AppCommand>,
session: Session,

cancel_button: buttons::Button,
delete_button: buttons::Button,
cancel_button: controls::Button,
delete_button: controls::Button,
}

impl Screen {
pub fn new(command_tx: mpsc::Sender<AppCommand>, session: Session) -> Self {
let cancel_button = buttons::Button::new("Cancel", Some('c')).primary();
let delete_button = buttons::Button::new("Delete Account", Some('d')).warning();
let cancel_button = controls::Button::new("Cancel", Some('c')).primary();
let delete_button = controls::Button::new("Delete Account", Some('d')).warning();

Self { command_tx, session, cancel_button, delete_button }
}
Expand Down Expand Up @@ -65,7 +65,7 @@ impl AppScreen for Screen {
Constraint::Min(0), // Fill height
Constraint::Length(SKULL_HEIGHT),
Constraint::Length(WARNING_HEIGHT),
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(centered_area);

Expand Down
20 changes: 10 additions & 10 deletions src/tui/screens/account_import_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use zeroize::Zeroizing;

use crate::core::seed_phrase::{WordCount, SeedPhrase};
use crate::tui::app::{AppCommand, AppScreen};
use crate::tui::widgets::{buttons, mnemonic};
use crate::tui::widgets::{controls, mnemonic};

const MAX_IMPORT_WIDTH: u16 = 80;
const INTRO_HEIGHT: u16 = 2;
Expand All @@ -24,9 +24,9 @@ pub struct Screen {
word_count: WordCount,

mnemonic_words: mnemonic::MnemonicWords,
back_button: buttons::Button,
reveal_button: buttons::SwapButton,
secure_button: buttons::Button,
back_button: controls::Button,
reveal_button: controls::SwapButton,
secure_button: controls::Button,
}

impl Screen {
Expand All @@ -38,12 +38,12 @@ impl Screen {
};

let mut mnemonic_words = mnemonic::MnemonicWords::new(words);
let back_button = buttons::Button::new("Back", Some('b'));
let reveal_button = buttons::SwapButton::new(
buttons::Button::new("Reveal", Some('r')).warning(),
buttons::Button::new("Hide", Some('h')).primary(),
let back_button = controls::Button::new("Back", Some('b'));
let reveal_button = controls::SwapButton::new(
controls::Button::new("Reveal", Some('r')).warning(),
controls::Button::new("Hide", Some('h')).primary(),
);
let mut secure_button = buttons::Button::new("Secure", Some('s'));
let mut secure_button = controls::Button::new("Secure", Some('s'));
if seed_phrase.is_none() {
secure_button.disabled = true;
mnemonic_words.color = Color::Red;
Expand Down Expand Up @@ -102,7 +102,7 @@ impl AppScreen for Screen {
.constraints([
Constraint::Length(INTRO_HEIGHT),
Constraint::Length(mnemonic::MNEMONIC_HEIGHT),
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(centered_area);

Expand Down
20 changes: 10 additions & 10 deletions src/tui/screens/account_import_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ratatui::{

use crate::core::seed_phrase::WordCount;
use crate::tui::app::{AppCommand, AppScreen};
use crate::tui::widgets::{buttons, ascii};
use crate::tui::widgets::{controls, ascii};

const MAX_IMPORT_WIDTH: u16 = 80;
const INTRO_HEIGHT: u16 = 3;
Expand All @@ -21,18 +21,18 @@ const OUTRO_TEXT: &str = "Next, you will be prompted to enter your seed phrase w
pub struct Screen {
command_tx: mpsc::Sender<AppCommand>,
word_count: WordCount,
word_cnt_switch: buttons::MultiSwitch,
back_button: buttons::Button,
continue_button: buttons::Button,
word_cnt_switch: controls::MultiSwitch,
back_button: controls::Button,
continue_button: controls::Button,
}

impl Screen {
pub fn new(command_tx: mpsc::Sender<AppCommand>) -> Self {
let word_count = WordCount::Words12;
let word_cnt_switch = buttons::MultiSwitch::new(vec![
buttons::Button::new("12 words", Some('1')), buttons::Button::new("24 words", Some('2'))]);
let back_button = buttons::Button::new("Back", Some('b'));
let continue_button = buttons::Button::new("Continue", Some('c'));
let word_cnt_switch = controls::MultiSwitch::new(vec![
controls::Button::new("12 words", Some('1')), controls::Button::new("24 words", Some('2'))]);
let back_button = controls::Button::new("Back", Some('b'));
let continue_button = controls::Button::new("Continue", Some('c'));

Self {
command_tx,
Expand Down Expand Up @@ -85,10 +85,10 @@ impl AppScreen for Screen {
.direction(Direction::Vertical)
.constraints([
Constraint::Length(INTRO_HEIGHT),
Constraint::Length(buttons::SWITCH_HEIGHT),
Constraint::Length(controls::SWITCH_HEIGHT),
Constraint::Fill(0), // Logo
Constraint::Length(OUTRO_HEIGHT),
Constraint::Length(buttons::BUTTONS_HEIGHT),
Constraint::Length(controls::BUTTONS_HEIGHT),
])
.split(centered_area);

Expand Down
Loading

0 comments on commit 046f3c1

Please sign in to comment.