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