leetcode_tui_rs/
popup.rs

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::ctx::Ctx;
use leetcode_tui_config::CONFIG;
use ratatui::prelude::*;
use ratatui::widgets::{
    Block, Borders, Clear, List, ListItem, Paragraph, Scrollbar, StatefulWidget, Widget, Wrap,
};
use leetcode_tui_shared::layout::GetWindowStats;

pub struct SelectPopup<'a> {
    ctx: &'a mut Ctx,
}

impl<'a> SelectPopup<'a> {
    pub fn new(ctx: &'a mut Ctx) -> Self {
        Self { ctx }
    }
}

impl<'a> Widget for SelectPopup<'a> {
    fn render(self, _area: Rect, buf: &mut Buffer) {
        let c_def = &CONFIG.as_ref().theme.defaults;
        let mut block: Block<'_> = Block::default();
        if let Some(title) = self.ctx.select_popup.get_title() {
            block = Block::default().title(title);
        }
        let block = block
            .borders(Borders::ALL)
            .border_style(Style::default().fg(c_def.info.into()));
        Clear.render(self.get_window().root.popup.outer, buf);
        block.render(self.get_window().root.popup.outer, buf);
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(vec![Constraint::Percentage(100), Constraint::Min(1)])
            .split(self.get_window().root.popup.inner);
        let content_area = chunks[0];
        let lines = self.ctx.select_popup.get_lines().clone();

        let list = List::new(
            lines
                .iter()
                .map(|l| ListItem::new(vec![Line::from(l.as_ref())]).fg(c_def.fg_dark))
                .collect::<Vec<_>>(),
        )
        .highlight_style(
            Style::default()
                .bg(c_def.bg_highlight.into())
                .fg(c_def.fg.into())
                .add_modifier(Modifier::BOLD),
        );
        StatefulWidget::render(list, content_area, buf, &mut self.ctx.select_popup.state);
        // Scrollbar::default()
        //     .orientation(ratatui::widgets::ScrollbarOrientation::VerticalRight)
        //     .begin_symbol(Some("↑"))
        //     .end_symbol(Some("↓"))
        //     .render(scrollbar_area, buf, &mut self.ctx.popup.v_scroll_state)
    }
}

pub struct Popup<'a> {
    ctx: &'a mut Ctx,
}

impl<'a> Popup<'a> {
    pub fn prepare_lines(&self) -> Vec<Line> {
        self.ctx
            .popup
            .get_lines()
            .iter()
            .map(|l| Line::from(l.as_str()))
            .collect()
    }

    pub fn prepare_paragraph(&self) -> Paragraph<'_> {
        Paragraph::new(self.prepare_lines())
            .scroll((self.ctx.popup.v_scroll, 0))
            .wrap(Wrap { trim: true })
            .style(Style::default().fg(CONFIG.as_ref().theme.defaults.fg.into()))
    }
}

impl<'a> Popup<'a> {
    pub fn new(ctx: &'a mut Ctx) -> Self {
        Self { ctx }
    }
}

impl<'a> Widget for Popup<'a> {
    fn render(self, _area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
        let c_def = &CONFIG.as_ref().theme.defaults;
        let mut block: Block<'_> = Block::default();
        if let Some(title) = self.ctx.popup.get_title() {
            block = Block::default().title(title);
        }
        let block = block
            .borders(Borders::ALL)
            .border_style(Style::default().fg(c_def.info.into()));
        Clear.render(self.get_window().root.popup.outer, buf);
        block.render(self.get_window().root.popup.outer, buf);
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(vec![Constraint::Percentage(100), Constraint::Min(1)])
            .split(self.get_window().root.popup.inner);
        let content_area = chunks[0];
        let scrollbar_area = chunks[1];
        self.prepare_paragraph().render(content_area, buf);
        Scrollbar::default()
            .orientation(ratatui::widgets::ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some("↑"))
            .end_symbol(Some("↓"))
            .render(scrollbar_area, buf, &mut self.ctx.popup.v_scroll_state)
    }
}