src/config/pages.rs
313 lines · 10179 bytes
1use crate::{
2 global::traits::ConfigTrait,
3 items::{
4 ChannelDisplay, ItemList, MessageBar, PageButton, SearchBar, SearchFilter, SingleItem,
5 },
6};
7use serde::{Deserialize, Serialize};
8use std::slice;
9use tui::layout::Constraint;
10use tui_additions::framework::{Framework, FrameworkItem, Row, RowItem, State};
11use typemap::Key;
12
13/// Minimum screen dimention for the tui to display without panicking, stored in `data.state`
14// is automatically generated from PageConfig
15#[derive(Clone, Copy, Default)]
16pub struct MinDimentions {
17 pub width: u16,
18 pub height: u16,
19}
20
21impl MinDimentions {
22 pub const fn new(width: u16, height: u16) -> Self {
23 Self { width, height }
24 }
25}
26
27impl Key for MinDimentions {
28 type Value = Self;
29}
30
31/// Layout for all pages
32#[derive(Clone, Serialize, Deserialize)]
33pub struct PagesConfig {
34 #[serde(default = "main_menu_default")]
35 pub main_menu: PageConfig,
36 #[serde(default = "search_default")]
37 pub search: PageConfig,
38 #[serde(default = "singleitem_default")]
39 pub singleitem: PageConfig,
40 #[serde(default = "channeldisplay_default")]
41 pub channeldisplay: PageConfig,
42}
43
44impl Key for PagesConfig {
45 type Value = Self;
46}
47
48impl ConfigTrait for PagesConfig {
49 const LABEL: &'static str = "pages";
50}
51
52impl Default for PagesConfig {
53 fn default() -> Self {
54 Self {
55 main_menu: main_menu_default(),
56 search: search_default(),
57 singleitem: singleitem_default(),
58 channeldisplay: channeldisplay_default(),
59 }
60 }
61}
62
63// This struct impls Into<State>, the corresponding page is converted into State on page load
64// Each page has its own minimum width and height, which is automatically determined by the items in the page
65// If it doesn't meet the minimum dimentions, a "protective screen" will be shown to prevent panicking
66/// Layout for one single page
67#[derive(Clone, Serialize, Deserialize)]
68pub struct PageConfig {
69 pub layout: Vec<PageRow>,
70 pub message: String,
71}
72
73impl PageConfig {
74 /// Calculates minimum width for all items to display
75 pub fn min_width(&self) -> u16 {
76 self.layout
77 .iter()
78 .map(|row| {
79 row.iter()
80 .map(|item| constraint_to_u16(&item.width()))
81 .sum::<u16>()
82 })
83 .max()
84 .unwrap()
85 }
86
87 /// Calculates minimum height for all items to display
88 pub fn min_height(&self) -> u16 {
89 self.layout
90 .iter()
91 .map(|row| {
92 row.iter()
93 .map(|item| constraint_to_u16(&item.height()))
94 .max()
95 .unwrap()
96 })
97 .sum::<u16>()
98 }
99
100 /// Converts itself into `State` to be used in `Framework`
101 pub fn to_state(&self, framework: &mut Framework) -> State {
102 State(
103 self.layout
104 .iter()
105 .map(|row| Row {
106 centered: row.is_centered(),
107 height: max_constraint(
108 &row.iter().map(|item| item.height()).collect::<Vec<_>>(),
109 ),
110 items: row
111 .iter()
112 .map(|item| RowItem {
113 item: item.to_framework_item(framework),
114 width: item.width(),
115 })
116 .collect::<Vec<_>>(),
117 })
118 .collect::<Vec<_>>(),
119 )
120 }
121}
122
123/// CenteredRow will have its items centered, while NonCenteredRow will align to the left
124#[derive(Clone, Serialize, Deserialize)]
125pub enum PageRow {
126 CenteredRow(Vec<PageItems>),
127 NonCenteredRow(Vec<PageItems>),
128}
129
130impl PageRow {
131 pub fn iter(&self) -> slice::Iter<PageItems> {
132 match self {
133 Self::CenteredRow(iter) | Self::NonCenteredRow(iter) => iter.iter(),
134 }
135 }
136
137 pub fn is_centered(&self) -> bool {
138 match self {
139 Self::CenteredRow(_) => true,
140 Self::NonCenteredRow(_) => false,
141 }
142 }
143
144 pub fn from_vec(items: Vec<PageItems>, centered: bool) -> Self {
145 if centered {
146 Self::CenteredRow(items)
147 } else {
148 Self::NonCenteredRow(items)
149 }
150 }
151}
152
153// PageItems will be converted into Box<dyn FrameworkItem> on page load to be used an item in the framework
154// Seen https://docs.rs/tui-additions/latest/tui_additions/framework/trait.FrameworkItem.html
155// Each item has a minimum width and height for it to render without panicking
156/// All avaliable items for `PageConfig`
157#[derive(Clone, Copy, Serialize, Deserialize)]
158pub enum PageItems {
159 /// the search bar
160 SearchBar,
161 /// a list of item e.g. in the main menu or search items
162 ItemList,
163 /// the bottom panel which optionally displays a text
164 MessageBar,
165 /// the trending button which loads the trending page
166 Trending,
167 /// the popular button which loads the popular page
168 Popular,
169 /// the history button which loads the watch history page
170 History,
171 /// the search filters `...` button, displays a popup when selected
172 SearchFilters,
173 /// playlist and video info display
174 SingleItemInfo,
175 /// displays channel info
176 ChannelDisplay,
177 /// button which loads the main channel page
178 ChannelMain,
179 /// button which loads the channel videos page
180 ChannelVideos,
181 /// button which loads the channel playlists page
182 ChannelPlaylists,
183}
184
185impl PageItems {
186 /// Converts `Self` into a `FrameworkItem` to be used in `State`
187 pub fn to_framework_item(&self, _framework: &mut Framework) -> Box<dyn FrameworkItem> {
188 match *self {
189 Self::SearchBar => Box::new(SearchBar::default()),
190 Self::Popular => Box::new(PageButton::Popular),
191 Self::Trending => Box::new(PageButton::Trending),
192 Self::History => Box::new(PageButton::History),
193 Self::MessageBar => Box::new(MessageBar::default()),
194 Self::ItemList => Box::new(ItemList::default()),
195 Self::SearchFilters => Box::new(SearchFilter::default()),
196 Self::SingleItemInfo => Box::new(SingleItem::default()),
197 Self::ChannelDisplay => Box::new(ChannelDisplay::default()),
198 Self::ChannelMain => Box::new(PageButton::ChannelMain),
199 Self::ChannelVideos => Box::new(PageButton::ChannelVideos),
200 Self::ChannelPlaylists => Box::new(PageButton::ChannelPlaylists),
201 }
202 }
203
204 pub fn width(&self) -> Constraint {
205 match self {
206 Self::Popular
207 | Self::Trending
208 | Self::History
209 | Self::ChannelMain
210 | Self::ChannelVideos
211 | Self::ChannelPlaylists => Constraint::Length(15),
212 Self::SearchBar => Constraint::Min(16),
213 Self::MessageBar => Constraint::Min(3),
214 Self::ItemList | Self::SingleItemInfo | Self::ChannelDisplay => Constraint::Min(9),
215 Self::SearchFilters => Constraint::Length(5),
216 }
217 }
218
219 pub fn height(&self) -> Constraint {
220 match self {
221 Self::Popular
222 | Self::History
223 | Self::ChannelMain
224 | Self::ChannelVideos
225 | Self::ChannelPlaylists
226 | Self::Trending
227 | Self::MessageBar
228 | Self::SearchBar
229 | Self::SearchFilters => Constraint::Length(3),
230 Self::ItemList | Self::SingleItemInfo | Self::ChannelDisplay => Constraint::Min(6),
231 }
232 }
233}
234
235fn constraint_to_u16(constraint: &Constraint) -> u16 {
236 match constraint {
237 Constraint::Max(length) | Constraint::Min(length) | Constraint::Length(length) => *length,
238 _ => unreachable!("only `Max`, `Min`, `Length` can be used as item length"),
239 }
240}
241
242fn max_constraint(constraints: &[Constraint]) -> Constraint {
243 let mut max_out = Constraint::Length(0);
244 let mut max = 0_u16;
245
246 constraints.iter().for_each(|constraint| {
247 let self_len = constraint_to_u16(constraint);
248 if self_len > max {
249 max = self_len;
250 max_out = *constraint;
251 }
252 });
253
254 max_out
255}
256
257// default functions
258
259fn main_menu_default() -> PageConfig {
260 PageConfig {
261 layout: vec![
262 PageRow::from_vec(vec![PageItems::SearchBar, PageItems::SearchFilters], false),
263 PageRow::from_vec(
264 vec![PageItems::Popular, PageItems::Trending, PageItems::History],
265 true,
266 ),
267 PageRow::from_vec(vec![PageItems::ItemList], false),
268 PageRow::from_vec(vec![PageItems::MessageBar], false),
269 ],
270 message: String::from("Loading main menu..."),
271 }
272}
273
274fn search_default() -> PageConfig {
275 PageConfig {
276 layout: vec![
277 PageRow::from_vec(vec![PageItems::SearchBar, PageItems::SearchFilters], false),
278 PageRow::from_vec(vec![PageItems::ItemList], false),
279 PageRow::from_vec(vec![PageItems::MessageBar], false),
280 ],
281 message: String::from("Loading search results..."),
282 }
283}
284
285fn singleitem_default() -> PageConfig {
286 PageConfig {
287 layout: vec![
288 PageRow::from_vec(vec![PageItems::SearchBar, PageItems::SearchFilters], false),
289 PageRow::from_vec(vec![PageItems::SingleItemInfo], false),
290 PageRow::from_vec(vec![PageItems::MessageBar], false),
291 ],
292 message: String::from("Loading item details..."),
293 }
294}
295
296fn channeldisplay_default() -> PageConfig {
297 PageConfig {
298 layout: vec![
299 PageRow::from_vec(vec![PageItems::SearchBar, PageItems::SearchFilters], false),
300 PageRow::from_vec(
301 vec![
302 PageItems::ChannelMain,
303 PageItems::ChannelVideos,
304 PageItems::ChannelPlaylists,
305 ],
306 true,
307 ),
308 PageRow::from_vec(vec![PageItems::ChannelDisplay], false),
309 PageRow::from_vec(vec![PageItems::MessageBar], false),
310 ],
311 message: String::from("Loading channel details..."),
312 }
313}