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