-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main_event_loop.rs
316 lines (285 loc) · 9.7 KB
/
main_event_loop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (c) 2022 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::{fmt::{Debug, Display},
sync::Arc};
use async_trait::async_trait;
use tokio::sync::RwLock;
use crate::*;
/// Controls whether input events are process by spawning a new task or by blocking the current task.
const SPAWN_PROCESS_INPUT: bool = true;
/// These are global state values for the entire application:
/// 1. The size holds the width and height of the terminal window.
/// 2. The cursor_position (for purposes of drawing via [RenderOp], [RenderOps], and
/// [RenderPipeline]). This is used for low level painting operations and are not meant to be
/// used by code that renders components.
#[derive(Clone, Debug, Default)]
pub struct TWData {
pub size: Size,
pub cursor_position: Position,
}
impl TWData {
fn try_to_create_instance() -> CommonResult<TWData> {
let mut tw_data = TWData::default();
tw_data.set_size(terminal_lib_operations::lookup_size()?);
Ok(tw_data)
}
pub fn set_size(&mut self, new_size: Size) {
self.size = new_size;
self.dump_state_to_log("main_event_loop -> Resize");
}
pub fn get_size(&self) -> Size { self.size }
pub fn dump_state_to_log(&self, msg: &str) {
call_if_true!(DEBUG_TUI_MOD, log_no_err!(INFO, "{} -> {:?}", msg, self));
}
}
pub struct TerminalWindow;
impl TerminalWindow {
/// The where clause needs to match up w/ the trait bounds for [Store].
///
/// ```ignore
/// where
/// S: Default + Clone + PartialEq + Debug + Sync + Send,
/// A: Default + Clone + Sync + Send,
/// ```
pub async fn main_event_loop<S, A>(
store: Store<S, A>, shared_app: SharedApp<S, A>, exit_keys: Vec<InputEvent>,
) -> CommonResult<()>
where
S: Display + Default + Clone + PartialEq + Debug + Sync + Send + 'static,
A: Display + Default + Clone + Sync + Send + 'static,
{
// Initialize the terminal window data struct.
let _tw_data = TWData::try_to_create_instance()?;
let shared_tw_data: SharedTWData = Arc::new(RwLock::new(_tw_data));
// Start raw mode.
let my_raw_mode = RawMode::start(&shared_tw_data).await;
// Move the store into an Arc & RwLock.
let shared_store: SharedStore<S, A> = Arc::new(RwLock::new(store));
// Create a subscriber (AppManager) & attach it to the store.
let _subscriber = AppManager::new_box(&shared_app, &shared_store, &shared_tw_data);
shared_store.write().await.add_subscriber(_subscriber).await;
// Create a new event stream (async).
let mut async_event_stream = AsyncEventStream::default();
// Perform first render.
AppManager::render_app(&shared_store, &shared_app, &shared_tw_data, None).await?;
shared_tw_data
.read()
.await
.dump_state_to_log("main_event_loop -> Startup 🚀");
// Main event loop.
loop {
// Try and get the next event if available (asynchronously).
let maybe_input_event = async_event_stream.try_to_get_input_event().await;
// Process the input_event.
let input_event = match maybe_input_event {
Some(it) => it,
_ => continue,
};
call_if_true!(
DEBUG_TUI_MOD,
log_no_err!(INFO, "main_event_loop -> Tick: ⏰ {}", input_event)
);
let propagation_result_from_app = TerminalWindow::process_input_event(
&shared_tw_data,
&shared_store,
&shared_app,
&input_event,
)
.await?;
// If event not consumed by app, propagate to the default input handler.
match propagation_result_from_app {
EventPropagation::ConsumedRerender => {
AppManager::render_app(&shared_store, &shared_app, &shared_tw_data, None).await?;
}
EventPropagation::Propagate => {
let continuation_result_from_default_handler =
DefaultInputEventHandler::no_consume(input_event, &exit_keys).await;
match continuation_result_from_default_handler {
Continuation::Exit => {
break;
}
Continuation::ResizeAndContinue(new_size) => {
shared_tw_data.write().await.set_size(new_size);
AppManager::render_app(&shared_store, &shared_app, &shared_tw_data, None).await?;
}
_ => {}
};
}
EventPropagation::Consumed => {}
}
}
// End raw mode.
my_raw_mode.end(&shared_tw_data).await;
Ok(())
}
async fn process_input_event<S, A>(
shared_tw_data: &SharedTWData, shared_store: &SharedStore<S, A>, shared_app: &SharedApp<S, A>,
input_event: &InputEvent,
) -> CommonResult<EventPropagation>
where
S: Display + Default + Clone + PartialEq + Debug + Sync + Send + 'static,
A: Display + Default + Clone + Sync + Send + 'static,
{
let propagation_result_from_app = match SPAWN_PROCESS_INPUT {
true => {
// Tokio spawn.
let propagation_result_from_app = {
let shared_tw_data_clone = shared_tw_data.clone();
let shared_store_clone = shared_store.clone();
let shared_app_clone = shared_app.clone();
let input_event_clone = input_event.clone();
let join_handle = tokio::spawn(async move {
AppManager::route_input_to_app(
&shared_tw_data_clone,
&shared_store_clone,
&shared_app_clone,
&input_event_clone,
)
.await
});
join_handle.await??
};
call_if_true!(
DEBUG_TUI_MOD,
log_no_err!(
INFO,
"main_event_loop -> 🚥 SPAWN propagation_result_from_app: {:?}",
propagation_result_from_app
)
);
propagation_result_from_app
}
false => {
// Blocking call.
let propagation_result_from_app =
AppManager::route_input_to_app(shared_tw_data, shared_store, shared_app, input_event)
.await?;
call_if_true!(
DEBUG_TUI_MOD,
log_no_err!(
INFO,
"main_event_loop -> 🚥 NO_SPAWN propagation_result_from_app: {:?}",
propagation_result_from_app
)
);
propagation_result_from_app
}
};
Ok(propagation_result_from_app)
}
}
struct AppManager<S, A>
where
S: Display + Default + Clone + PartialEq + Debug + Sync + Send + 'static,
A: Display + Default + Clone + Sync + Send + 'static,
{
shared_app: SharedApp<S, A>,
shared_store: SharedStore<S, A>,
shared_tw_data: SharedTWData,
}
#[async_trait]
impl<S, A> AsyncSubscriber<S> for AppManager<S, A>
where
S: Display + Default + Clone + PartialEq + Debug + Sync + Send + 'static,
A: Display + Default + Clone + Sync + Send,
{
async fn run(&self, my_state: S) {
let result = AppManager::render_app(
&self.shared_store,
&self.shared_app,
&self.shared_tw_data,
my_state.into(),
)
.await;
if let Err(e) = result {
call_if_true!(
DEBUG_TUI_MOD,
log_no_err!(ERROR, "MySubscriber::run -> Error: {}", e)
)
}
}
}
impl<S, A> AppManager<S, A>
where
S: Display + Default + Clone + PartialEq + Debug + Sync + Send + 'static,
A: Display + Default + Clone + Sync + Send,
{
fn new_box(
shared_app: &SharedApp<S, A>, shared_store: &SharedStore<S, A>, shared_window: &SharedTWData,
) -> Box<Self> {
Box::new(AppManager {
shared_app: shared_app.clone(),
shared_store: shared_store.clone(),
shared_tw_data: shared_window.clone(),
})
}
/// Pass the event to the `shared_app` for further processing.
pub async fn route_input_to_app(
shared_window: &SharedTWData, shared_store: &SharedStore<S, A>, shared_app: &SharedApp<S, A>,
input_event: &InputEvent,
) -> CommonResult<EventPropagation> {
throws_with_return!({
let latest_state = shared_store.read().await.get_state();
let window_size = shared_window.read().await.get_size();
shared_app
.write()
.await
.app_handle_event(input_event, &latest_state, shared_store, window_size)
.await?
});
}
pub async fn render_app(
shared_store: &SharedStore<S, A>, shared_app: &SharedApp<S, A>, shared_tw_data: &SharedTWData,
maybe_state: Option<S>,
) -> CommonResult<()> {
throws!({
let state: S = if maybe_state.is_none() {
shared_store.read().await.get_state()
} else {
maybe_state.unwrap()
};
let render_result = shared_app
.write()
.await
.app_render(&state, shared_store, shared_tw_data)
.await;
match render_result {
Err(error) => {
RenderOp::default().flush();
call_if_true!(
DEBUG_TUI_MOD,
log_no_err!(ERROR, "MySubscriber::render() error ❌: {}", error)
);
}
Ok(render_pipeline) => {
render_pipeline
.paint(FlushKind::ClearBeforeFlush, shared_tw_data)
.await;
let window_size = shared_tw_data.read().await.get_size();
call_if_true!(DEBUG_TUI_MOD, {
log_no_err!(
INFO,
"🎨 MySubscriber::paint() ok ✅: \n size: {:?}\n state: {:?}\n",
window_size,
state,
);
});
}
}
});
}
}