src/global/page.rs

db597a03bc7a1786d5261e8a55628da30193c775
youtube-tui/src/global/page.rs history · blame · raw

52 lines · 1354 bytes

 1use crate::config::{PageConfig, PagesConfig, Search};
 2use tui_additions::framework::Framework;
 3use typemap::Key;
 4
 5// Page can be converted into PageConfig, which can then be converted into State
 6// Covers all possible pages and its variants
 7#[derive(Clone, PartialEq, Eq)]
 8pub enum Page {
 9    MainMenu(MainMenuPage),
10    Search(Search),
11}
12
13impl Default for Page {
14    fn default() -> Self {
15        Self::MainMenu(MainMenuPage::default())
16    }
17}
18
19impl Key for Page {
20    type Value = Self;
21}
22
23#[derive(Clone, PartialEq, Eq)]
24pub enum MainMenuPage {
25    Trending,
26    Popular,
27}
28
29impl Default for MainMenuPage {
30    fn default() -> Self {
31        Self::Trending
32    }
33}
34
35impl Page {
36    pub fn to_page_config(&self, framework: &Framework) -> PageConfig {
37        let pages_config = framework.data.global.get::<PagesConfig>().unwrap();
38        match self {
39            Self::MainMenu(_) => pages_config.main_menu.clone(),
40            Self::Search(_) => todo!(),
41        }
42    }
43
44    // each page displays a text when loading, and that text is taken from config
45    pub fn load_msg(&self, framework: &Framework) -> String {
46        let pages_config = framework.data.global.get::<PagesConfig>().unwrap();
47        match self {
48            Self::MainMenu(_) => pages_config.main_menu.message.clone(),
49            Self::Search(_) => todo!(),
50        }
51    }
52}