src/app/pages/global.rs

6f8b64c4fa1b89868a2dbaa1bbd9fda20ecd0768
youtube-tui/src/app/pages/global.rs history · blame · raw

246 lines · 8525 bytes

  1use std::fmt;
  2
  3use crossterm::event::KeyCode;
  4use tui::{
  5    backend::Backend,
  6    layout::{Alignment, Rect},
  7    style::{Color, Style},
  8    widgets::{Block, BorderType, Borders, Clear, Paragraph},
  9    Frame,
 10};
 11
 12use crate::{
 13    app::app::App,
 14    functions::center_rect,
 15    structs::{Page, SearchSettings},
 16    traits::{KeyInput, SelectItem},
 17    widgets::{force_clear::ForceClear, horizontal_split::HorizontalSplit},
 18};
 19
 20use super::search::Search;
 21
 22#[derive(Debug, Clone)]
 23pub enum GlobalItem {
 24    SearchBar,
 25    SearchSettings,
 26    MessageBar,
 27}
 28
 29#[derive(Debug, Clone, Copy)]
 30pub enum Mode {
 31    Youtube,
 32    Invidious,
 33}
 34
 35impl fmt::Display for Mode {
 36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 37        match self {
 38            Mode::Youtube => write!(f, "Youtube"),
 39            Mode::Invidious => write!(f, "Invidious"),
 40        }
 41    }
 42}
 43
 44impl Mode {
 45    pub fn toggle(&mut self) {
 46        match self {
 47            Mode::Youtube => *self = Mode::Invidious,
 48            Mode::Invidious => *self = Mode::Youtube,
 49        }
 50    }
 51}
 52
 53impl SelectItem for GlobalItem {
 54    fn select(&mut self, mut app: App) -> (App, bool) {
 55        let selected = match self {
 56            GlobalItem::SearchBar => true,
 57            GlobalItem::SearchSettings => {
 58                app.popup_focus = true;
 59                true
 60            }
 61            _ => false,
 62        };
 63
 64        (app, selected)
 65    }
 66
 67    fn selectable(&self) -> bool {
 68        match self {
 69            GlobalItem::SearchBar | GlobalItem::SearchSettings => true,
 70            _ => false,
 71        }
 72    }
 73}
 74
 75impl KeyInput for GlobalItem {
 76    fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) {
 77        match self {
 78            GlobalItem::SearchBar => match key {
 79                KeyCode::Char(c) => {
 80                    app.search_text.push(c);
 81                }
 82                KeyCode::Backspace => {
 83                    app.search_text.pop();
 84                }
 85                KeyCode::Enter => {
 86                    if app.search_text.len() == 0 {
 87                        *app.message.lock().unwrap() =
 88                            Some(String::from("Search term cannot be empty"));
 89                    } else {
 90                        let state = Search::default();
 91                        let mut history = app.history.clone();
 92                        let search_text = app.search_text.clone();
 93                        let search_settings = app.search_settings.clone();
 94                        history.push(app.into());
 95
 96                        return (
 97                            false,
 98                            App {
 99                                history,
100                                page: Page::Search,
101                                selectable: App::selectable(&state),
102                                state,
103                                search_text,
104                                search_settings,
105                                load: true,
106                                ..Default::default()
107                            },
108                        );
109                    }
110                }
111                _ => {}
112            },
113            GlobalItem::SearchSettings => {
114                app.search_settings.key_input(key);
115            }
116            _ => {}
117        }
118
119        (true, app)
120    }
121}
122
123impl GlobalItem {
124    pub fn render_item<B: Backend>(
125        &self,
126        frame: &mut Frame<B>,
127        rect: Rect,
128        selected: bool,
129        hover: bool,
130        popup_render: bool,
131        message: &Option<String>,
132        search_settings: &mut SearchSettings,
133        search_text: &String,
134    ) -> bool {
135        let area = frame.size();
136        match self {
137            GlobalItem::SearchBar => {
138                let block = Block::default()
139                    .border_type(BorderType::Rounded)
140                    .borders(Borders::ALL)
141                    .border_style(tui::style::Style::default().fg(if selected {
142                        Color::LightBlue
143                    } else if hover {
144                        Color::LightRed
145                    } else {
146                        Color::Reset
147                    }))
148                    .title("Search YouTube")
149                    .title_alignment(Alignment::Center);
150                let paragraph = Paragraph::new(search_text.clone()).block(block);
151                frame.render_widget(paragraph, rect);
152            }
153            GlobalItem::MessageBar => {
154                // let color = Color::LightYellow;
155                let block = Block::default()
156                    .border_type(BorderType::Rounded)
157                    .borders(Borders::ALL)
158                    .border_style(Style::default().fg(if message.is_some() {
159                        Color::LightRed
160                    } else {
161                        Color::Reset
162                    }));
163                let paragraph =
164                    Paragraph::new(message.clone().unwrap_or(String::from("All good :)")))
165                        .block(block);
166                frame.render_widget(paragraph, rect);
167            }
168            GlobalItem::SearchSettings => {
169                let block = Block::default()
170                    .border_type(BorderType::Rounded)
171                    .borders(Borders::ALL)
172                    .border_style(Style::default().fg(if selected {
173                        Color::LightYellow
174                    } else if hover {
175                        Color::LightRed
176                    } else {
177                        Color::Reset
178                    }));
179                let paragraph = Paragraph::new(String::from("...")).block(block);
180                frame.render_widget(paragraph, rect);
181
182                if selected {
183                    if popup_render {
184                        match center_rect((area.width * 70 / 100, area.height * 70 / 100), area) {
185                            Ok(rect) => {
186                                let split = HorizontalSplit::default()
187                                    .percentages(vec![40, 60])
188                                    .border_style(Style::default().fg(if selected {
189                                        Color::LightBlue
190                                    } else if hover {
191                                        Color::LightRed
192                                    } else {
193                                        Color::Reset
194                                    }));
195                                let chunks = split.inner(rect);
196
197                                frame.render_widget(split, rect);
198
199                                search_settings.text_list.area(chunks[0]);
200
201                                if search_settings.row {
202                                    search_settings
203                                        .text_list
204                                        .selected_style(Style::default().fg(Color::LightRed));
205
206                                    search_settings
207                                        .select_text_list
208                                        .selected_style(Style::default().fg(Color::LightYellow));
209                                } else {
210                                    search_settings
211                                        .text_list
212                                        .selected_style(Style::default().fg(Color::LightYellow));
213
214                                    search_settings
215                                        .select_text_list
216                                        .selected_style(Style::default().fg(Color::LightRed));
217                                }
218
219                                frame.render_widget(search_settings.text_list.clone(), chunks[0]);
220
221                                search_settings.select_text_list.area(chunks[1]);
222
223                                frame.render_widget(
224                                    search_settings.select_text_list.clone(),
225                                    chunks[1],
226                                );
227
228                                frame.render_widget(ForceClear, chunks[0]);
229
230                                frame.render_widget(ForceClear, chunks[1]);
231                            }
232                            Err(rect) => {
233                                frame.render_widget(Clear, rect);
234                            }
235                        }
236                        //panic!("{:?}", rect);
237                    } else {
238                        return true;
239                    }
240                }
241            }
242        }
243
244        false
245    }
246}