rolodex_tui/components/error_dialog/
mod.rs1use crossterm::event::{KeyCode, KeyEvent};
2use ratatui::{prelude::*, widgets::*};
3
4use crate::components::Component;
5
6pub enum ErrorMsg {
7 Dismiss,
8}
9pub enum ErrorOutput {
10 Dismissed,
11}
12#[derive(Debug, Default)]
13pub struct ErrorDialog {
14 message: String,
15}
16impl ErrorDialog {
17 pub fn new() -> Self {
18 Self::default()
19 }
20 pub fn set_error(&mut self, message: &str) {
21 self.message = message.to_string();
22 }
23 pub fn handle_key(&self, event: KeyEvent) -> Option<ErrorMsg> {
24 match event.code {
25 KeyCode::Esc => Some(ErrorMsg::Dismiss),
26 _ => None,
27 }
28 }
29 pub fn draw(&self, f: &mut Frame, area: Rect, _focused: bool) {
30 f.render_widget(Clear, area);
31
32 let text = format!("\n{}\n\n[Press Esc to dismiss]", self.message);
34 let paragraph = Paragraph::new(text)
35 .style(Style::default().fg(Color::Red))
36 .alignment(Alignment::Center)
37 .block(
38 Block::default()
39 .borders(Borders::ALL)
40 .title(" Error ")
41 .style(Style::default().fg(Color::White).bg(Color::Black)),
42 );
43 f.render_widget(paragraph, area);
44 }
45 pub fn update<ParentMsg>(
46 &mut self,
47 msg: ErrorMsg,
48 map: impl Fn(ErrorOutput) -> ParentMsg,
49 ) -> Option<ParentMsg> {
50 match msg {
51 ErrorMsg::Dismiss => Some(map(ErrorOutput::Dismissed)),
52 }
53 }
54}
55
56impl Component for ErrorDialog {
57 type Msg = ErrorMsg;
58 type Output = ErrorOutput;
59
60 fn update<ParentMsg>(
61 &mut self,
62 msg: Self::Msg,
63 map: impl Fn(Self::Output) -> ParentMsg,
64 ) -> Option<ParentMsg> {
65 self.update(msg, map)
66 }
67
68 fn handle_key(&self, key: KeyEvent) -> Option<Self::Msg> {
69 self.handle_key(key)
70 }
71
72 fn draw(&self, f: &mut Frame, area: Rect, focused: bool) {
73 self.draw(f, area, focused)
74 }
75}