src/run.rs
291 lines · 12685 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 } else {
178 let new_index = status
179 .command_history_index
180 .unwrap_or(history.0.len())
181 + 1;
182 let new_content = history.0[new_index].clone();
183
184 let status =
185 framework.data.global.get_mut::<Status>().unwrap();
186 let text_field =
187 status.command_capture.as_mut().unwrap();
188 text_field.content = new_content;
189 status.command_history_index = Some(new_index);
190 let _ = text_field.last();
191 }
192 true
193 }
194 }
195 TextbarScrollBehaviour::Character => {
196 command_capture.as_mut().unwrap().right().is_ok()
197 }
198 }
199 }
200 MouseEventKind::ScrollDown => {
201 let data: Box<dyn Any> = Box::new("scrolldown".to_string());
202 updated = updated
203 || framework.message(HashMap::from([("type".to_string(), data)]))
204 }
205 _ => {}
206 }
207
208 if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
209 if updated {
210 framework
211 .data
212 .state
213 .get_mut::<Tasks>()
214 .unwrap()
215 .priority
216 .push(Task::RenderAll);
217 }
218
219 continue;
220 }
221
222 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
223
224 let status = framework.data.global.get_mut::<Status>().unwrap();
225 status.command_capture = None;
226
227 // check if the search filter popup is clicked
228 let mut searchfilter_clicked = false;
229 if status.search_filter_opened {
230 let (mut frameworkclean, state) = framework.split_clean();
231 for row in state.0.iter_mut() {
232 for item in row.items.iter_mut() {
233 if (*item.item).type_id() == TypeId::of::<SearchFilter>()
234 && item.item.mouse_event(
235 &mut frameworkclean,
236 0,
237 0,
238 mouse.column,
239 mouse.row,
240 )
241 {
242 searchfilter_clicked = true;
243 break;
244 }
245 }
246
247 if searchfilter_clicked {
248 break;
249 }
250 }
251 }
252
253 updated = updated
254 || if searchfilter_clicked {
255 true
256 } else {
257 framework.mouse_event(mouse.column, mouse.row)
258 };
259
260 if updated {
261 framework
262 .data
263 .state
264 .get_mut::<Tasks>()
265 .unwrap()
266 .priority
267 .push(Task::RenderAll);
268 }
269 }
270 Event::Key(key) => key_input(key, framework, terminal),
271 // always render if there is a screen resize event
272 Event::Resize(_, _) => {
273 framework
274 .data
275 .state
276 .get_mut::<Tasks>()
277 .unwrap()
278 .priority
279 .push(Task::RenderAll);
280 framework
281 .data
282 .global
283 .get_mut::<Status>()
284 .unwrap()
285 .render_image = true;
286 }
287 _ => {}
288 }
289 }
290 Ok(())
291}