src/items/pagebutton.rs
85 lines · 2461 bytes
1use crate::{
2 config::AppearanceConfig,
3 global::{
4 page::{MainMenuPage, Page},
5 tasks::{Task, Tasks},
6 },
7};
8use tui::{
9 layout::Alignment,
10 style::Style,
11 widgets::{Block, Borders, Paragraph},
12};
13use tui_additions::framework::FrameworkItem;
14
15// button that on press will go to another page instead of selecting it
16#[derive(Clone, Copy)]
17pub enum PageButton {
18 Trending,
19 Popular,
20}
21
22impl PageButton {
23 pub fn page(&self) -> Page {
24 match self {
25 Self::Trending => Page::MainMenu(MainMenuPage::Trending),
26 Self::Popular => Page::MainMenu(MainMenuPage::Popular),
27 }
28 }
29}
30
31impl ToString for PageButton {
32 fn to_string(&self) -> String {
33 match self {
34 Self::Popular => String::from("Popular"),
35 Self::Trending => String::from("Trending"),
36 }
37 }
38}
39
40impl FrameworkItem for PageButton {
41 // it is basically a paragraph (text) with borders
42 fn render(
43 &mut self,
44 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
45 framework: &mut tui_additions::framework::FrameworkClean,
46 area: tui::layout::Rect,
47 popup_render: bool,
48 info: tui_additions::framework::ItemInfo,
49 ) {
50 if popup_render {
51 return;
52 }
53
54 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
55 let same_page = &self.page() == framework.data.state.get::<Page>().unwrap();
56
57 let block = Block::default()
58 .border_type(appearance.borders)
59 .border_style(Style::default().fg(if info.hover {
60 appearance.colors.outline_hover
61 } else if same_page {
62 appearance.colors.outline_secondary
63 } else {
64 appearance.colors.outline
65 }))
66 .borders(Borders::ALL);
67 let paragraph = Paragraph::new(self.to_string())
68 .block(block)
69 .alignment(Alignment::Center);
70
71 frame.render_widget(paragraph, area);
72 }
73
74 // when selected creates a load page task, but returns false to show that it is not being selected
75 fn select(&mut self, framework: &mut tui_additions::framework::FrameworkClean) -> bool {
76 let tasks = framework.data.state.get_mut::<Tasks>().unwrap();
77 tasks.priority.push(Task::LoadPage(self.page()));
78
79 false
80 }
81
82 fn selectable(&self) -> bool {
83 true
84 }
85}