leetcode_tui_rs/
topic.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
use leetcode_tui_config::CONFIG;
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, List, ListItem, Widget};
use leetcode_tui_shared::layout::GetWindowStats;

use crate::ctx::Ctx;

pub struct Topic<'a> {
    cx: &'a Ctx,
}

impl<'a> Topic<'a> {
    pub(super) fn new(cx: &'a Ctx) -> Self {
        Self { cx }
    }

    fn get_styled_block(&self) -> Block {
        Block::default()
            .borders(Borders::ALL)
            .border_style(CONFIG.as_ref().theme.border.normal.into())
            .cyan()
            .title("Topics")
            .title_alignment(Alignment::Center)
    }
}

impl<'a> Widget for Topic<'a> {
    fn render(self, _area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
        if let Some(hovered) = self.cx.content.get_topic().hovered() {
            let config = &CONFIG.as_ref().theme.topic;
            let c_hovered = config.hovered.into();
            let normal = config.normal.into();

            let lines = self
                .cx
                .content
                .get_topic()
                .window()
                .iter()
                .map(|t| {
                    ListItem::new(t.slug.as_str()).style(if t.slug == hovered.slug {
                        c_hovered
                    } else {
                        normal
                    })
                })
                .collect::<Vec<_>>();
            self.get_styled_block()
                .render(self.get_window().root.center_layout.topic.outer, buf);
            let list = List::new(lines);
            list.render(self.get_window().root.center_layout.topic.inner, buf);
        }
    }
}