src/items/searchbar.rs
198 lines · 6520 bytes
1use std::error::Error;
2
3use crate::{config::*, global::structs::*};
4
5#[cfg(feature = "clipboard")]
6use crate::global::functions::get_clipboard;
7use crate::global::functions::{next_word, previous_word, remove_word};
8
9use crossterm::event::KeyCode;
10use ratatui::{
11 layout::Alignment,
12 style::Style,
13 widgets::{Block, Borders},
14};
15use tui_additions::{framework::FrameworkItem, widgets::TextField};
16
17/// the search bar item
18#[derive(Clone, Default)]
19pub struct SearchBar {
20 pub text_field: TextField,
21}
22
23impl FrameworkItem for SearchBar {
24 // basically is a TextField with borders
25 fn render(
26 &mut self,
27 frame: &mut ratatui::Frame<ratatui::backend::CrosstermBackend<std::io::Stdout>>,
28 framework: &mut tui_additions::framework::FrameworkClean,
29 area: ratatui::layout::Rect,
30 popup_render: bool,
31 info: tui_additions::framework::ItemInfo,
32 ) {
33 if popup_render {
34 return;
35 }
36
37 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
38
39 let block = Block::default()
40 .title("Search YouTube")
41 .title_alignment(Alignment::Center)
42 .borders(Borders::ALL)
43 .border_type(appearance.borders)
44 .border_style(Style::default().fg(if info.hover {
45 appearance.colors.outline_hover
46 } else if info.selected {
47 appearance.colors.outline_selected
48 } else {
49 appearance.colors.outline
50 }));
51
52 let inner = block.inner(area);
53
54 frame.render_widget(block, area);
55
56 self.text_field.set_width(inner.width);
57
58 frame.render_widget(self.text_field.clone(), inner);
59 }
60
61 // sets text field content to the curreny search query, thats it
62 // kinda pointless if it doesnt break anything gonna remove it
63 fn load_item(
64 &mut self,
65 framework: &mut tui_additions::framework::FrameworkClean,
66 _info: tui_additions::framework::ItemInfo,
67 ) -> Result<(), Box<dyn Error>> {
68 let search = framework.data.state.get::<Search>().unwrap().query.clone();
69 self.text_field.content = search;
70 let _ = self.text_field.last();
71
72 Ok(())
73 }
74
75 fn key_event(
76 &mut self,
77 framework: &mut tui_additions::framework::FrameworkClean,
78 key: crossterm::event::KeyEvent,
79 _info: tui_additions::framework::ItemInfo,
80 ) -> Result<(), Box<dyn Error>> {
81 let mut render = true;
82 match framework
83 .data
84 .global
85 .get::<KeyBindingsConfig>()
86 .unwrap()
87 .get(key)
88 {
89 #[cfg(feature = "clipboard")]
90 Some(KeyAction::Paste) => {
91 let content = get_clipboard();
92 if content.is_empty() {
93 *framework.data.global.get_mut::<Message>().unwrap() =
94 Message::Error(String::from("Clipboard empty"));
95 }
96
97 // push all characters at cursor location
98 content.chars().for_each(|c| {
99 let _ = self.text_field.push(c);
100 });
101 }
102 Some(KeyAction::RemoveWord) => remove_word(&mut self.text_field),
103 Some(KeyAction::PreviousWord) => previous_word(&mut self.text_field),
104 Some(KeyAction::NextWord) => next_word(&mut self.text_field),
105 Some(KeyAction::ClearLine) => {
106 self.text_field.content.clear();
107 self.text_field.scroll = 0;
108 self.text_field.cursor = 0;
109 }
110 Some(KeyAction::First | KeyAction::MoveUp) => self.text_field.cursor = 0,
111 Some(KeyAction::End | KeyAction::MoveDown) => {
112 self.text_field.cursor = self.text_field.content.len()
113 }
114 _ => render = false,
115 }
116
117 if render {
118 framework
119 .data
120 .state
121 .get_mut::<Tasks>()
122 .unwrap()
123 .priority
124 .push(Task::RenderAll);
125 return Ok(());
126 }
127 // pasting clipboard content
128
129 let updated = match key.code {
130 KeyCode::Backspace => self.text_field.remove(self.text_field.cursor).is_ok(),
131 KeyCode::Char(c) => self.text_field.push(c).is_ok(),
132 KeyCode::Up => self.text_field.first().is_ok(),
133 KeyCode::Down => self.text_field.last().is_ok(),
134 KeyCode::Right => self.text_field.right().is_ok(),
135 KeyCode::Left => self.text_field.left().is_ok(),
136 KeyCode::Enter => {
137 if self.text_field.content.is_empty() {
138 *framework.data.global.get_mut::<Message>().unwrap() =
139 Message::Message(String::from("Search string must not be empty"));
140 framework
141 .data
142 .state
143 .get_mut::<Tasks>()
144 .unwrap()
145 .priority
146 .push(Task::RenderAll);
147 return Ok(());
148 }
149
150 let mut search = framework.data.state.get_mut::<Search>().unwrap();
151 search.query = self.text_field.content.clone();
152 let search = search.clone();
153 let tasks = framework.data.state.get_mut::<Tasks>().unwrap();
154 tasks.priority.push(Task::LoadPage(Page::Search(search)));
155 false
156 }
157 _ => false,
158 };
159
160 // only re-render screen if updated
161 if updated {
162 let tasks = framework.data.state.get_mut::<Tasks>().unwrap();
163 tasks.priority.push(Task::RenderAll);
164 }
165
166 Ok(())
167 }
168
169 fn mouse_event(
170 &mut self,
171 _framework: &mut tui_additions::framework::FrameworkClean,
172 mut x: u16,
173 y: u16,
174 _absolute_x: u16,
175 _absolute_y: u16,
176 ) -> bool {
177 if y != 1 || x == 0 {
178 return false;
179 }
180
181 x -= 1; // there is 1 character to the left of the text field
182
183 if x == self.text_field.cursor as u16 {
184 return false;
185 }
186
187 if x > self.text_field.content.len() as u16 {
188 return self.text_field.last().is_ok();
189 }
190
191 self.text_field.cursor = x as usize;
192 self.text_field.update().is_ok()
193 }
194
195 fn selectable(&self) -> bool {
196 true
197 }
198}