1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crossterm::event::{KeyCode, KeyEvent};
use indexmap::IndexSet;
use ratatui::{
    prelude::*,
    widgets::{Block, Borders, Clear, Paragraph, Wrap},
};

use crate::app_ui::components::help_text::CommonHelpText;

use super::{CommonState, Component, FrameBackend};

#[derive(Debug, Clone)]
pub(crate) struct ParagraphPopup {
    common_state: CommonState,
    content: String,
    pub scroll_x: u16,
    pub scroll_y: u16,
}

impl ParagraphPopup {
    pub fn new(title: String, content: String) -> Self {
        Self {
            common_state: CommonState {
                help_text: IndexSet::from_iter([
                    CommonHelpText::Close.into(),
                    CommonHelpText::ScrollUp.into(),
                    CommonHelpText::ScrollDown.into(),
                ]),
                title,
                show: true,
            },
            content,
            scroll_x: 0,
            scroll_y: 0,
        }
    }
}

impl Component for ParagraphPopup {
    fn event_handler(&mut self, event: KeyEvent) -> Option<KeyEvent> {
        match event.code {
            KeyCode::Up => self.scroll_y = self.scroll_y.saturating_sub(1),
            KeyCode::Down => self.scroll_y += 1,
            KeyCode::Esc => self.hide(),
            KeyCode::Enter => {
                self.hide();
                return Some(event);
            }
            _ => return Some(event),
        }
        None
    }

    fn render(&mut self, f: &mut FrameBackend, render_area: Rect) {
        let block = Block::default()
            .borders(Borders::ALL)
            .style(Style::default().fg(Color::Gray))
            .title(Span::styled(
                self.common_state.title.clone(),
                Style::default().add_modifier(Modifier::BOLD),
            ));

        let content = Paragraph::new(self.content.as_str())
            .wrap(Wrap { trim: true })
            .scroll((self.scroll_y, self.scroll_x))
            .block(block);

        f.render_widget(Clear, render_area);
        f.render_widget(content, render_area); // frame.render_widget(block, area);
    }

    fn get_common_state_mut(&mut self) -> &mut CommonState {
        &mut self.common_state
    }

    fn get_common_state(&self) -> &CommonState {
        &self.common_state
    }
}