src/items/pagebutton.rs

143 lines · 4756 bytes

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