src/structs/states.rs

2512b334b421431b5e8577ab9c3031350fcd71bd
youtube-tui/src/structs/states.rs history · blame · raw

108 lines · 3136 bytes

  1use std::error::Error;
  2
  3use crossterm::event::KeyCode;
  4use tui::{
  5    backend::Backend,
  6    layout::{Constraint, Rect},
  7    Frame,
  8};
  9
 10use crate::{
 11    app::{
 12        app::App,
 13        pages::{
 14            channel::ChannelItem, global::GlobalItem, item_info::ItemInfoItem,
 15            main_menu::MainMenuItem, search::SearchItem,
 16        },
 17    },
 18    traits::ItemTrait,
 19};
 20
 21use super::WatchHistory;
 22
 23#[derive(Debug, Clone)]
 24pub enum Item {
 25    Global(GlobalItem),
 26    MainMenu(MainMenuItem),
 27    ItemInfo(ItemInfoItem),
 28    Search(SearchItem),
 29    Channel(ChannelItem),
 30}
 31
 32impl Item {
 33    pub fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
 34        match self {
 35            Item::Global(item) => item.key_input(key, app),
 36            Item::MainMenu(item) => item.key_input(key, app),
 37            Item::ItemInfo(item) => item.key_input(key, app),
 38            Item::Search(item) => item.key_input(key, app),
 39            Item::Channel(item) => item.key_input(key, app),
 40        }
 41    }
 42
 43    pub fn render_item<B: Backend>(
 44        &self,
 45        frame: &mut Frame<B>,
 46        rect: Rect,
 47        app: App,
 48        selected: bool,
 49        hover: bool,
 50        popup_focus: bool,
 51        popup_render: bool,
 52    ) -> (bool, Option<Item>, App) {
 53        match self {
 54            Item::Global(item) => {
 55                item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
 56            }
 57            Item::MainMenu(item) => {
 58                item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
 59            }
 60            Item::ItemInfo(item) => {
 61                item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
 62            }
 63            Item::Search(item) => {
 64                item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
 65            }
 66            Item::Channel(item) => {
 67                item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
 68            }
 69        }
 70    }
 71
 72    pub fn select(&mut self, app: App) -> (App, bool) {
 73        match self {
 74            Item::Global(item) => item.select(app),
 75            Item::MainMenu(item) => item.select(app),
 76            Item::ItemInfo(item) => item.select(app),
 77            Item::Search(item) => item.select(app),
 78            Item::Channel(item) => item.select(app),
 79        }
 80    }
 81
 82    pub fn load_item(
 83        &self,
 84        app: &App,
 85        watchhistory: &mut WatchHistory,
 86    ) -> Result<Self, Box<dyn Error>> {
 87        match self {
 88            Item::Global(item) => item.load_item(app, watchhistory),
 89            Item::MainMenu(item) => item.load_item(app, watchhistory),
 90            Item::ItemInfo(item) => item.load_item(app, watchhistory),
 91            Item::Search(item) => item.load_item(app, watchhistory),
 92            Item::Channel(item) => item.load_item(app, watchhistory),
 93        }
 94    }
 95}
 96
 97#[derive(Debug, Clone)]
 98pub struct Row {
 99    pub items: Vec<RowItem>,
100    pub centered: bool,
101    pub height: Constraint,
102}
103
104#[derive(Debug, Clone)]
105pub struct RowItem {
106    pub item: Item,
107    pub constraint: Constraint,
108}