use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use color_eyre::Result; use ratatui::{ layout::{Alignment, Constraint, Flex, Layout, Rect}, widgets::{Block, Clear, Paragraph}, Frame }; use std::sync::mpsc::Sender; use tokio::sync::mpsc::UnboundedSender; use super::{Component, PartialComponent, CurrentTab}; use crate::{ action::Action, config::Config, palette::StylePalette, }; #[derive(Debug)] pub struct ChillPopup { is_active: bool, action_tx: Option>, network_tx: Option>, secret_seed: [u8; 32], palette: StylePalette } impl Default for ChillPopup { fn default() -> Self { Self::new() } } impl ChillPopup { pub fn new() -> Self { Self { is_active: false, secret_seed: [0u8; 32], network_tx: None, action_tx: None, palette: StylePalette::default(), } } fn submit_message(&mut self) { if let Some(network_tx) = &self.network_tx { let _ = network_tx.send(Action::ChillFrom(self.secret_seed)); if let Some(action_tx) = &self.action_tx { let _ = action_tx.send(Action::ClosePopup); } } } } impl PartialComponent for ChillPopup { fn set_active(&mut self, current_tab: CurrentTab) { match current_tab { CurrentTab::ChillPopup => self.is_active = true, _ => self.is_active = false, } } } impl Component for ChillPopup { 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::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(" Do you want to chill stash account and stop validation?") .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!("Chill stash"))); let v = Layout::vertical([Constraint::Max(3)]).flex(Flex::Center); let h = Layout::horizontal([Constraint::Max(57)]).flex(Flex::Center); let [area] = v.areas(area); let [area] = h.areas(area); frame.render_widget(Clear, area); frame.render_widget(input, area); } Ok(()) } }