src/run.rs
141 lines · 4770 bytes
1use crossterm::event::{self, Event, MouseButton, MouseEventKind};
2use ratatui::{backend::CrosstermBackend, Terminal};
3#[cfg(feature = "mpv")]
4use std::time::{Duration, Instant};
5use std::{any::TypeId, error::Error, io::Stdout};
6use tui_additions::framework::Framework;
7
8use crate::{
9 config::*,
10 global::{functions::*, structs::*},
11 items::*,
12};
13
14/// the main event loop of the program
15pub fn run(
16 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
17 framework: &mut Framework,
18) -> Result<(), Box<dyn Error>> {
19 #[cfg(feature = "mpv")]
20 let tick_rate = Duration::from_secs(1);
21 #[cfg(feature = "mpv")]
22 let mut last_tick = Instant::now();
23 loop {
24 // repeat forever until all tasks are ran (and Tasks is cleared)
25 if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() {
26 tasks.run(framework, terminal)?;
27 continue;
28 }
29
30 framework
31 .data
32 .global
33 .get_mut::<Status>()
34 .unwrap()
35 .storage
36 .clear();
37
38 // exits the function is `.exit` is true - a way for items/commands to exit the program
39 if framework.data.global.get::<Status>().unwrap().exit {
40 break;
41 }
42
43 #[cfg(feature = "mpv")]
44 if !event::poll(
45 tick_rate
46 .checked_sub(last_tick.elapsed())
47 .unwrap_or_else(|| Duration::from_secs(0)),
48 )? {
49 // do tick changes
50 last_tick = Instant::now();
51 // TaskQueue::render_filter(framework, terminal, |item| (*item).type_id() == TypeId::of::<MessageBar>())?;
52 if MessageBar::is_mpv_render(&framework.split_clean().0) {
53 TaskQueue::render(framework, terminal)?;
54 }
55 continue;
56 }
57
58 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
59
60 match event::read()? {
61 Event::Mouse(mouse)
62 if framework
63 .data
64 .global
65 .get::<MainConfig>()
66 .unwrap()
67 .mouse_support =>
68 {
69 if mouse.kind != MouseEventKind::Down(MouseButton::Left) {
70 continue;
71 }
72
73 *framework.data.global.get_mut::<Message>().unwrap() = Message::None;
74
75 let status = framework.data.global.get_mut::<Status>().unwrap();
76 status.command_capture = None;
77
78 // check if the search filter popup is clicked
79 let mut searchfilter_clicked = false;
80 if status.search_filter_opened {
81 let (mut frameworkclean, state) = framework.split_clean();
82 for row in state.0.iter_mut() {
83 for item in row.items.iter_mut() {
84 if item.item.type_id() == TypeId::of::<SearchFilter>()
85 && item.item.mouse_event(
86 &mut frameworkclean,
87 0,
88 0,
89 mouse.column,
90 mouse.row,
91 )
92 {
93 searchfilter_clicked = true;
94 break;
95 }
96 }
97
98 if searchfilter_clicked {
99 break;
100 }
101 }
102 }
103
104 let updated = if searchfilter_clicked {
105 true
106 } else {
107 framework.mouse_event(mouse.column, mouse.row)
108 };
109
110 if updated {
111 framework
112 .data
113 .state
114 .get_mut::<Tasks>()
115 .unwrap()
116 .priority
117 .push(Task::RenderAll);
118 }
119 }
120 Event::Key(key) => key_input(key, framework, terminal),
121 // always render if there is a screen resize event
122 Event::Resize(_, _) => {
123 framework
124 .data
125 .state
126 .get_mut::<Tasks>()
127 .unwrap()
128 .priority
129 .push(Task::RenderAll);
130 framework
131 .data
132 .global
133 .get_mut::<Status>()
134 .unwrap()
135 .render_image = true;
136 }
137 _ => {}
138 }
139 }
140 Ok(())
141}