leetcode_tui_rs/widgets/
topic.rs1use leetcode_tui_config::CONFIG;
2use leetcode_tui_shared::layout::GetWindowStats;
3use ratatui::prelude::*;
4use ratatui::widgets::{Block, Borders, List, ListItem, Widget};
5
6use crate::ctx::Ctx;
7
8pub struct Topic<'a> {
9 cx: &'a Ctx,
10}
11
12impl<'a> Topic<'a> {
13 pub(super) fn new(cx: &'a Ctx) -> Self {
14 Self { cx }
15 }
16
17 fn get_styled_block(&self) -> Block {
18 let style: Style = CONFIG.as_ref().theme.border.normal.into();
19 Block::default()
20 .borders(Borders::ALL)
21 .border_style(style)
22 .cyan()
23 .title("Topics")
24 .title_alignment(Alignment::Center)
25 }
26}
27
28impl<'a> Widget for Topic<'a> {
29 fn render(self, _area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
30 if let Some(hovered) = self.cx.content.get_topic().hovered() {
31 let config = &CONFIG.as_ref().theme.topic;
32 let c_hovered: Style = config.hovered.into();
33 let normal = config.normal.into();
34
35 let lines = self
36 .cx
37 .content
38 .get_topic()
39 .window()
40 .iter()
41 .map(|t| {
42 ListItem::new(t.slug.as_str()).style(if t.slug == hovered.slug {
43 c_hovered
44 } else {
45 normal
46 })
47 })
48 .collect::<Vec<_>>();
49 self.get_styled_block()
50 .render(self.get_window().root.center_layout.topic.outer, buf);
51 let list = List::new(lines);
52 ratatui::widgets::Widget::render(
53 list,
54 self.get_window().root.center_layout.topic.inner,
55 buf,
56 );
57 }
58 }
59}