src/items/itemlist.rs
226 lines · 7015 bytes
1use std::error::Error;
2
3use tui::{layout::Constraint, style::Style};
4use tui_additions::{
5 framework::{Framework, FrameworkItem},
6 widgets::{Grid, TextList},
7};
8
9use super::ItemInfo;
10use crate::{
11 config::{AppearanceConfig, KeyBindingsConfig, MainConfig},
12 global::{
13 functions::download_all_images,
14 invidiousclient::InvidiousClient,
15 item::Item,
16 keyaction::KeyAction,
17 page::{MainMenuPage, Page},
18 tasks::{Task, Tasks},
19 },
20};
21
22// An item list displays a list of items
23// It consists of a 1 x 2 grid, with the left cell displaying a text list, the right displaying item info of the currently hovered item
24#[derive(Clone)]
25pub struct ItemList {
26 pub info: ItemInfo,
27 pub items: Vec<Item>,
28 pub textlist: TextList,
29 pub grid: Grid,
30}
31
32impl ItemList {
33 pub fn new(framework: &mut Framework) -> Self {
34 Self {
35 info: ItemInfo::default(),
36 items: Vec::new(),
37 textlist: TextList::default()
38 .border_type(
39 framework
40 .data
41 .global
42 .get::<AppearanceConfig>()
43 .unwrap()
44 .borders,
45 )
46 .ascii_only(
47 !framework
48 .data
49 .global
50 .get::<MainConfig>()
51 .unwrap()
52 .allow_unicode,
53 )
54 .non_ascii_replace(' '),
55 grid: Grid::new(
56 vec![Constraint::Percentage(60), Constraint::Percentage(40)],
57 vec![Constraint::Percentage(100)],
58 )
59 .unwrap(),
60 }
61 }
62}
63
64impl FrameworkItem for ItemList {
65 fn render(
66 &mut self,
67 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
68 framework: &mut tui_additions::framework::FrameworkClean,
69 area: tui::layout::Rect,
70 popup_render: bool,
71 info: tui_additions::framework::ItemInfo,
72 ) {
73 if popup_render {
74 return;
75 }
76
77 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
78
79 // creates the grid
80 self.grid.set_border_type(appearance.borders);
81 self.grid
82 .set_border_style(Style::default().fg(if info.hover {
83 appearance.colors.outline_hover
84 } else if info.selected {
85 appearance.colors.outline_selected
86 } else {
87 appearance.colors.outline
88 }));
89
90 let grid = self.grid.clone();
91 let chunks = &grid.chunks(area).unwrap()[0];
92
93 // creates the text list in cell (0, 1)
94 self.textlist.set_height(chunks[0].height);
95 self.textlist
96 .set_cursor_style(Style::default().fg(if info.selected {
97 appearance.colors.outline_hover
98 } else {
99 appearance.colors.outline_secondary
100 }));
101
102 let textlist = self.textlist.clone();
103
104 frame.render_widget(grid, area);
105 frame.render_widget(textlist, chunks[0]);
106
107 // used the `.render()` function in self.info because it is an ItemInfo and impls FrameworkItem instead of Widget
108 self.info
109 .render(frame, framework, chunks[1], popup_render, info);
110 }
111
112 fn selectable(&self) -> bool {
113 true
114 }
115
116 fn load_item(
117 &mut self,
118 framework: &mut tui_additions::framework::FrameworkClean,
119 _info: tui_additions::framework::ItemInfo,
120 ) -> Result<(), Box<dyn Error>> {
121 let page = framework.data.state.get::<Page>().unwrap();
122 let image_index = framework
123 .data
124 .global
125 .get::<MainConfig>()
126 .unwrap()
127 .image_index;
128 let client = &framework.data.global.get::<InvidiousClient>().unwrap().0;
129
130 // fetch the items using the invidious api
131 match page {
132 Page::MainMenu(MainMenuPage::Trending) => {
133 self.items = client
134 .trending(None)?
135 .videos
136 .into_iter()
137 .map(|item| Item::from_trending_video(item, image_index))
138 .collect();
139 }
140 Page::MainMenu(MainMenuPage::Popular) => {
141 self.items = client
142 .popular(None)?
143 .items
144 .into_iter()
145 .map(|item| Item::from_popular_item(item, image_index))
146 .collect();
147 }
148 Page::Search(search) => {
149 self.items = client
150 .search(Some(&search.to_string()))?
151 .items
152 .into_iter()
153 .map(|item| Item::from_search_item(item, image_index))
154 .collect();
155 }
156 }
157
158 // download thumbnails of all videos in the list
159 download_all_images(self.items.iter().map(|item| item.into()).collect());
160
161 // update the items in text list
162 self.textlist.set_items(&self.items).unwrap();
163 self.update();
164
165 Ok(())
166 }
167
168 fn key_event(
169 &mut self,
170 framework: &mut tui_additions::framework::FrameworkClean,
171 key: crossterm::event::KeyEvent,
172 _info: tui_additions::framework::ItemInfo,
173 ) -> Result<(), Box<dyn Error>> {
174 let action = if let Some(keyactions) = framework
175 .data
176 .global
177 .get::<KeyBindingsConfig>()
178 .unwrap()
179 .0
180 .get(&key.code)
181 {
182 if let Some(action) = keyactions.get(&key.modifiers.bits()) {
183 *action
184 } else {
185 return Ok(());
186 }
187 } else {
188 return Ok(());
189 };
190
191 // move the textlist cursor in the corresponding directions
192 let updated = match action {
193 KeyAction::MoveUp => self.textlist.up().is_ok(),
194 KeyAction::MoveDown => self.textlist.down().is_ok(),
195 KeyAction::MoveLeft => self.textlist.first().is_ok(),
196 KeyAction::MoveRight => self.textlist.last().is_ok(),
197 _ => false,
198 };
199
200 // only create a render task if the key event actually changed something
201 if updated {
202 self.info.item = Some(self.items[self.textlist.selected].clone());
203 framework
204 .data
205 .state
206 .get_mut::<Tasks>()
207 .unwrap()
208 .last
209 .push(Task::RenderAll);
210 }
211
212 Ok(())
213 }
214}
215
216impl ItemList {
217 // change `self.item` to the currently selected item
218 pub fn update(&mut self) {
219 if self.items.len() == 0 {
220 self.info.item = None;
221 return;
222 }
223
224 self.info.item = Some(self.items[self.textlist.selected].clone());
225 }
226}