src/items/channeldisplay.rs
379 lines · 14179 bytes
1use super::ItemInfo;
2use crate::{
3 config::{AppearanceConfig, KeyBindingsConfig, MainConfig},
4 global::{
5 functions::download_all_images,
6 structs::{
7 ChannelDisplayPageType, InvidiousClient, Item, KeyAction, Message, Page,
8 SingleItemPage, Task, Tasks,
9 },
10 },
11};
12use tui::{
13 layout::Constraint,
14 style::Style,
15 widgets::{Block, Borders},
16};
17use tui_additions::{
18 framework::FrameworkItem,
19 widgets::{Grid, TextList},
20};
21
22/// the 4 pages that a channel has (including the default "blank" page when loading)
23#[derive(Clone)]
24pub enum ChannelDisplay {
25 None,
26 Main {
27 channel: Item,
28 },
29 Videos {
30 videos: Vec<Item>,
31 textlist: TextList,
32 iteminfo: ItemInfo,
33 grid: Grid,
34 },
35 Playlists {
36 playlists: Vec<Item>,
37 textlist: TextList,
38 iteminfo: ItemInfo,
39 grid: Grid,
40 },
41}
42
43impl Default for ChannelDisplay {
44 fn default() -> Self {
45 Self::None
46 }
47}
48
49impl ChannelDisplay {
50 fn update_appearance(
51 &mut self,
52 info: &tui_additions::framework::ItemInfo,
53 appearance: &AppearanceConfig,
54 ) {
55 // is runs on every render
56 match self {
57 ChannelDisplay::Videos { textlist, grid, .. } => {
58 if info.selected {
59 textlist
60 .set_selected_style(Style::default().fg(appearance.colors.text_special));
61 textlist.set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
62 grid.set_border_style(Style::default().fg(appearance.colors.outline_selected));
63 } else {
64 if info.hover {
65 grid.set_border_style(Style::default().fg(appearance.colors.outline_hover));
66 } else {
67 grid.set_border_style(Style::default().fg(appearance.colors.outline));
68 }
69 textlist
70 .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
71 textlist
72 .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
73 }
74 }
75 ChannelDisplay::Playlists { textlist, grid, .. } => {
76 if info.selected {
77 textlist
78 .set_selected_style(Style::default().fg(appearance.colors.text_special));
79 textlist.set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
80 grid.set_border_style(Style::default().fg(appearance.colors.outline_selected));
81 } else {
82 if info.hover {
83 grid.set_border_style(Style::default().fg(appearance.colors.outline_hover));
84 } else {
85 grid.set_border_style(Style::default().fg(appearance.colors.outline));
86 }
87 textlist
88 .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
89 textlist
90 .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
91 }
92 }
93 _ => {}
94 }
95 }
96
97 pub fn selectable(&self) -> bool {
98 !(matches!(self, Self::Main { .. }) || matches!(self, Self::None))
99 }
100}
101
102impl FrameworkItem for ChannelDisplay {
103 fn render(
104 &mut self,
105 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
106 framework: &mut tui_additions::framework::FrameworkClean,
107 area: tui::layout::Rect,
108 popup_render: bool,
109 info: tui_additions::framework::ItemInfo,
110 ) {
111 if popup_render {
112 return;
113 }
114
115 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
116 self.update_appearance(&info, appearance);
117 let border_style = Style::default().fg(if info.hover {
118 appearance.colors.outline_hover
119 } else if info.selected {
120 appearance.colors.outline_selected
121 } else {
122 appearance.colors.outline
123 });
124
125 // matches itself to render differently depending on the enum variation
126 match self {
127 Self::None => {
128 // the block is created when rendered because its quite simple and should not be a
129 // performance issue
130 let block = Block::default()
131 .border_type(appearance.borders)
132 .borders(Borders::ALL)
133 .border_style(border_style);
134 frame.render_widget(block, area);
135 }
136 Self::Main { channel } => {
137 let block = Block::default()
138 .border_type(appearance.borders)
139 .borders(Borders::ALL)
140 .border_style(border_style);
141 let inner = block.inner(area);
142
143 frame.render_widget(block, area);
144 ItemInfo {
145 item: Some(channel.clone()),
146 }
147 .render(frame, framework, inner, popup_render, info);
148 }
149 Self::Videos {
150 textlist,
151 iteminfo,
152 grid,
153 ..
154 }
155 | Self::Playlists {
156 textlist,
157 iteminfo,
158 grid,
159 ..
160 } => {
161 let inner = &grid.chunks(area).unwrap()[0];
162
163 frame.render_widget(grid.clone(), area);
164 textlist.set_height(inner[0].height);
165 frame.render_widget(textlist.clone(), inner[0]);
166 iteminfo.render(frame, framework, inner[1], popup_render, info);
167 }
168 }
169 }
170
171 fn select(&mut self, _framework: &mut tui_additions::framework::FrameworkClean) -> bool {
172 self.selectable()
173 }
174
175 fn key_event(
176 &mut self,
177 framework: &mut tui_additions::framework::FrameworkClean,
178 key: crossterm::event::KeyEvent,
179 _info: tui_additions::framework::ItemInfo,
180 ) -> Result<(), Box<dyn std::error::Error>> {
181 let action = if let Some(keyactions) = framework
182 .data
183 .global
184 .get::<KeyBindingsConfig>()
185 .unwrap()
186 .0
187 .get(&key.code)
188 {
189 if let Some(action) = keyactions.get(&key.modifiers.bits()) {
190 *action
191 } else {
192 return Ok(());
193 }
194 } else {
195 return Ok(());
196 };
197
198 match self {
199 Self::Videos {
200 videos,
201 textlist,
202 iteminfo,
203 ..
204 } => {
205 let updated = match action {
206 KeyAction::MoveUp => textlist.up().is_ok(),
207 KeyAction::MoveDown => textlist.down().is_ok(),
208 KeyAction::MoveLeft => textlist.first().is_ok(),
209 KeyAction::MoveRight => textlist.last().is_ok(),
210 KeyAction::Select => {
211 // on select loads that in singleitem
212 if !videos.is_empty() {
213 framework
214 .data
215 .state
216 .get_mut::<Tasks>()
217 .unwrap()
218 .priority
219 .push(Task::LoadPage(Page::SingleItem(SingleItemPage::Video(
220 videos[textlist.selected].minivideo()?.id.clone(),
221 ))));
222 } else {
223 *framework.data.global.get_mut::<Message>().unwrap() =
224 Message::Error(String::from("There is nothing to select"));
225 }
226 true
227 }
228 _ => false,
229 };
230
231 // only update iteminfo (it requires cloning) if changed and not empty
232 if updated && !videos.is_empty() {
233 framework
234 .data
235 .state
236 .get_mut::<Tasks>()
237 .unwrap()
238 .priority
239 .push(Task::RenderAll);
240 iteminfo.item = Some(videos[textlist.selected].clone());
241 }
242 }
243 Self::Playlists {
244 playlists,
245 textlist,
246 iteminfo,
247 ..
248 } => {
249 let updated = match action {
250 KeyAction::MoveUp => textlist.up().is_ok(),
251 KeyAction::MoveDown => textlist.down().is_ok(),
252 KeyAction::MoveLeft => textlist.first().is_ok(),
253 KeyAction::MoveRight => textlist.last().is_ok(),
254 KeyAction::Select => {
255 if !playlists.is_empty() {
256 framework
257 .data
258 .state
259 .get_mut::<Tasks>()
260 .unwrap()
261 .priority
262 .push(Task::LoadPage(Page::SingleItem(SingleItemPage::Playlist(
263 playlists[textlist.selected].miniplaylist()?.id.clone(),
264 ))));
265 } else {
266 *framework.data.global.get_mut::<Message>().unwrap() =
267 Message::Error(String::from("There is nothing to select"));
268 }
269 true
270 }
271 _ => false,
272 };
273
274 if updated && !playlists.is_empty() {
275 framework
276 .data
277 .state
278 .get_mut::<Tasks>()
279 .unwrap()
280 .priority
281 .push(Task::RenderAll);
282 iteminfo.item = Some(playlists[textlist.selected].clone());
283 }
284 }
285 _ => {}
286 }
287
288 Ok(())
289 }
290
291 fn load_item(
292 &mut self,
293 framework: &mut tui_additions::framework::FrameworkClean,
294 _info: tui_additions::framework::ItemInfo,
295 ) -> Result<(), Box<dyn std::error::Error>> {
296 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
297 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
298 let page = framework.data.state.get::<Page>().unwrap().channeldisplay();
299
300 match page.r#type {
301 ChannelDisplayPageType::Main => {
302 let channel = Item::from_full_channel(
303 framework
304 .data
305 .global
306 .get::<InvidiousClient>()
307 .unwrap()
308 .0
309 .channel(&page.id, None)?,
310 mainconfig.image_index,
311 );
312 download_all_images(vec![(&channel).into()]);
313 *self = Self::Main { channel }
314 }
315 ChannelDisplayPageType::Videos => {
316 let videos = framework
317 .data
318 .global
319 .get::<InvidiousClient>()
320 .unwrap()
321 .0
322 .channel_videos(&page.id, None)?
323 .videos
324 .into_iter()
325 .map(|video| Item::from_channel_video(video, mainconfig.image_index))
326 .collect::<Vec<_>>();
327 download_all_images(videos.iter().map(|item| item.into()).collect());
328 *self = Self::Videos {
329 textlist: TextList::default()
330 .ascii_only(!mainconfig.allow_unicode)
331 .border_type(appearance.borders)
332 .style(Style::default().fg(appearance.colors.text))
333 .items(&videos)?,
334 iteminfo: ItemInfo {
335 item: videos.first().cloned(),
336 },
337 grid: Grid::new(
338 vec![Constraint::Percentage(60), Constraint::Percentage(40)],
339 vec![Constraint::Percentage(100)],
340 )?
341 .border_type(appearance.borders),
342 videos,
343 };
344 }
345 ChannelDisplayPageType::Playlists => {
346 let playlists = framework
347 .data
348 .global
349 .get::<InvidiousClient>()
350 .unwrap()
351 .0
352 .channel_playlists(&page.id, None)?
353 .playlists
354 .into_iter()
355 .map(Item::from_channel_playlist)
356 .collect::<Vec<_>>();
357 download_all_images(playlists.iter().map(|item| item.into()).collect());
358 *self = Self::Playlists {
359 textlist: TextList::default()
360 .ascii_only(!mainconfig.allow_unicode)
361 .border_type(appearance.borders)
362 .style(Style::default().fg(appearance.colors.text))
363 .items(&playlists)?,
364 iteminfo: ItemInfo {
365 item: playlists.first().cloned(),
366 },
367 grid: Grid::new(
368 vec![Constraint::Percentage(60), Constraint::Percentage(40)],
369 vec![Constraint::Percentage(100)],
370 )?
371 .border_type(appearance.borders),
372 playlists,
373 };
374 }
375 }
376
377 Ok(())
378 }
379}