src/app/pages/item_info.rs
541 lines · 21350 bytes
1use std::{collections::LinkedList, error::Error};
2
3use crate::{
4 app::{
5 app::{App, AppNoState},
6 config::{
7 Action, ConstraintTransitional, ItemTransitional, LayoutConfig, PageCommand,
8 RowItemTransitional, RowTransitional,
9 },
10 pages::{channel::*, global::*},
11 },
12 functions::download_all_thumbnails,
13 structs::{FullPlayList, FullVideo, Item, ListItem, Page, WatchHistory},
14 traits::{ItemTrait, PageTrait},
15 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
16};
17use crossterm::event::KeyEvent;
18use tui::{
19 backend::Backend,
20 layout::Rect,
21 style::{Color, Style},
22 Frame,
23};
24
25use super::global::GlobalItem;
26
27#[derive(Clone)]
28pub enum ItemInfoItem {
29 Unknown,
30 Video(VideoItemInfo),
31 PlayList(PlayListItemInfo),
32}
33
34#[derive(Clone)]
35pub struct VideoItemInfo {
36 video: FullVideo,
37 list: TextList,
38 mode: Mode,
39 commands: Vec<PageCommand>,
40}
41
42#[derive(Clone)]
43pub struct PlayListItemInfo {
44 playlist: FullPlayList,
45 video_view_list: TextList,
46 playlist_view_list: TextList,
47 display_view: PlayListDisplayView,
48 mode: Mode,
49 commands: Vec<PageCommand>,
50}
51
52#[derive(Clone, Copy)]
53pub enum PlayListDisplayView {
54 VideoList,
55 PlayListView,
56}
57
58impl PlayListDisplayView {
59 pub fn toggle(self) -> Self {
60 match self {
61 PlayListDisplayView::VideoList => PlayListDisplayView::PlayListView,
62 PlayListDisplayView::PlayListView => PlayListDisplayView::VideoList,
63 }
64 }
65}
66
67impl ItemTrait for ItemInfoItem {
68 fn reset(&mut self) {
69 // panic!("Got here!");
70 *self = Self::Unknown;
71 }
72 fn select(&mut self, app: App) -> (App, bool) {
73 let selected = true;
74
75 (app, selected)
76 }
77
78 fn selectable(&self) -> bool {
79 true
80 }
81
82 fn key_input(&mut self, key: KeyEvent, mut app: App) -> (bool, App) {
83 let action = match app.config.keybindings.0.get(&key) {
84 Some(action) => *action,
85 None => return (false, app),
86 };
87 match self {
88 ItemInfoItem::Video(videoinfo) => match action {
89 Action::Up => videoinfo.list.up(),
90 Action::Down => videoinfo.list.down(),
91 Action::FirstItem => videoinfo.list.first(),
92 Action::LastItem => videoinfo.list.last(),
93 Action::Select => {
94 let command = &videoinfo.commands[videoinfo.list.selected];
95
96 match command.command.as_str() {
97 "{toggle_mode}" => videoinfo.mode.toggle(),
98 "{goto_channel}" => {
99 let state = app.config.layouts.channel.clone().into();
100 let mut history = app.history.clone();
101 history.push(app.into());
102
103 return (
104 false,
105 App {
106 history,
107 page: Page::Channel(
108 ChannelPage::Home,
109 videoinfo.video.channel_id.clone(),
110 ),
111 selectable: App::selectable(&state),
112 state,
113 ..Default::default()
114 },
115 );
116 }
117 _ => {
118 match app.config.commands.0.get(&command.command) {
119 Some(command) => {
120 let mut env = app.config.main.env.clone();
121
122 env.insert(
123 String::from("embed_url"),
124 format!(
125 "{}/embed/{}",
126 match videoinfo.mode {
127 Mode::Invidious => app.client.server.as_str(),
128 Mode::Youtube => "https://youtube.com",
129 },
130 videoinfo.video.video_id
131 ),
132 );
133
134 match command.run_command(&env) {
135 Some(e) => {
136 app.message = Some(format!("Unknown variable `{}`", e))
137 }
138 None => app.message = Some(command.message.clone()),
139 }
140 }
141 None => {
142 app.message =
143 Some(format!("Unkown command `{}`", &command.command));
144 }
145 };
146 }
147 };
148 }
149 _ => {}
150 },
151
152 ItemInfoItem::PlayList(playlistinfo) => match playlistinfo.display_view {
153 PlayListDisplayView::PlayListView => match action {
154 Action::Up => playlistinfo.playlist_view_list.up(),
155 Action::Down => playlistinfo.playlist_view_list.down(),
156 Action::FirstItem => playlistinfo.playlist_view_list.first(),
157 Action::LastItem => playlistinfo.playlist_view_list.last(),
158 Action::Select => {
159 let command =
160 &playlistinfo.commands[playlistinfo.playlist_view_list.selected];
161
162 match command.command.as_str() {
163 "{toggle_mode}" => playlistinfo.mode.toggle(),
164 "{goto_channel}" => {
165 let state = app.config.layouts.channel.clone().into();
166 let mut history = app.history.clone();
167 history.push(app.into());
168
169 return (
170 false,
171 App {
172 history,
173 page: Page::Channel(
174 ChannelPage::Home,
175 playlistinfo.playlist.playlist_id.clone(),
176 ),
177 selectable: App::selectable(&state),
178 state,
179 ..Default::default()
180 },
181 );
182 }
183
184 "{switch_view}" => {
185 playlistinfo.display_view = playlistinfo.display_view.toggle()
186 }
187 _ => {
188 match app.config.commands.0.get(&command.command) {
189 Some(command) => {
190 let mut env = app.config.main.env.clone();
191
192 env.insert(
193 String::from("embed_url"),
194 format!(
195 "{}/playlist?list={}",
196 match playlistinfo.mode {
197 Mode::Invidious => app.client.server.as_str(),
198 Mode::Youtube => "https://youtube.com",
199 },
200 playlistinfo.playlist.playlist_id
201 ),
202 );
203
204 match command.run_command(&env) {
205 Some(e) => {
206 app.message =
207 Some(format!("Unknown variable `{}`", e))
208 }
209 None => app.message = Some(command.message.clone()),
210 }
211 }
212 None => {
213 app.message =
214 Some(format!("Unkown command `{}`", &command.command));
215 }
216 };
217 }
218 };
219 }
220
221 _ => {}
222 },
223 PlayListDisplayView::VideoList => match action {
224 Action::Up => playlistinfo.video_view_list.up(),
225 Action::Down => playlistinfo.video_view_list.down(),
226 Action::FirstItem => playlistinfo.video_view_list.first(),
227 Action::LastItem => playlistinfo.video_view_list.last(),
228 Action::Select => match playlistinfo.video_view_list.selected {
229 0 => {
230 playlistinfo.display_view = playlistinfo.display_view.toggle();
231 }
232
233 x => {
234 let state = app.config.layouts.channel.clone().into();
235 let mut history = app.history.clone();
236 history.push(app.into());
237
238 return (
239 false,
240 App {
241 history,
242 page: Page::ItemDisplay(DisplayItem::Video(
243 playlistinfo.playlist.videos[x - 1].video_id.clone(),
244 )),
245 selectable: App::selectable(&state),
246 state,
247 ..Default::default()
248 },
249 );
250 }
251 },
252
253 _ => {}
254 },
255 },
256 _ => {}
257 }
258
259 (true, app)
260 }
261
262 fn load_item(
263 &self,
264 app: &App,
265 watch_history: &mut WatchHistory,
266 ) -> Result<Item, Box<dyn Error>> {
267 let mut this = self.clone();
268
269 if let ItemInfoItem::Unknown = this {
270 match &app.page {
271 Page::ItemDisplay(display_item) => match display_item {
272 DisplayItem::Video(id) => {
273 let mut list = TextList::default();
274 let video: FullVideo = app.client.video(id, None)?.into();
275 if app.config.main.display_thumbnails {
276 let _ = download_all_thumbnails(LinkedList::from([(
277 video.video_thumbnail.clone(),
278 video.video_id.clone(),
279 )]));
280 }
281 let commands = app
282 .config
283 .page_commands
284 .0
285 .get("item_info:video")
286 .unwrap_or(&Vec::new())
287 .clone();
288 list.items(commands.iter().map(|item| item.label.to_string()).collect());
289
290 let iteminfo = VideoItemInfo {
291 video,
292 list,
293 mode: Mode::Youtube,
294 commands,
295 };
296
297 watch_history.push(
298 iteminfo.video.video_id.clone(),
299 ListItem::FullVideo(iteminfo.video.clone()),
300 &app.config,
301 );
302
303 this = ItemInfoItem::Video(iteminfo);
304 }
305
306 DisplayItem::PlayList(id) => {
307 let mut videos_text_list = TextList::default();
308 let mut playlist_text_list = TextList::default();
309 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
310 if app.config.main.display_thumbnails {
311 let _ = download_all_thumbnails(
312 playlist
313 .videos
314 .iter()
315 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
316 .collect(),
317 );
318 }
319
320 videos_text_list.items = vec![String::from("Swtich view")];
321 videos_text_list
322 .items
323 .extend(playlist.videos.iter().map(|v| v.title.clone()));
324
325 let commands = app
326 .config
327 .page_commands
328 .0
329 .get("item_info:playlist")
330 .unwrap_or(&Vec::new())
331 .clone();
332 playlist_text_list
333 .items(commands.iter().map(|item| item.label.to_string()).collect());
334
335 let iteminfo = PlayListItemInfo {
336 playlist: playlist,
337 video_view_list: videos_text_list,
338 playlist_view_list: playlist_text_list,
339 mode: Mode::Youtube,
340 display_view: PlayListDisplayView::PlayListView,
341 commands,
342 };
343
344 watch_history.push(
345 iteminfo.playlist.playlist_id.clone(),
346 ListItem::FullPlayList(iteminfo.playlist.clone()),
347 &app.config,
348 );
349
350 this = ItemInfoItem::PlayList(iteminfo);
351 }
352 },
353
354 _ => {}
355 }
356 }
357 Ok(Item::ItemInfo(this))
358 }
359
360 fn render_item<B: Backend>(
361 &mut self,
362 frame: &mut Frame<B>,
363 rect: Rect,
364 app: AppNoState,
365 selected: bool,
366 hover: bool,
367 popup_focus: bool,
368 popup_render: bool,
369 ) -> (bool, AppNoState) {
370 let out = (false, app);
371
372 if popup_render {
373 return out;
374 }
375
376 match self {
377 ItemInfoItem::Video(videoinfo) => {
378 // list.items[videoinfo.list.items.len() - 1] = format!("Mode: {}", videoinfo.mode);
379
380 let split = HorizontalSplit::default()
381 .percentages(vec![40, 60])
382 .border_style(Style::default().fg(if selected {
383 Color::LightBlue
384 } else if hover {
385 Color::LightRed
386 } else {
387 Color::Reset
388 }));
389
390 let chunks = split.inner(rect);
391
392 videoinfo.list.area(chunks[1]);
393
394 let mut list = videoinfo.list.clone();
395
396 list.items.iter_mut().for_each(|item| match item.as_str() {
397 "{mode}" => *item = format!("Mode: {}", videoinfo.mode),
398 _ => {}
399 });
400 if selected {
401 list.selected_style(Style::default().fg(Color::LightRed));
402 } else {
403 list.selected_style(Style::default().fg(Color::LightYellow));
404 }
405
406 frame.render_widget(split, rect);
407
408 if !popup_focus {
409 let item_display = ItemDisplay {
410 item: ListItem::FullVideo(videoinfo.video.clone()),
411 };
412
413 frame.render_widget(item_display, chunks[0]);
414 }
415
416 frame.render_widget(list, chunks[1]);
417 }
418
419 ItemInfoItem::PlayList(playlistinfo) => {
420 let split = HorizontalSplit::default()
421 .percentages(vec![40, 60])
422 .border_style(Style::default().fg(if selected {
423 Color::LightBlue
424 } else if hover {
425 Color::LightRed
426 } else {
427 Color::Reset
428 }));
429
430 let chunks = split.inner(rect);
431
432 frame.render_widget(split, rect);
433
434 if !popup_focus {
435 let item_display = ItemDisplay {
436 item: ListItem::FullPlayList(playlistinfo.playlist.clone()),
437 };
438
439 frame.render_widget(item_display, chunks[0]);
440 }
441
442 match playlistinfo.display_view {
443 PlayListDisplayView::VideoList => {
444 playlistinfo.video_view_list.area(chunks[1]);
445 let mut list = playlistinfo.video_view_list.clone();
446 list.area(chunks[1]);
447 if selected {
448 list.selected_style(Style::default().fg(Color::LightRed));
449 } else {
450 list.selected_style(Style::default().fg(Color::LightYellow));
451 }
452
453 frame.render_widget(list, chunks[1]);
454 }
455 PlayListDisplayView::PlayListView => {
456 playlistinfo.playlist_view_list.area(chunks[1]);
457 let mut list = playlistinfo.playlist_view_list.clone();
458
459 list.items.iter_mut().for_each(|item| match item.as_str() {
460 "{mode}" => *item = format!("Mode: {}", playlistinfo.mode),
461 _ => {}
462 });
463
464 list.area(chunks[1]);
465
466 if selected {
467 list.selected_style(Style::default().fg(Color::LightRed));
468 } else {
469 list.selected_style(Style::default().fg(Color::LightYellow));
470 }
471
472 frame.render_widget(list, chunks[1]);
473 }
474 }
475 }
476
477 ItemInfoItem::Unknown => {
478 let split = HorizontalSplit::default()
479 .percentages(vec![40, 60])
480 .border_style(Style::default().fg(if selected {
481 Color::LightBlue
482 } else if hover {
483 Color::LightRed
484 } else {
485 Color::Reset
486 }));
487
488 frame.render_widget(split, rect);
489 }
490 }
491 out
492 }
493}
494
495#[derive(Debug, Clone, PartialEq)]
496pub enum DisplayItem {
497 Video(String),
498 PlayList(String),
499}
500
501pub struct ItemInfo;
502impl PageTrait for ItemInfo {
503 fn default() -> LayoutConfig {
504 LayoutConfig {
505 layout: vec![
506 RowTransitional {
507 items: vec![
508 RowItemTransitional {
509 item: ItemTransitional::Global(GlobalItem::SearchBar),
510 constraint: ConstraintTransitional::Min(16),
511 },
512 RowItemTransitional {
513 item: ItemTransitional::Global(GlobalItem::SearchSettings),
514 constraint: ConstraintTransitional::Length(5),
515 },
516 ],
517 centered: false,
518 height: ConstraintTransitional::Length(3),
519 },
520 RowTransitional {
521 items: vec![RowItemTransitional {
522 item: ItemTransitional::ItemInfo,
523 constraint: ConstraintTransitional::Percentage(100),
524 }],
525 centered: false,
526 height: ConstraintTransitional::Min(6),
527 },
528 RowTransitional {
529 items: vec![RowItemTransitional {
530 item: ItemTransitional::Global(GlobalItem::MessageBar),
531 constraint: ConstraintTransitional::Percentage(100),
532 }],
533 centered: false,
534 height: ConstraintTransitional::Length(3),
535 },
536 ],
537 min: (21, 12),
538 message: String::from("Loading item info..."),
539 }
540 }
541}