leetcode_tui_rs/widgets/
root.rs1use leetcode_tui_config::CONFIG;
2use leetcode_tui_shared::layout::GetWindowStats;
3use ratatui::prelude::*;
4use ratatui::widgets::*;
5
6use crate::ctx::Ctx;
7use crate::widgets::help::Help;
8use crate::widgets::popup::{Popup, SelectPopup};
9use crate::widgets::progress_bar::ProgressBar;
10use crate::widgets::question::Questions;
11use crate::widgets::topic::Topic;
12
13pub struct Root<'a> {
14 cx: &'a mut Ctx,
15}
16
17impl<'a> Root<'a> {
18 pub fn new(cx: &'a mut Ctx) -> Self {
19 Self { cx }
20 }
21}
22
23impl<'a> Widget for Root<'a> {
24 fn render(self, _area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
25 if self.cx.help.is_visible() {
26 Help::new(self.cx).render(_area, buf);
27 return;
28 } else {
29 let line = Line::from(vec![Span::styled(
30 " [?] Help ",
31 Style::default()
32 .bg(CONFIG.as_ref().theme.defaults.info.into())
33 .fg(CONFIG.as_ref().theme.defaults.terminal_black.into()),
34 )]);
35 Paragraph::new(line)
36 .alignment(Alignment::Right)
37 .render(self.get_window().root.status_bar.message_area, buf);
38 }
39
40 Topic::new(self.cx).render(_area, buf);
41 Questions::new(self.cx).render(_area, buf);
42
43 if self.cx.popup.visible {
44 Popup::new(self.cx).render(_area, buf);
45 }
46
47 if self.cx.select_popup.visible {
48 SelectPopup::new(self.cx).render(_area, buf);
49 }
50
51 if self.cx.input.visible {
52 let mut search_text: String = "/".into();
53 if let Some(input_text) = self.cx.input.text() {
54 search_text.push_str(input_text);
55 }
56 let line = Line::from(search_text.as_str());
57 Paragraph::new(line).render(self.get_window().root.status_bar.search_area, buf);
58 }
59
60 if self.cx.progress.is_visible() {
61 ProgressBar::new(self.cx).render(self.get_window().root.top_bar, buf);
62 }
63 }
64}