src/items/pagebutton.rs
153 lines · 5524 bytes
1use crate::{config::AppearanceConfig, global::structs::*};
2use tui::{
3 layout::Alignment,
4 style::Style,
5 widgets::{Block, Borders, Paragraph},
6};
7use tui_additions::framework::FrameworkItem;
8
9/// button that on press will go to another page instead of selecting it
10#[derive(Clone, Copy, PartialEq, Eq)]
11pub enum PageButton {
12 Trending,
13 Popular,
14 ChannelMain,
15 ChannelVideos,
16 ChannelPlaylists,
17 History,
18 Subscriptions,
19 Library,
20}
21
22impl PageButton {
23 pub fn page(&self, current_page: &Page) -> Page {
24 match self {
25 Self::Trending => Page::MainMenu(MainMenuPage::Trending),
26 Self::Popular => Page::MainMenu(MainMenuPage::Popular),
27 Self::ChannelMain => Page::ChannelDisplay(ChannelDisplayPage {
28 id: current_page.channeldisplay().id.clone(),
29 r#type: ChannelDisplayPageType::Main,
30 }),
31 Self::ChannelVideos => Page::ChannelDisplay(ChannelDisplayPage {
32 id: current_page.channeldisplay().id.clone(),
33 r#type: ChannelDisplayPageType::Videos,
34 }),
35 Self::ChannelPlaylists => Page::ChannelDisplay(ChannelDisplayPage {
36 id: current_page.channeldisplay().id.clone(),
37 r#type: ChannelDisplayPageType::Playlists,
38 }),
39 Self::History => Page::MainMenu(MainMenuPage::History),
40 Self::Subscriptions => Page::MainMenu(MainMenuPage::Subscriptions),
41 Self::Library => Page::MainMenu(MainMenuPage::Library),
42 }
43 }
44
45 fn update_toggleable(&mut self, page: &Page) {
46 if *self == Self::Popular || *self == Self::Trending {
47 match page {
48 Page::MainMenu(MainMenuPage::Popular) => *self = Self::Popular,
49 Page::MainMenu(MainMenuPage::Trending) => *self = Self::Trending,
50 _ => {}
51 }
52 } else if *self == Self::Subscriptions || *self == Self::Library {
53 // match page {
54 // Page::MainMenu(MainMenuPage::Subscriptions) => *self = Self::Subscriptions,
55 // Page::MainMenu(MainMenuPage::Library) => *self = Self::Library,
56 // _ => {}
57 // }
58 }
59 }
60}
61
62impl ToString for PageButton {
63 fn to_string(&self) -> String {
64 match self {
65 Self::Popular => String::from("Popular"),
66 Self::Trending => String::from("Trending"),
67 Self::ChannelMain => String::from("Main"),
68 Self::ChannelVideos => String::from("Videos"),
69 Self::ChannelPlaylists => String::from("Playlists"),
70 Self::History => String::from("History"),
71 Self::Subscriptions => String::from("Subscriptions"),
72 Self::Library => String::from("Library"),
73 }
74 }
75}
76
77impl FrameworkItem for PageButton {
78 fn load_item(
79 &mut self,
80 framework: &mut tui_additions::framework::FrameworkClean,
81 _info: tui_additions::framework::ItemInfo,
82 ) -> Result<(), Box<dyn std::error::Error>> {
83 self.update_toggleable(framework.data.state.get::<Page>().unwrap());
84 Ok(())
85 }
86 // it is basically a paragraph (text) with borders
87 fn render(
88 &mut self,
89 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
90 framework: &mut tui_additions::framework::FrameworkClean,
91 area: tui::layout::Rect,
92 popup_render: bool,
93 info: tui_additions::framework::ItemInfo,
94 ) {
95 if popup_render {
96 return;
97 }
98
99 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
100 let same_page = &self.page(framework.data.state.get::<Page>().unwrap())
101 == framework.data.state.get::<Page>().unwrap();
102
103 let block = Block::default()
104 .border_type(appearance.borders)
105 .border_style(Style::default().fg(if info.hover {
106 appearance.colors.outline_hover
107 } else if same_page {
108 appearance.colors.outline_secondary
109 } else {
110 appearance.colors.outline
111 }))
112 .borders(Borders::ALL);
113 let paragraph = Paragraph::new(self.to_string())
114 .block(block)
115 .alignment(Alignment::Center);
116
117 frame.render_widget(paragraph, area);
118 }
119
120 // when selected creates a load page task, but returns false to show that it is not being selected
121 fn select(&mut self, framework: &mut tui_additions::framework::FrameworkClean) -> bool {
122 let current_page = framework.data.state.get::<Page>().unwrap();
123 let self_page = self.page(current_page);
124
125 if self_page == *current_page {
126 match self {
127 Self::Trending => *self = Self::Popular,
128 Self::Popular => *self = Self::Trending,
129 // Self::Subscriptions => *self = Self::Library,
130 // Self::Library => *self = Self::Subscriptions,
131 _ => {
132 *framework.data.global.get_mut::<Message>().unwrap() =
133 Message::Message(String::from("You are already on this page"));
134 return false;
135 }
136 }
137 }
138
139 framework
140 .data
141 .state
142 .get_mut::<Tasks>()
143 .unwrap()
144 .priority
145 .push(Task::LoadPage(self.page(&self_page)));
146
147 false
148 }
149
150 fn selectable(&self) -> bool {
151 true
152 }
153}