src/items/singleitem.rs
965 lines · 36092 bytes
1use super::ItemInfo;
2use crate::{
3 config::{AppearanceConfig, CommandsConfig, KeyBindingsConfig, MainConfig, Provider},
4 global::{
5 functions::{apply_envs, download_all_images, set_envs},
6 structs::{
7 InvidiousClient, Item, KeyAction, Message, Page, SingleItemPage, StateEnvs, Status,
8 Task, Tasks, WatchHistory,
9 },
10 },
11};
12use tui::{
13 layout::{Constraint, Rect},
14 style::Style,
15};
16use tui_additions::{
17 framework::{FrameworkClean, FrameworkItem},
18 widgets::{Grid, TextList},
19};
20
21impl Default for SingleItem {
22 fn default() -> Self {
23 Self {
24 item: None,
25 iteminfo: ItemInfo::default(),
26 grid: Grid::new(
27 vec![Constraint::Percentage(30), Constraint::Percentage(70)],
28 vec![Constraint::Percentage(100)],
29 )
30 .unwrap(),
31 r#type: SingleItemType::None,
32 }
33 }
34}
35
36/// main item in the `SingleItem(_)` page
37#[derive(Clone)]
38pub struct SingleItem {
39 pub item: Option<Item>,
40 pub iteminfo: ItemInfo,
41 pub grid: Grid,
42 pub r#type: SingleItemType,
43}
44
45/// variations that the struct can hold
46#[derive(Clone)]
47pub enum SingleItemType {
48 None,
49 Video(SingleVideoItem),
50 Playlist(Box<SinglePlaylistItem>),
51}
52
53#[derive(Clone)]
54pub struct SingleVideoItem {
55 pub textlist: TextList,
56 pub commands: Vec<(String, String)>,
57}
58
59#[derive(Clone)]
60pub struct SinglePlaylistItem {
61 pub commands_view: TextList,
62 pub videos_view: TextList,
63 pub commands: Vec<(String, String)>,
64 pub is_commands_view: bool,
65 pub hovered_video: ItemInfo,
66}
67
68impl SingleVideoItem {
69 pub fn new(commands: &CommandsConfig) -> Self {
70 Self {
71 textlist: TextList::default()
72 .items(
73 &commands
74 .video
75 .iter()
76 .map(|command| &command.0)
77 .collect::<Vec<_>>(),
78 )
79 .unwrap(),
80 commands: commands
81 .video
82 .clone()
83 .into_iter()
84 .map(|(display, command)| (display, command))
85 .collect(),
86 }
87 }
88
89 /// find all occurances of ${provider}
90 pub fn update_provider(&mut self) -> Vec<usize> {
91 self.commands
92 .iter()
93 .enumerate()
94 .filter(|(_index, (display, _command))| display.contains("${provider}"))
95 .map(|(index, _)| index)
96 .collect()
97 }
98
99 pub fn update_appearance(
100 &mut self,
101 appearance: &AppearanceConfig,
102 iteminfo: &tui_additions::framework::ItemInfo,
103 ) {
104 self.textlist.set_border_type(appearance.borders);
105 self.textlist
106 .set_style(Style::default().fg(appearance.colors.text));
107
108 if iteminfo.selected {
109 self.textlist
110 .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
111 self.textlist
112 .set_selected_style(Style::default().fg(appearance.colors.text_special));
113 } else {
114 self.textlist
115 .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
116 self.textlist
117 .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
118 }
119 }
120
121 /// creates a hashmap from `self`, containing info of the current item
122 pub fn inflate_load(
123 &self,
124 mainconfig: &MainConfig,
125 status: &Status,
126 item: &Item,
127 ) -> Vec<(String, String)> {
128 let video_item = item.fullvideo().unwrap();
129
130 vec![
131 (
132 String::from("url"),
133 match status.provider {
134 Provider::Invidious => format!(
135 "{}/watch?v={}",
136 mainconfig.invidious_instance, video_item.id
137 ),
138 Provider::YouTube => format!("'https://youtu.be/{}'", video_item.id),
139 },
140 ),
141 (String::from("id"), video_item.id.clone()),
142 (
143 String::from("embed-url"),
144 match status.provider {
145 Provider::Invidious => {
146 format!("{}/embed/{}", mainconfig.invidious_instance, video_item.id)
147 }
148 Provider::YouTube => format!("'https://youtube.com/embed/{}'", video_item.id),
149 },
150 ),
151 (String::from("channel-id"), video_item.channel_id.clone()),
152 (
153 String::from("channel-url"),
154 match status.provider {
155 Provider::YouTube => {
156 format!("https://www.youtube.com/channel/{}", video_item.channel_id)
157 }
158 Provider::Invidious => format!(
159 "{}/channel/{}",
160 mainconfig.invidious_instance, video_item.channel_id
161 ),
162 },
163 ),
164 ]
165 }
166}
167
168impl SinglePlaylistItem {
169 pub fn new(commands: &CommandsConfig, playlist_items: &[Item]) -> Self {
170 let hovered_video = ItemInfo::new(if playlist_items.is_empty() {
171 None
172 } else {
173 Some(playlist_items[0].clone())
174 });
175 Self {
176 commands_view: TextList::default()
177 .items(
178 &commands
179 .playlist
180 .iter()
181 .map(|command| &command.0)
182 .collect::<Vec<_>>(),
183 )
184 .unwrap(),
185 videos_view: TextList::default()
186 .items(&{
187 let mut items = vec!["Switch view"];
188 items.extend(
189 playlist_items
190 .iter()
191 .map(|item| item.minivideo().unwrap().title.as_str()),
192 );
193 items
194 })
195 .unwrap(),
196 commands: commands
197 .playlist
198 .clone()
199 .into_iter()
200 .map(|(display, command)| (display, command))
201 .collect(),
202 hovered_video,
203 is_commands_view: true,
204 }
205 }
206
207 pub fn update_appearance(
208 &mut self,
209 appearance: &AppearanceConfig,
210 iteminfo: &tui_additions::framework::ItemInfo,
211 grid: &mut Grid,
212 ) {
213 if self.is_commands_view {
214 grid.widths = vec![Constraint::Percentage(30), Constraint::Percentage(70)];
215 self.commands_view.set_border_type(appearance.borders);
216 self.commands_view
217 .set_style(Style::default().fg(appearance.colors.text));
218
219 if iteminfo.selected {
220 self.commands_view
221 .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
222 self.commands_view
223 .set_selected_style(Style::default().fg(appearance.colors.text_special));
224 } else {
225 self.commands_view
226 .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
227 self.commands_view
228 .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
229 }
230 } else {
231 grid.widths = if self.videos_view.selected == 0 {
232 vec![Constraint::Percentage(30), Constraint::Percentage(70)]
233 } else {
234 vec![
235 Constraint::Percentage(30),
236 Constraint::Percentage(40),
237 Constraint::Percentage(30),
238 ]
239 };
240 self.videos_view.set_border_type(appearance.borders);
241 self.videos_view
242 .set_style(Style::default().fg(appearance.colors.text));
243
244 if iteminfo.selected {
245 self.videos_view
246 .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
247 self.videos_view
248 .set_selected_style(Style::default().fg(appearance.colors.text_special));
249 } else {
250 self.videos_view
251 .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
252 self.videos_view
253 .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
254 }
255 }
256 }
257
258 /// find all occurances of ${provider}
259 pub fn update_provider(&mut self) -> Vec<usize> {
260 self.commands
261 .iter()
262 .enumerate()
263 .filter(|(_index, (display, _command))| display.contains("${provider}"))
264 .map(|(index, _)| index)
265 .collect()
266 }
267
268 /// creates a hashmap from `self`, containing info of the current item
269 pub fn inflate_load(&self, item: &Item) -> Vec<(String, String)> {
270 let playlist_item = item.fullplaylist().unwrap();
271
272 vec![
273 (String::from("id"), playlist_item.id.clone()),
274 (String::from("channel-id"), playlist_item.channel_id.clone()),
275 (
276 String::from("all-ids"),
277 playlist_item
278 .videos
279 .iter()
280 .map(|video| video.minivideo().unwrap().id.as_str())
281 .collect::<Vec<&str>>()
282 .join(" "),
283 ),
284 ]
285 }
286}
287
288impl SingleItemType {
289 /// self is none
290 pub fn is_none(&self) -> bool {
291 matches!(self, Self::None)
292 }
293
294 /// update colours every render
295 pub fn update_appearance(
296 &mut self,
297 appearance: &AppearanceConfig,
298 iteminfo: &tui_additions::framework::ItemInfo,
299 grid: &mut Grid,
300 ) {
301 match self {
302 Self::None => {}
303 Self::Playlist(playlistitem) => {
304 playlistitem.update_appearance(appearance, iteminfo, grid)
305 }
306 Self::Video(videoitem) => videoitem.update_appearance(appearance, iteminfo),
307 }
308 }
309
310 pub fn inflate_load(
311 &self,
312 mainconfig: &MainConfig,
313 status: &Status,
314 item: &Option<Item>,
315 ) -> Vec<(String, String)> {
316 let item = if let Some(item) = item.as_ref() {
317 item
318 } else {
319 return Vec::new();
320 };
321
322 match self {
323 Self::Video(singlevideoitem) => singlevideoitem.inflate_load(mainconfig, status, item),
324 Self::Playlist(singleplaylistitem) => singleplaylistitem.inflate_load(item),
325 Self::None => Vec::new(),
326 }
327 }
328}
329
330impl SingleItem {
331 fn infalte_item_update(
332 &self,
333 mainconfig: &MainConfig,
334 status: &Status,
335 ) -> Vec<(String, String)> {
336 if let SingleItemType::Playlist(singleplaylistitem) = &self.r#type {
337 if singleplaylistitem.videos_view.selected == 0 {
338 return vec![(String::from("hover-url"), String::from("not avaliable"))];
339 }
340 vec![(
341 String::from("hover-url"),
342 match &self.item {
343 Some(item) => format!(
344 "{}/watch?v={}",
345 match status.provider {
346 Provider::YouTube => "https://youtube.com",
347 Provider::Invidious => &mainconfig.invidious_instance,
348 },
349 item.fullplaylist().unwrap().videos
350 [singleplaylistitem.videos_view.selected - 1]
351 .minivideo()
352 .unwrap()
353 .id
354 ),
355 None => String::from("not avaliable"),
356 },
357 )]
358 } else {
359 vec![(String::from("hover-url"), String::from("not avaliable"))]
360 }
361 }
362 /// update colours and layout every render
363 fn update_appearance(
364 &mut self,
365 appearance: &AppearanceConfig,
366 iteminfo: &tui_additions::framework::ItemInfo,
367 ) {
368 self.grid.set_border_type(appearance.borders);
369
370 if iteminfo.selected {
371 self.grid
372 .set_border_style(Style::default().fg(appearance.colors.outline_selected));
373 } else if iteminfo.hover {
374 self.grid
375 .set_border_style(Style::default().fg(appearance.colors.outline_hover));
376 } else {
377 self.grid
378 .set_border_style(Style::default().fg(appearance.colors.outline));
379 }
380
381 self.r#type
382 .update_appearance(appearance, iteminfo, &mut self.grid);
383 }
384
385 /// update hover item preview
386 fn update(&mut self) {
387 if let SingleItemType::Playlist(singleplaylistitem) = &mut self.r#type {
388 let SinglePlaylistItem {
389 hovered_video,
390 videos_view,
391 ..
392 } = &mut **singleplaylistitem;
393 if videos_view.items.is_empty() || videos_view.selected == 0 {
394 hovered_video.item = None;
395 return;
396 }
397
398 if hovered_video.item.is_none()
399 || self.item.as_ref().unwrap().fullplaylist().unwrap().videos
400 [videos_view.selected - 1]
401 .id()
402 != hovered_video.item.as_ref().unwrap().id()
403 {
404 hovered_video.item = Some(
405 self.item.as_ref().unwrap().fullplaylist().unwrap().videos
406 [videos_view.selected - 1]
407 .clone(),
408 );
409 }
410 }
411 }
412
413 /// handle enter presses
414 fn select_at_cursor(
415 &mut self,
416 framework: &mut FrameworkClean,
417 // info: tui_additions::framework::ItemInfo,
418 ) {
419 match &mut self.r#type {
420 SingleItemType::Video(singlevideoitem) => {
421 let command_string = singlevideoitem.commands[singlevideoitem.textlist.selected]
422 .1
423 .clone();
424
425 // check if the command starts with an ':' which case should be captured
426 framework
427 .data
428 .state
429 .get_mut::<Tasks>()
430 .unwrap()
431 .priority
432 .push(Task::Command(apply_envs(command_string)));
433 }
434 SingleItemType::Playlist(singleplaylistitem) => {
435 let command_string = singleplaylistitem.commands
436 [singleplaylistitem.commands_view.selected]
437 .1
438 .clone();
439
440 // checks for special cases
441 match command_string.as_str() {
442 "%switch-view%" => {
443 singleplaylistitem.is_commands_view = !singleplaylistitem.is_commands_view;
444 // self.update_appearance(
445 // framework.data.global.get::<AppearanceConfig>().unwrap(),
446 // &info,
447 // );
448 *framework.data.global.get_mut::<Message>().unwrap() =
449 Message::Success(String::from("Switched view"));
450 }
451 _ => {
452 // check if the command starts with an ':' which case should be captured
453 framework
454 .data
455 .state
456 .get_mut::<Tasks>()
457 .unwrap()
458 .priority
459 .push(Task::Command(apply_envs(command_string)));
460 }
461 };
462 }
463 _ => return,
464 }
465
466 framework
467 .data
468 .state
469 .get_mut::<Tasks>()
470 .unwrap()
471 .priority
472 .push(Task::RenderAll);
473 }
474}
475
476impl FrameworkItem for SingleItem {
477 fn render(
478 &mut self,
479 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
480 framework: &mut tui_additions::framework::FrameworkClean,
481 area: tui::layout::Rect,
482 popup_render: bool,
483 info: tui_additions::framework::ItemInfo,
484 ) {
485 let status = framework.data.global.get::<Status>().unwrap();
486 if popup_render {
487 return;
488 }
489
490 if status.provider_updated {
491 set_envs(
492 self.infalte_item_update(
493 framework.data.global.get::<MainConfig>().unwrap(),
494 status,
495 )
496 .into_iter(),
497 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
498 );
499 }
500
501 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
502 self.update_appearance(appearance, &info);
503
504 let chunks = self.grid.chunks(area).unwrap()[0].clone();
505
506 frame.render_widget(self.grid.clone(), area);
507
508 // only continue if `self.item` is Some
509 if self.item.is_none() {
510 return;
511 }
512
513 match &mut self.r#type {
514 SingleItemType::Video(typeinfo) => {
515 // 2 by 1 grid, item info in the first cell and textlist at the second
516 if status.provider_updated {
517 typeinfo.update_provider().into_iter().for_each(|index| {
518 typeinfo.textlist.items[index] = typeinfo.commands[index].0.clone().replace(
519 "${provider}",
520 framework
521 .data
522 .global
523 .get::<Status>()
524 .unwrap()
525 .provider
526 .as_str(),
527 )
528 });
529 }
530 self.iteminfo
531 .render(frame, framework, chunks[0], popup_render, info);
532 typeinfo.textlist.set_height(chunks[1].height);
533 frame.render_widget(typeinfo.textlist.clone(), chunks[1]);
534 }
535 SingleItemType::Playlist(typeinfo) => {
536 // 3 by 1 grid if hovering a video inside the playlist
537 // if not then 2 by 1
538 //
539 // item info in the first cell, textlists in the second, hovering video on 3rd (if
540 // present)
541 if typeinfo.is_commands_view {
542 if status.provider_updated {
543 typeinfo.update_provider().into_iter().for_each(|index| {
544 typeinfo.commands_view.items[index] =
545 typeinfo.commands[index].0.clone().replace(
546 "${provider}",
547 framework
548 .data
549 .global
550 .get::<Status>()
551 .unwrap()
552 .provider
553 .as_str(),
554 )
555 });
556 }
557 typeinfo.commands_view.set_height(chunks[1].height);
558 frame.render_widget(typeinfo.commands_view.clone(), chunks[1]);
559 } else {
560 typeinfo.videos_view.set_height(chunks[1].height);
561 frame.render_widget(typeinfo.videos_view.clone(), chunks[1]);
562
563 if typeinfo.videos_view.selected != 0 {
564 typeinfo.hovered_video.render(
565 frame,
566 framework,
567 chunks[2],
568 popup_render,
569 info,
570 );
571 }
572 }
573 self.iteminfo
574 .render(frame, framework, chunks[0], popup_render, info);
575 }
576 SingleItemType::None => {}
577 }
578 }
579
580 fn load_item(
581 &mut self,
582 framework: &mut tui_additions::framework::FrameworkClean,
583 _info: tui_additions::framework::ItemInfo,
584 ) -> Result<(), Box<dyn std::error::Error>> {
585 *self = Self::default();
586
587 let page = framework.data.state.get::<Page>().unwrap();
588
589 let r#type = if let Page::SingleItem(r#type) = page {
590 r#type
591 } else {
592 unreachable!("item `SingleItem` cannot be used in {page:?}")
593 };
594
595 let client = &framework.data.global.get::<InvidiousClient>().unwrap().0;
596
597 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
598 // load items using the invidious api
599 // gets the item that it needs to load from `data.state.Page`
600 self.item = match r#type {
601 SingleItemPage::Video(id) => {
602 self.r#type = SingleItemType::Video(SingleVideoItem::new(
603 framework.data.global.get::<CommandsConfig>().unwrap(),
604 ));
605 let video = Item::from_full_video(client.video(id, None)?, mainconfig.image_index);
606 if mainconfig.images.display() {
607 download_all_images(vec![(&video).into()]);
608 }
609 Some(video)
610 }
611 SingleItemPage::Playlist(id) => {
612 let playlist =
613 Item::from_full_playlist(client.playlist(id, None)?, mainconfig.image_index);
614 let videos = &playlist.fullplaylist()?.videos;
615 self.r#type = SingleItemType::Playlist(
616 SinglePlaylistItem::new(
617 framework.data.global.get::<CommandsConfig>().unwrap(),
618 videos,
619 )
620 .into(),
621 );
622
623 if mainconfig.images.display() {
624 download_all_images({
625 let mut items = videos.iter().map(|item| item.into()).collect::<Vec<_>>();
626 items.extend([(&playlist).into()].into_iter());
627 items
628 });
629 }
630
631 Some(playlist)
632 }
633 };
634 self.iteminfo.item = self.item.clone();
635
636 if let Some(item) = &self.item {
637 if item.is_unknown() {
638 return Ok(());
639 }
640
641 let item = item.clone();
642 let max_watch_history = framework
643 .data
644 .global
645 .get::<MainConfig>()
646 .unwrap()
647 .max_watch_history;
648
649 // push to watch history
650 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
651 watch_history.push(item, max_watch_history);
652 watch_history.save()?;
653 }
654
655 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
656 // need to update provider every time the item loads or else it will display `${provider}`
657 // instead of the actual provider (e.g. `YouTube`)
658 set_envs(
659 self.r#type
660 .inflate_load(
661 mainconfig,
662 framework.data.global.get::<Status>().unwrap(),
663 &self.item,
664 )
665 .into_iter(),
666 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
667 );
668
669 set_envs(
670 self.infalte_item_update(mainconfig, framework.data.global.get::<Status>().unwrap())
671 .into_iter(),
672 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
673 );
674
675 Ok(())
676 }
677
678 fn key_event(
679 &mut self,
680 framework: &mut tui_additions::framework::FrameworkClean,
681 key: crossterm::event::KeyEvent,
682 info: tui_additions::framework::ItemInfo,
683 ) -> Result<(), Box<dyn std::error::Error>> {
684 // discard any key inputs if `self.is_none()` becuase nothing can happen if self is none
685 if self.r#type.is_none() {
686 return Ok(());
687 }
688
689 let action = if let Some(action) = framework
690 .data
691 .global
692 .get::<KeyBindingsConfig>()
693 .unwrap()
694 .get(key)
695 {
696 action
697 } else {
698 return Ok(());
699 };
700
701 let updated = match &mut self.r#type {
702 SingleItemType::Video(singlevideoitem) => match action {
703 // move the cursor in the textlist, only update the screen if it is changed
704 KeyAction::MoveUp => singlevideoitem.textlist.up().is_ok(),
705 KeyAction::MoveDown => singlevideoitem.textlist.down().is_ok(),
706 KeyAction::MoveLeft => singlevideoitem.textlist.first().is_ok(),
707 KeyAction::MoveRight => singlevideoitem.textlist.last().is_ok(),
708 KeyAction::Select => {
709 self.select_at_cursor(framework);
710 return Ok(());
711 }
712 _ => false,
713 },
714 SingleItemType::Playlist(singleplaylistitem) => {
715 // there are 2 possible states in a playlist item
716 // they are handelled separately
717 if singleplaylistitem.is_commands_view {
718 match action {
719 KeyAction::MoveUp => singleplaylistitem.commands_view.up().is_ok(),
720 KeyAction::MoveDown => singleplaylistitem.commands_view.down().is_ok(),
721 KeyAction::MoveLeft => singleplaylistitem.commands_view.first().is_ok(),
722 KeyAction::MoveRight => singleplaylistitem.commands_view.last().is_ok(),
723 KeyAction::Select => {
724 self.select_at_cursor(framework);
725 return Ok(());
726 }
727 _ => false,
728 }
729 } else {
730 let updated = match action {
731 // checks if it is updated, if it is and selected is not 0 (is hovering on
732 // a video), then also need to update the iteminfo
733 KeyAction::MoveUp => {
734 if singleplaylistitem.videos_view.selected == 1 {
735 // going from a hovering video to not hovering will make the image
736 // stay on the screen, therefore it needs to be removed by clearing
737 // the screen
738 framework
739 .data
740 .state
741 .get_mut::<Tasks>()
742 .unwrap()
743 .priority
744 .push(Task::ClearPage);
745 } else if singleplaylistitem.videos_view.selected == 0 {
746 return Ok(());
747 }
748
749 let updated = singleplaylistitem.videos_view.up().is_ok();
750 if singleplaylistitem.videos_view.selected != 0 {
751 singleplaylistitem.hovered_video.item = Some(
752 self.item.as_ref().unwrap().fullplaylist()?.videos
753 [singleplaylistitem.videos_view.selected - 1]
754 .clone(),
755 );
756 }
757 updated
758 }
759 KeyAction::MoveDown => {
760 if singleplaylistitem.videos_view.selected
761 == singleplaylistitem.videos_view.items.len() - 1
762 {
763 return Ok(());
764 }
765
766 let updated = singleplaylistitem.videos_view.down().is_ok();
767 if updated && singleplaylistitem.videos_view.selected != 0 {
768 singleplaylistitem.hovered_video.item = Some(
769 self.item.as_ref().unwrap().fullplaylist()?.videos
770 [singleplaylistitem.videos_view.selected - 1]
771 .clone(),
772 );
773 }
774 updated
775 }
776 KeyAction::MoveLeft => {
777 if singleplaylistitem.videos_view.selected != 0 {
778 framework
779 .data
780 .state
781 .get_mut::<Tasks>()
782 .unwrap()
783 .priority
784 .push(Task::ClearPage);
785 } else if singleplaylistitem.videos_view.selected == 0 {
786 return Ok(());
787 }
788
789 let updated = singleplaylistitem.videos_view.first().is_ok();
790 singleplaylistitem.hovered_video.item = None;
791 updated
792 }
793 KeyAction::MoveRight => {
794 if singleplaylistitem.videos_view.selected
795 == singleplaylistitem.videos_view.items.len() - 1
796 {
797 return Ok(());
798 }
799 let updated = singleplaylistitem.videos_view.last().is_ok();
800 if updated && singleplaylistitem.videos_view.selected != 0 {
801 singleplaylistitem.hovered_video.item = Some(
802 self.item.as_ref().unwrap().fullplaylist()?.videos
803 [singleplaylistitem.videos_view.selected - 1]
804 .clone(),
805 );
806 }
807 updated
808 }
809 KeyAction::Select => {
810 if singleplaylistitem.videos_view.selected == 0 {
811 singleplaylistitem.is_commands_view =
812 !singleplaylistitem.is_commands_view;
813 self.update_appearance(
814 framework.data.global.get::<AppearanceConfig>().unwrap(),
815 &info,
816 );
817 *framework.data.global.get_mut::<Message>().unwrap() =
818 Message::Success(String::from("Switched view"));
819 } else {
820 framework
821 .data
822 .state
823 .get_mut::<Tasks>()
824 .unwrap()
825 .priority
826 .push(Task::LoadPage(Page::SingleItem(SingleItemPage::Video(
827 self.item.as_ref().unwrap().fullplaylist()?.videos
828 [singleplaylistitem.videos_view.selected - 1]
829 .minivideo()?
830 .id
831 .clone(),
832 ))));
833 }
834
835 true
836 }
837 _ => false,
838 };
839
840 if updated {
841 framework
842 .data
843 .global
844 .get_mut::<Status>()
845 .unwrap()
846 .render_image = true;
847 set_envs(
848 self.infalte_item_update(
849 framework.data.global.get::<MainConfig>().unwrap(),
850 framework.data.global.get::<Status>().unwrap(),
851 )
852 .into_iter(),
853 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
854 );
855 }
856
857 updated
858 }
859 }
860 SingleItemType::None => false,
861 };
862
863 if updated {
864 framework
865 .data
866 .state
867 .get_mut::<Tasks>()
868 .unwrap()
869 .priority
870 .push(Task::RenderAll);
871 }
872
873 Ok(())
874 }
875
876 fn mouse_event(
877 &mut self,
878 framework: &mut FrameworkClean,
879 x: u16,
880 y: u16,
881 _absolute_x: u16,
882 _absolute_y: u16,
883 ) -> bool {
884 let chunk = self
885 .grid
886 .chunks(
887 if let Some(prev_frame) = framework.data.global.get::<Status>().unwrap().prev_frame
888 {
889 prev_frame
890 } else {
891 return false;
892 },
893 )
894 .unwrap()[0][1];
895
896 if !chunk.intersects(Rect::new(x, y, 1, 1)) {
897 return false;
898 }
899
900 let textlist = match &mut self.r#type {
901 SingleItemType::Video(SingleVideoItem { textlist, .. }) => textlist,
902 SingleItemType::Playlist(singleplaylistitem) => {
903 if singleplaylistitem.is_commands_view {
904 &mut singleplaylistitem.commands_view
905 } else {
906 let y = (y - chunk.y) as usize + singleplaylistitem.videos_view.scroll;
907 if singleplaylistitem.videos_view.selected != 0 && y == 0 {
908 framework
909 .data
910 .state
911 .get_mut::<Tasks>()
912 .unwrap()
913 .priority
914 .push(Task::ClearPage);
915 }
916 &mut singleplaylistitem.videos_view
917 }
918 }
919 _ => return false,
920 };
921
922 let y = (y - chunk.y) as usize + textlist.scroll;
923
924 // clicking on already selected item
925 if y == textlist.selected || y == textlist.selected + 2 || y == textlist.selected + 1 {
926 self.select_at_cursor(framework);
927 return true;
928 }
929
930 // clicking on rows after the last item
931 if y > textlist.items.len() + 1 {
932 return false;
933 }
934
935 // moving the cursor
936 if y <= textlist.selected {
937 textlist.selected = y;
938 }
939
940 if y >= textlist.selected + 2 {
941 textlist.selected = y - 2;
942 }
943
944 self.update();
945
946 set_envs(
947 self.infalte_item_update(
948 framework.data.global.get::<MainConfig>().unwrap(),
949 framework.data.global.get::<Status>().unwrap(),
950 )
951 .into_iter(),
952 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
953 );
954
955 // render the new image
956 framework
957 .data
958 .global
959 .get_mut::<Status>()
960 .unwrap()
961 .render_image = true;
962
963 true
964 }
965}