use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use color_eyre::Result; use ratatui::{ layout::{Position, Alignment, Constraint, Flex, Layout, Rect}, widgets::{Block, Clear, Paragraph}, Frame }; use tokio::sync::mpsc::UnboundedSender; use std::sync::mpsc::Sender; use super::{Component, PartialComponent, CurrentTab}; use crate::{ action::Action, config::Config, palette::StylePalette, types::{ActionLevel, ActionTarget}, widgets::{Input, InputRequest}, }; #[derive(Debug)] pub struct RebondPopup { is_active: bool, action_tx: Option>, network_tx: Option>, secret_seed: [u8; 32], amount: Input, palette: StylePalette } impl Default for RebondPopup { fn default() -> Self { Self::new() } } impl RebondPopup { pub fn new() -> Self { Self { is_active: false, secret_seed: [0u8; 32], action_tx: None, network_tx: None, amount: Input::new(String::new()), palette: StylePalette::default(), } } } impl RebondPopup { fn log_event(&mut self, message: String, level: ActionLevel) { if let Some(action_tx) = &self.action_tx { let _ = action_tx.send( Action::EventLog(message, level, ActionTarget::ValidatorLog)); } } fn submit_message(&mut self) { if let Some(network_tx) = &self.network_tx { match self.amount.value().parse::() { Ok(value) => { let amount = (value * 1_000_000_000_000_000_000.0) as u128; let _ = network_tx.send(Action::RebondFrom(self.secret_seed, amount)); if let Some(action_tx) = &self.action_tx { let _ = action_tx.send(Action::ClosePopup); } }, Err(err) => self.log_event( format!("invalid amount, error: {err}"), ActionLevel::Error), } } } fn enter_char(&mut self, new_char: char) { let is_separator_needed = !self.amount.value().contains('.') && new_char == '.'; if new_char.is_digit(10) || is_separator_needed { let _ = self.amount.handle(InputRequest::InsertChar(new_char)); } } fn delete_char(&mut self) { let _ = self.amount.handle(InputRequest::DeletePrevChar); } fn move_cursor_right(&mut self) { let _ = self.amount.handle(InputRequest::GoToNextChar); } fn move_cursor_left(&mut self) { let _ = self.amount.handle(InputRequest::GoToPrevChar); } } impl PartialComponent for RebondPopup { fn set_active(&mut self, current_tab: CurrentTab) { match current_tab { CurrentTab::RebondPopup => self.is_active = true, _ => { self.is_active = false; self.amount = Input::new(String::new()); } }; } } impl Component for RebondPopup { fn register_network_handler(&mut self, tx: Sender) -> Result<()> { self.network_tx = Some(tx); Ok(()) } fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { self.action_tx = Some(tx); Ok(()) } fn register_config_handler(&mut self, config: Config) -> Result<()> { if let Some(style) = config.styles.get(&crate::app::Mode::Wallet) { self.palette.with_normal_style(style.get("normal_style").copied()); self.palette.with_normal_border_style(style.get("normal_border_style").copied()); self.palette.with_normal_title_style(style.get("normal_title_style").copied()); self.palette.with_popup_style(style.get("popup_style").copied()); self.palette.with_popup_title_style(style.get("popup_title_style").copied()); } Ok(()) } fn handle_key_event(&mut self, key: KeyEvent) -> Result> { if self.is_active && key.kind == KeyEventKind::Press { match key.code { KeyCode::Enter => self.submit_message(), KeyCode::Char(to_insert) => self.enter_char(to_insert), KeyCode::Backspace => self.delete_char(), KeyCode::Left => self.move_cursor_left(), KeyCode::Right => self.move_cursor_right(), KeyCode::Esc => self.is_active = false, _ => {}, }; } Ok(None) } fn update(&mut self, action: Action) -> Result> { match action { Action::SetStashSecret(secret_seed) => self.secret_seed = secret_seed, _ => {} }; Ok(None) } fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { if self.is_active { let (border_style, border_type) = self.palette.create_popup_style(); let input = Paragraph::new(self.amount.value()) .block(Block::bordered() .border_style(border_style) .border_type(border_type) .title_style(self.palette.create_popup_title_style()) .title_alignment(Alignment::Right) .title(format!("Amount to rebond"))); let v = Layout::vertical([Constraint::Max(3)]).flex(Flex::Center); let h = Layout::horizontal([Constraint::Max(50)]).flex(Flex::Center); let [area] = v.areas(area); let [area] = h.areas(area); frame.render_widget(Clear, area); frame.render_widget(input, area); frame.set_cursor_position(Position::new( area.x + self.amount.cursor() as u16 + 1, area.y + 1 )); } Ok(()) } }