tracexec_tui/
error_popup.rs1use crossterm::event::KeyEvent;
2use ratatui::{
3 buffer::Buffer,
4 layout::Rect,
5 style::{
6 Style,
7 Stylize,
8 },
9 text::Line,
10 widgets::{
11 Paragraph,
12 StatefulWidget,
13 Widget,
14 Wrap,
15 },
16};
17use tui_popup::Popup;
18
19use super::{
20 sized_paragraph::SizedParagraph,
21 theme::THEME,
22};
23use crate::action::{
24 Action,
25 ActivePopup,
26};
27
28#[derive(Debug, Clone)]
29pub struct InfoPopupState {
30 pub title: String,
31 pub message: Vec<Line<'static>>,
32 pub style: Style,
33}
34
35impl InfoPopupState {
36 pub fn handle_key_event(&self, _key: KeyEvent) -> Option<Action> {
37 Some(Action::CancelCurrentPopup)
38 }
39
40 pub fn error(title: String, message: Vec<Line<'static>>) -> Self {
41 Self {
42 title,
43 message,
44 style: THEME.error_popup,
45 }
46 }
47
48 pub fn info(title: String, message: Vec<Line<'static>>) -> Self {
49 Self {
50 title,
51 message,
52 style: THEME.info_popup,
53 }
54 }
55}
56
57#[derive(Debug, Clone)]
58pub struct InfoPopup;
59
60impl StatefulWidget for InfoPopup {
61 type State = InfoPopupState;
62
63 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
64 let help = Line::raw("Press any key to close this popup");
65 let mut message = state.message.clone();
66 message.push("".into());
67 message.push(help.centered().bold());
68 let paragraph = Paragraph::new(message).wrap(Wrap { trim: false });
69 let popup = Popup::new(SizedParagraph::new(
70 paragraph,
71 (area.width as f32 * 0.7) as usize,
72 ))
73 .title(Line::raw(state.title.as_str()).centered())
74 .style(state.style);
75 Widget::render(popup, area, buf);
76 }
77}
78
79pub fn err_popup_goto_parent_miss(title: &'static str) -> ActivePopup {
80 ActivePopup::InfoPopup(InfoPopupState::info(
81 title.into(),
82 vec![Line::raw(
83 "The parent exec event is found, but has been cleared from memory.",
84 )],
85 ))
86}
87
88pub fn err_popup_goto_parent_not_found(title: &'static str) -> ActivePopup {
89 ActivePopup::InfoPopup(InfoPopupState::info(
90 title.into(),
91 vec![Line::raw("No parent exec event is found for this event.")],
92 ))
93}
94
95pub fn err_popup_goto_parent_not_exec(title: &'static str) -> ActivePopup {
96 ActivePopup::InfoPopup(InfoPopupState::error(
97 title.into(),
98 vec![Line::raw(
99 "This feature is currently limited to exec events.",
100 )],
101 ))
102}