src/items/pagebutton.rs

158 lines · 5635 bytes

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