src/app/pages/global.rs
122 lines · 3379 bytes
1use std::fmt;
2
3use crossterm::event::KeyCode;
4use tui::{style::{Color, Style}, widgets::{Borders, Paragraph, BorderType, Block}, layout::{Alignment, Rect}, Frame, backend::Backend};
5
6use crate::{
7 app::app::App,
8 traits::{KeyInput, SelectItem},
9};
10
11#[derive(Debug, Clone)]
12pub enum GlobalItem {
13 SearchBar(String),
14 MessageBar,
15}
16
17#[derive(Debug, Clone, Copy)]
18pub enum Mode {
19 Youtube,
20 Invidious,
21}
22
23impl fmt::Display for Mode {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self {
26 Mode::Youtube => write!(f, "Youtube"),
27 Mode::Invidious => write!(f, "Invidious"),
28 }
29 }
30}
31
32impl Mode {
33 pub fn toggle(&mut self) {
34 match self {
35 Mode::Youtube => *self = Mode::Invidious,
36 Mode::Invidious => *self = Mode::Youtube,
37 }
38 }
39}
40
41impl SelectItem for GlobalItem {
42 fn select(&mut self, app: App) -> (App, bool) {
43 let selected = match self {
44 GlobalItem::SearchBar(_) => true,
45 _ => false,
46 };
47
48 (app, selected)
49 }
50
51 fn selectable(&self) -> bool {
52 match self {
53 GlobalItem::SearchBar(_) => true,
54 _ => false,
55 }
56 }
57}
58
59impl KeyInput for GlobalItem {
60 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
61 match self {
62 GlobalItem::SearchBar(search_bar) => match key {
63 KeyCode::Char(c) => {
64 search_bar.push(c);
65 }
66 KeyCode::Backspace => {
67 search_bar.pop();
68 }
69 KeyCode::Enter => {}
70 _ => {}
71 },
72 _ => {}
73 }
74
75 (true, app)
76 }
77}
78
79impl GlobalItem {
80 pub fn render_item<B: Backend>(
81 &self,
82 frame: &mut Frame<B>,
83 rect: Rect,
84 selected: bool,
85 hover: bool,
86 message: &Option<String>,
87 ) {
88 match self {
89 GlobalItem::SearchBar(search) => {
90 let block = Block::default()
91 .border_type(BorderType::Rounded)
92 .borders(Borders::ALL)
93 .border_style(tui::style::Style::default().fg(if selected {
94 Color::LightBlue
95 } else if hover {
96 Color::LightRed
97 } else {
98 Color::Reset
99 }))
100 .title("Search YouTube")
101 .title_alignment(Alignment::Center);
102 let paragraph = Paragraph::new(search.clone()).block(block);
103 frame.render_widget(paragraph, rect);
104 }
105 GlobalItem::MessageBar => {
106 // let color = Color::LightYellow;
107 let block = Block::default()
108 .border_type(BorderType::Rounded)
109 .borders(Borders::ALL)
110 .border_style(Style::default().fg(if message.is_some() {
111 Color::LightRed
112 } else {
113 Color::Reset
114 }));
115 let paragraph =
116 Paragraph::new(message.clone().unwrap_or(String::from("All good :)")))
117 .block(block);
118 frame.render_widget(paragraph, rect);
119 }
120 }
121 }
122}