src/run.rs
296 lines · 13047 bytes
1use crossterm::event::{self, Event, MouseButton, MouseEventKind};
2use ratatui::{backend::CrosstermBackend, Terminal};
3#[cfg(feature = "mpv")]
4use std::time::{Duration, Instant};
5use std::{
6 any::{Any, TypeId},
7 collections::HashMap,
8 error::Error,
9 io::Stdout,
10};
11use tui_additions::framework::Framework;
12
13use crate::{
14 config::*,
15 global::{functions::*, structs::*},
16 items::*,
17};
18
19/// the main event loop of the program
20pub fn run(
21 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
22 framework: &mut Framework,
23) -> Result<(), Box<dyn Error>> {
24 #[cfg(feature = "mpv")]
25 let tick_rate = Duration::from_secs(1);
26 #[cfg(feature = "mpv")]
27 let mut last_tick = Instant::now();
28 loop {
29 // repeat forever until all tasks are ran (and Tasks is cleared)
30 if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
31 tasks.run(framework, terminal)?;
32 continue;
33 }
34
35 framework
36 .data
37 .global
38 .get_mut::<Status>()
39 .unwrap()
40 .storage
41 .clear();
42
43 // exits the function is `.exit` is true - a way for items/commands to exit the program
44 if framework.data.global.get::<Status>().unwrap().exit {
45 break;
46 }
47
48 #[cfg(feature = "mpv")]
49 if !event::poll(
50 tick_rate
51 .checked_sub(last_tick.elapsed())
52 .unwrap_or_else(|| Duration::from_secs(0)),
53 )? {
54 // do tick changes
55 last_tick = Instant::now();
56 // TaskQueue::render_filter(framework, terminal, |item| (*item).type_id() == TypeId::of::<MessageBar>())?;
57 if MessageBar::is_mpv_render(&framework.split_clean().0) {
58 TaskQueue::render(framework, terminal)?;
59 }
60 continue;
61 }
62
63 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
64
65 let mut updated = false;
66
67 match event::read()? {
68 Event::Mouse(mouse)
69 if framework
70 .data
71 .global
72 .get::<MainConfig>()
73 .unwrap()
74 .mouse_support =>
75 {
76 let scroll_behaviour = framework
77 .data
78 .global
79 .get::<MainConfig>()
80 .unwrap()
81 .textbar_scroll_behaviour;
82 let command_capture = &mut framework
83 .data
84 .global
85 .get_mut::<Status>()
86 .unwrap()
87 .command_capture;
88
89 match mouse.kind {
90 // copied from messagebar.rs::key_event
91 // if modify this, also modify it there
92 MouseEventKind::ScrollUp if command_capture.is_some() => {
93 updated = updated
94 || match scroll_behaviour {
95 TextbarScrollBehaviour::Character => {
96 command_capture.as_mut().unwrap().left().is_ok()
97 }
98 TextbarScrollBehaviour::History => {
99 let status = framework.data.global.get::<Status>().unwrap();
100 let history =
101 framework.data.global.get::<CommandHistory>().unwrap();
102 if status.command_history_index != Some(0)
103 && !history.0.is_empty()
104 {
105 let new_index =
106 status.command_history_index.unwrap_or(history.0.len())
107 - 1;
108 let new_content = history.0[new_index].clone();
109
110 let status =
111 framework.data.global.get_mut::<Status>().unwrap();
112 let text_field = status.command_capture.as_mut().unwrap();
113 if status.command_history_index.is_none() {
114 status.command_editing_cache =
115 text_field.content.clone();
116 }
117 text_field.content = new_content;
118 status.command_history_index = Some(new_index);
119 let _ = text_field.last();
120 true
121 } else {
122 false
123 }
124 }
125 TextbarScrollBehaviour::Word => {
126 previous_word(
127 framework
128 .data
129 .global
130 .get_mut::<Status>()
131 .unwrap()
132 .command_capture
133 .as_mut()
134 .unwrap(),
135 );
136 true
137 }
138 }
139 }
140 MouseEventKind::ScrollUp => {
141 let data: Box<dyn Any> = Box::new("scrollup".to_string());
142 updated = updated
143 || framework.message(HashMap::from([("type".to_string(), data)]))
144 }
145 MouseEventKind::ScrollDown if command_capture.is_some() => {
146 updated = updated
147 || match scroll_behaviour {
148 TextbarScrollBehaviour::Word => {
149 next_word(
150 framework
151 .data
152 .global
153 .get_mut::<Status>()
154 .unwrap()
155 .command_capture
156 .as_mut()
157 .unwrap(),
158 );
159 true
160 }
161 TextbarScrollBehaviour::History => {
162 let status = framework.data.global.get::<Status>().unwrap();
163 if status.command_history_index.is_none() {
164 false
165 } else {
166 let history =
167 framework.data.global.get::<CommandHistory>().unwrap();
168 if status.command_history_index == Some(history.0.len() - 1)
169 {
170 let status =
171 framework.data.global.get_mut::<Status>().unwrap();
172 let text_field =
173 status.command_capture.as_mut().unwrap();
174 status.command_history_index = None;
175 text_field.content =
176 status.command_editing_cache.clone();
177 text_field.cursor =
178 text_field.cursor.min(text_field.content.len());
179 if !text_field.content.is_empty() {
180 let _ = text_field.last();
181 }
182 } else {
183 let new_index = status
184 .command_history_index
185 .unwrap_or(history.0.len())
186 + 1;
187 let new_content = history.0[new_index].clone();
188
189 let status =
190 framework.data.global.get_mut::<Status>().unwrap();
191 let text_field =
192 status.command_capture.as_mut().unwrap();
193 text_field.content = new_content;
194 status.command_history_index = Some(new_index);
195 let _ = text_field.last();
196 }
197 true
198 }
199 }
200 TextbarScrollBehaviour::Character => {
201 command_capture.as_mut().unwrap().right().is_ok()
202 }
203 }
204 }
205 MouseEventKind::ScrollDown => {
206 let data: Box<dyn Any> = Box::new("scrolldown".to_string());
207 updated = updated
208 || framework.message(HashMap::from([("type".to_string(), data)]))
209 }
210 _ => {}
211 }
212
213 if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
214 if updated {
215 framework
216 .data
217 .state
218 .get_mut::<Tasks>()
219 .unwrap()
220 .priority
221 .push(Task::RenderAll);
222 }
223
224 continue;
225 }
226
227 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
228
229 let status = framework.data.global.get_mut::<Status>().unwrap();
230 status.command_capture = None;
231
232 // check if the search filter popup is clicked
233 let mut searchfilter_clicked = false;
234 if status.search_filter_opened {
235 let (mut frameworkclean, state) = framework.split_clean();
236 for row in state.0.iter_mut() {
237 for item in row.items.iter_mut() {
238 if (*item.item).type_id() == TypeId::of::<SearchFilter>()
239 && item.item.mouse_event(
240 &mut frameworkclean,
241 0,
242 0,
243 mouse.column,
244 mouse.row,
245 )
246 {
247 searchfilter_clicked = true;
248 break;
249 }
250 }
251
252 if searchfilter_clicked {
253 break;
254 }
255 }
256 }
257
258 updated = updated
259 || if searchfilter_clicked {
260 true
261 } else {
262 framework.mouse_event(mouse.column, mouse.row)
263 };
264
265 if updated {
266 framework
267 .data
268 .state
269 .get_mut::<Tasks>()
270 .unwrap()
271 .priority
272 .push(Task::RenderAll);
273 }
274 }
275 Event::Key(key) => key_input(key, framework, terminal),
276 // always render if there is a screen resize event
277 Event::Resize(_, _) => {
278 framework
279 .data
280 .state
281 .get_mut::<Tasks>()
282 .unwrap()
283 .priority
284 .push(Task::RenderAll);
285 framework
286 .data
287 .global
288 .get_mut::<Status>()
289 .unwrap()
290 .render_image = true;
291 }
292 _ => {}
293 }
294 }
295 Ok(())
296}