src/app/pages/item_info.rs
545 lines · 21635 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, MessageText},
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 = MessageText::Text(format!("Unknown variable `{}`", e))
137 }
138 None => app.message = MessageText::Text(command.message.clone()),
139 }
140 }
141 None => {
142 app.message =
143 MessageText::Text(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 MessageText::Text(format!("Unknown variable `{}`", e))
208 }
209 None => app.message = MessageText::Text(command.message.clone()),
210 }
211 }
212 None => {
213 app.message =
214 MessageText::Text(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.item_info.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 // panic!("Got here");
274 let mut list = TextList::default();
275 let video: FullVideo = app.client.video(id, None)?.into();
276 if app.config.main.display_thumbnails {
277 let _ = download_all_thumbnails(LinkedList::from([(
278 video.video_thumbnail.clone(),
279 video.video_id.clone(),
280 )]));
281 }
282 let commands = app
283 .config
284 .page_commands
285 .0
286 .get("item_info:video")
287 .unwrap_or(&Vec::new())
288 .clone();
289 list.items(commands.iter().map(|item| item.label.to_string()).collect());
290
291 let iteminfo = VideoItemInfo {
292 video,
293 list,
294 mode: Mode::Youtube,
295 commands,
296 };
297
298 watch_history.push(
299 iteminfo.video.video_id.clone(),
300 ListItem::FullVideo(iteminfo.video.clone()),
301 &app.config,
302 );
303
304 this = ItemInfoItem::Video(iteminfo);
305 }
306
307 DisplayItem::PlayList(id) => {
308 let mut videos_text_list = TextList::default();
309 let mut playlist_text_list = TextList::default();
310 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
311 if app.config.main.display_thumbnails {
312 let _ = download_all_thumbnails(
313 playlist
314 .videos
315 .iter()
316 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
317 .collect(),
318 );
319 }
320
321 videos_text_list.items = vec![String::from("Swtich view")];
322 videos_text_list
323 .items
324 .extend(playlist.videos.iter().map(|v| v.title.clone()));
325
326 let commands = app
327 .config
328 .page_commands
329 .0
330 .get("item_info:playlist")
331 .unwrap_or(&Vec::new())
332 .clone();
333 playlist_text_list
334 .items(commands.iter().map(|item| item.label.to_string()).collect());
335
336 let iteminfo = PlayListItemInfo {
337 playlist: playlist,
338 video_view_list: videos_text_list,
339 playlist_view_list: playlist_text_list,
340 mode: Mode::Youtube,
341 display_view: PlayListDisplayView::PlayListView,
342 commands,
343 };
344
345 watch_history.push(
346 iteminfo.playlist.playlist_id.clone(),
347 ListItem::FullPlayList(iteminfo.playlist.clone()),
348 &app.config,
349 );
350
351 this = ItemInfoItem::PlayList(iteminfo);
352 }
353 },
354
355 _ => {}
356 }
357 }
358 Ok(Item::ItemInfo(this))
359 }
360
361 fn render_item<B: Backend>(
362 &mut self,
363 frame: &mut Frame<B>,
364 rect: Rect,
365 app: AppNoState,
366 selected: bool,
367 hover: bool,
368 popup_focus: bool,
369 popup_render: bool,
370 ) -> (bool, AppNoState) {
371 let out = (false, app);
372
373 if popup_render {
374 return out;
375 }
376
377 match self {
378 ItemInfoItem::Video(videoinfo) => {
379 // list.items[videoinfo.list.items.len() - 1] = format!("Mode: {}", videoinfo.mode);
380
381 let split = HorizontalSplit::default()
382 .percentages(vec![40, 60])
383 .border_style(Style::default().fg(if selected {
384 Color::LightBlue
385 } else if hover {
386 Color::LightRed
387 } else {
388 Color::Reset
389 }));
390
391 let chunks = split.inner(rect);
392
393 videoinfo.list.area(chunks[1]);
394
395 let mut list = videoinfo.list.clone();
396
397 list.items.iter_mut().for_each(|item| match item.as_str() {
398 "{mode}" => *item = format!("Mode: {}", videoinfo.mode),
399 _ => {}
400 });
401 if selected {
402 list.selected_style(Style::default().fg(Color::LightRed));
403 } else {
404 list.selected_style(Style::default().fg(Color::LightYellow));
405 }
406
407 frame.render_widget(split, rect);
408
409 if !popup_render {
410 let item_display = ItemDisplay {
411 item: ListItem::FullVideo(videoinfo.video.clone()),
412 render_image: !popup_focus,
413 };
414
415 frame.render_widget(item_display, chunks[0]);
416 }
417
418 frame.render_widget(list, chunks[1]);
419 }
420
421 ItemInfoItem::PlayList(playlistinfo) => {
422 let split = HorizontalSplit::default()
423 .percentages(vec![40, 60])
424 .border_style(Style::default().fg(if selected {
425 Color::LightBlue
426 } else if hover {
427 Color::LightRed
428 } else {
429 Color::Reset
430 }));
431
432 let chunks = split.inner(rect);
433
434 frame.render_widget(split, rect);
435
436 if !popup_render {
437 let item_display = ItemDisplay {
438 item: ListItem::FullPlayList(playlistinfo.playlist.clone()),
439 render_image: !popup_focus,
440 };
441
442 frame.render_widget(item_display, chunks[0]);
443 }
444
445 match playlistinfo.display_view {
446 PlayListDisplayView::VideoList => {
447 playlistinfo.video_view_list.area(chunks[1]);
448 let mut list = playlistinfo.video_view_list.clone();
449 list.area(chunks[1]);
450 if selected {
451 list.selected_style(Style::default().fg(Color::LightRed));
452 } else {
453 list.selected_style(Style::default().fg(Color::LightYellow));
454 }
455
456 frame.render_widget(list, chunks[1]);
457 }
458 PlayListDisplayView::PlayListView => {
459 playlistinfo.playlist_view_list.area(chunks[1]);
460 let mut list = playlistinfo.playlist_view_list.clone();
461
462 list.items.iter_mut().for_each(|item| match item.as_str() {
463 "{mode}" => *item = format!("Mode: {}", playlistinfo.mode),
464 _ => {}
465 });
466
467 list.area(chunks[1]);
468
469 if selected {
470 list.selected_style(Style::default().fg(Color::LightRed));
471 } else {
472 list.selected_style(Style::default().fg(Color::LightYellow));
473 }
474
475 frame.render_widget(list, chunks[1]);
476 }
477 }
478 }
479
480 ItemInfoItem::Unknown => {
481 let split = HorizontalSplit::default()
482 .percentages(vec![40, 60])
483 .border_style(Style::default().fg(if selected {
484 Color::LightBlue
485 } else if hover {
486 Color::LightRed
487 } else {
488 Color::Reset
489 }));
490
491 frame.render_widget(split, rect);
492 }
493 }
494 out
495 }
496}
497
498#[derive(Debug, Clone, PartialEq)]
499pub enum DisplayItem {
500 Video(String),
501 PlayList(String),
502}
503
504pub struct ItemInfo;
505impl PageTrait for ItemInfo {
506 fn default() -> LayoutConfig {
507 LayoutConfig {
508 layout: vec![
509 RowTransitional {
510 items: vec![
511 RowItemTransitional {
512 item: ItemTransitional::Global(GlobalItem::SearchBar),
513 constraint: ConstraintTransitional::Min(16),
514 },
515 RowItemTransitional {
516 item: ItemTransitional::Global(GlobalItem::SearchSettings),
517 constraint: ConstraintTransitional::Length(5),
518 },
519 ],
520 centered: false,
521 height: ConstraintTransitional::Length(3),
522 },
523 RowTransitional {
524 items: vec![RowItemTransitional {
525 item: ItemTransitional::ItemInfo,
526 constraint: ConstraintTransitional::Percentage(100),
527 }],
528 centered: false,
529 height: ConstraintTransitional::Min(6),
530 },
531 RowTransitional {
532 items: vec![RowItemTransitional {
533 item: ItemTransitional::Global(GlobalItem::MessageBar),
534 constraint: ConstraintTransitional::Percentage(100),
535 }],
536 centered: false,
537 height: ConstraintTransitional::Length(3),
538 },
539 ],
540 min: (21, 12),
541 message: String::from("Loading item info..."),
542 def_selected: Some((0,1)),
543 }
544 }
545}