src/app/pages/item_info.rs
160 lines · 4842 bytes
1use crate::{
2 app::app::{App, Item, Page, Row, RowItem},
3 traits::{KeyInput, LoadItem, SelectItem},
4 widgets::{horizontal_split::HorizontalSplit, text_list::TextList},
5};
6use crossterm::event::KeyCode;
7use invidious::structs::video::Video;
8use tui::{
9 backend::Backend,
10 layout::{Constraint, Rect},
11 style::{Color, Style},
12 Frame,
13};
14
15use super::global::GlobalItem;
16
17#[derive(Debug, Clone)]
18pub enum ItemInfoItem {
19 Unknown,
20 Video(Option<VideoItemInfo>),
21}
22
23#[derive(Debug, Clone)]
24pub struct VideoItemInfo {
25 video: Video,
26 list: TextList,
27}
28
29impl SelectItem for ItemInfoItem {
30 fn select(&mut self, app: App) -> (App, bool) {
31 let selected = true;
32
33 (app, selected)
34 }
35
36 fn selectable(&self) -> bool {
37 true
38 }
39}
40
41impl KeyInput for ItemInfoItem {
42 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
43 match self {
44 ItemInfoItem::Video(Some(video)) => match key {
45 KeyCode::Up => video.list.up(),
46 KeyCode::Down => video.list.down(),
47 KeyCode::PageUp => video.list.selected = 0,
48 KeyCode::PageDown => video.list.selected = video.list.items.len() - 1,
49 _ => {}
50 },
51 _ => {}
52 }
53
54 (true, app)
55 }
56}
57
58impl LoadItem for ItemInfoItem {
59 fn load_item(&self, app: &App) -> Result<Box<Self>, Box<dyn std::error::Error>> {
60 let mut this = self.clone();
61
62 if let ItemInfoItem::Unknown = self {
63 match &app.page {
64 Page::ItemDisplay(display_item) => match display_item {
65 DisplayItem::Video(id) => {
66 if let ItemInfoItem::Video(videoinfo) = &mut this {
67 let mut list = TextList::default();
68 let video = app.client.video(id, None)?;
69 list.items(vec![
70 String::from("Watch from YouTube (Recommeneded)"),
71 String::from("Watch from Invidious"),
72 String::from("Play audio only from YouTube (Recommended)"),
73 String::from("Play audio only from Invidious"),
74 String::from("Download from YouTube (Recommended)"),
75 String::from("Download from Invidious"),
76 String::from("Download audio only from YouTube (Recommended)"),
77 String::from("Download audio only from Invidious"),
78 String::from("Visit channel"),
79 String::from("View thumbnail"),
80 ]);
81 *videoinfo = Some(VideoItemInfo { video, list });
82 }
83 }
84 },
85
86 _ => {}
87 }
88 }
89
90 Ok(Box::new(this))
91 }
92}
93
94impl ItemInfoItem {
95 pub fn render_item<B: Backend>(
96 &mut self,
97 frame: &mut Frame<B>,
98 rect: Rect,
99 selected: bool,
100 hover: bool,
101 ) {
102 match self {
103 ItemInfoItem::Video(videoinfo) => {
104 let split = HorizontalSplit::default()
105 .percentages(vec![40, 60])
106 .border_style(Style::default().fg(if selected {
107 Color::LightBlue
108 } else if hover {
109 Color::LightRed
110 } else {
111 Color::Reset
112 }));
113
114 let chunks = split.inner(rect);
115
116 frame.render_widget(split, rect);
117 }
118
119 _ => {}
120 }
121 }
122}
123
124#[derive(Debug, Clone, PartialEq)]
125pub enum DisplayItem {
126 Video(String),
127}
128
129pub struct ItemInfo;
130
131impl ItemInfo {
132 pub fn default() -> Vec<Row> {
133 vec![
134 Row {
135 items: vec![RowItem {
136 item: Item::Global(GlobalItem::SearchBar(String::new())),
137 constraint: Constraint::Percentage(100),
138 }],
139 centered: false,
140 height: Constraint::Length(3),
141 },
142 Row {
143 items: vec![RowItem {
144 item: Item::ItemInfo(ItemInfoItem::Video(None)),
145 constraint: Constraint::Percentage(100),
146 }],
147 centered: false,
148 height: Constraint::Min(6),
149 },
150 Row {
151 items: vec![RowItem {
152 item: Item::Global(GlobalItem::MessageBar),
153 constraint: Constraint::Percentage(100),
154 }],
155 centered: false,
156 height: Constraint::Length(3),
157 },
158 ]
159 }
160}