src/items/pagebutton.rs

141 lines · 4848 bytes

  1use crate::{config::AppearanceConfig, global::structs::*};
  2use ratatui::{
  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    Feed,
 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::Feed => Page::Feed,
 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    //     }
 53    // }
 54}
 55
 56impl ToString for PageButton {
 57    fn to_string(&self) -> String {
 58        match self {
 59            Self::Popular => String::from("Popular"),
 60            Self::Trending => String::from("Trending"),
 61            Self::ChannelMain => String::from("Main"),
 62            Self::ChannelVideos => String::from("Videos"),
 63            Self::ChannelPlaylists => String::from("Playlists"),
 64            Self::History => String::from("History"),
 65            Self::Feed => String::from("Feed"),
 66            Self::Library => String::from("Library"),
 67        }
 68    }
 69}
 70
 71impl FrameworkItem for PageButton {
 72    // fn load_item(
 73    //     &mut self,
 74    //     framework: &mut tui_additions::framework::FrameworkClean,
 75    //     _info: tui_additions::framework::ItemInfo,
 76    // ) -> Result<(), Box<dyn std::error::Error>> {
 77    //     self.update_toggleable(framework.data.state.get::<Page>().unwrap());
 78    //     Ok(())
 79    // }
 80
 81    // it is basically a paragraph (text) with borders
 82    fn render(
 83        &mut self,
 84        frame: &mut ratatui::Frame<ratatui::backend::CrosstermBackend<std::io::Stdout>>,
 85        framework: &mut tui_additions::framework::FrameworkClean,
 86        area: ratatui::layout::Rect,
 87        popup_render: bool,
 88        info: tui_additions::framework::ItemInfo,
 89    ) {
 90        if popup_render {
 91            return;
 92        }
 93
 94        let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
 95        let same_page = &self.page(framework.data.state.get::<Page>().unwrap())
 96            == framework.data.state.get::<Page>().unwrap();
 97
 98        let block = Block::default()
 99            .border_type(appearance.borders)
100            .border_style(Style::default().fg(if info.hover {
101                appearance.colors.outline_hover
102            } else if same_page {
103                appearance.colors.outline_secondary
104            } else {
105                appearance.colors.outline
106            }))
107            .borders(Borders::ALL);
108        let paragraph = Paragraph::new(self.to_string())
109            .block(block)
110            .alignment(Alignment::Center);
111
112        frame.render_widget(paragraph, area);
113    }
114
115    // when selected creates a load page task, but returns false to show that it is not being selected
116    fn select(&mut self, framework: &mut tui_additions::framework::FrameworkClean) -> bool {
117        let current_page = framework.data.state.get::<Page>().unwrap();
118        let self_page = self.page(current_page);
119
120        // if self_page == *current_page {
121        //     match self {
122        //         Self::Trending => *self = Self::Popular,
123        //         Self::Popular => *self = Self::Trending,
124        //     }
125        // }
126
127        framework
128            .data
129            .state
130            .get_mut::<Tasks>()
131            .unwrap()
132            .priority
133            .push(Task::LoadPage(self.page(&self_page)));
134
135        false
136    }
137
138    fn selectable(&self) -> bool {
139        true
140    }
141}