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
// Copyright (c) 2022-2025 R3BL LLC. Licensed under Apache License, Version 2.0.
use crate::{Component, DEBUG_TUI_MOD, DialogEngine, DialogEngineApi,
DialogEngineApplyResponse, DialogEngineArgs, DialogEngineConfigOptions,
EditorEngineConfig, EventPropagation, FlexBox, FlexBoxId, GlobalData,
HasDialogBuffers, HasFocus, InputEvent, OnDialogEditorChangeFn,
OnDialogPressFn, RenderPipeline, SurfaceBounds,
common::{CommonError, CommonErrorType, CommonResult}};
use std::fmt::Debug;
/// This is a shim which allows the reusable [`DialogEngine`] to be used in the context of
/// [Component]. The main methods here simply pass thru all their arguments to the
/// [`DialogEngine`].
#[derive(Debug, Default)]
pub struct DialogComponent<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
pub data: DialogComponentData<S, AS>,
}
#[derive(Debug, Default)]
pub struct DialogComponentData<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
pub id: FlexBoxId,
pub dialog_engine: DialogEngine,
/// Make sure to dispatch actions to handle the user's dialog choice
/// [`crate::DialogChoice`].
pub on_dialog_press_handler: Option<OnDialogPressFn<S, AS>>,
/// Make sure to dispatch an action to update the dialog buffer's editor buffer.
pub on_dialog_editor_change_handler: Option<OnDialogEditorChangeFn<S, AS>>,
_phantom: std::marker::PhantomData<AS>,
}
impl<S, AS> Component<S, AS> for DialogComponent<S, AS>
where
S: Debug + Default + Clone + Sync + Send + HasDialogBuffers,
AS: Debug + Default + Clone + Sync + Send,
{
fn reset(&mut self) { self.data.dialog_engine.reset(); }
fn get_id(&self) -> FlexBoxId { self.data.id }
/// This shim simply calls
/// [DialogEngineApi::render_engine](DialogEngineApi::render_engine) w/ all the
/// necessary arguments:
/// - Global scope: [`GlobalData`] containing the app's state.
/// - Has focus: [`HasFocus`] containing whether the current box has focus.
/// - Surface bounds: [`SurfaceBounds`] containing the bounds of the current box.
///
/// Note:
/// 1. The 3rd argument `_current_box` [`FlexBox`] is ignored since the dialog
/// component breaks out of whatever box the layout places it in, and ends up
/// painting itself over the entire screen.
/// 2. However, [`SurfaceBounds`] is saved for later use. And it is used to restrict
/// where the dialog can be placed on the screen.
fn render(
&mut self,
global_data: &mut GlobalData<S, AS>,
_current_box: FlexBox, /* Ignore this. */
surface_bounds: SurfaceBounds, /* Save this. */
has_focus: &mut HasFocus,
) -> CommonResult<RenderPipeline> {
// Unpack the global data.
let GlobalData { state, .. } = global_data;
// Unpack the component data.
let DialogComponentData {
id, dialog_engine, ..
} = &mut self.data;
let self_id = *id;
dialog_engine.maybe_surface_bounds = Some(surface_bounds);
match state.get_mut_dialog_buffer(self_id) {
Some(_) => {
let args = {
DialogEngineArgs {
self_id,
global_data,
engine: dialog_engine,
has_focus,
}
};
DialogEngineApi::render_engine(args)
}
None => Ok(RenderPipeline::default()),
}
}
/// This shim simply calls
/// [DialogEngineApi::apply_event](DialogEngineApi::apply_event) w/ all the necessary
/// arguments:
/// - Global scope: [`GlobalData`] containing the app's state.
/// - User input (from [`crate::main_event_loop`]): [`InputEvent`].
/// - Has focus: [`HasFocus`] containing whether the current box has focus.
///
/// Usually a component must have focus in order for the [`crate::App`] to
/// [`route_event_to_focused_component`](crate::ComponentRegistry::route_event_to_focused_component)
/// in the first place.
fn handle_event(
&mut self,
global_data: &mut GlobalData<S, AS>,
input_event: InputEvent,
has_focus: &mut HasFocus,
) -> CommonResult<EventPropagation> {
// Unpack the global data.
let GlobalData {
state,
main_thread_channel_sender,
..
} = global_data;
let DialogComponentData {
id,
dialog_engine,
on_dialog_press_handler,
on_dialog_editor_change_handler,
..
} = &mut self.data;
let id = *id;
if state.get_mut_dialog_buffer(id).is_some() {
use DialogEngineApplyResponse::{DialogChoice, Noop,
SelectScrollResultsPanel, UpdateEditorBuffer};
match DialogEngineApi::apply_event::<S, AS>(
state,
id,
dialog_engine,
input_event,
)? {
// Handler user's choice.
DialogChoice(dialog_choice) => {
has_focus.reset_modal_id();
DEBUG_TUI_MOD.then(|| {
// % is Display, ? is Debug.
tracing::debug!(
message = "🐝 restore focus to non modal",
has_focus = ?has_focus
);
});
// Run the handler (if any) w/ `dialog_choice`.
if let Some(fun) = &on_dialog_press_handler {
fun(
dialog_choice,
state,
&mut main_thread_channel_sender.clone(),
);
}
// Trigger re-render, now that focus has been restored to
// non-modal component.
Ok(EventPropagation::ConsumedRender)
}
// Handler user input that has updated the
// dialog_buffer.editor_buffer.
UpdateEditorBuffer => {
// Run the handler (if any) w/ `new_editor_buffer`.
if let Some(it) = &on_dialog_editor_change_handler {
it(state, &mut main_thread_channel_sender.clone());
}
// The handler should dispatch action to change state since
// dialog_buffer.editor_buffer is updated.
Ok(EventPropagation::ConsumedRender)
}
// Handle user input that has updated the results panel.
SelectScrollResultsPanel => Ok(EventPropagation::ConsumedRender),
// All else.
Noop => Ok(EventPropagation::Propagate),
}
} else {
let msg = format!(
"🐝 DialogComponent::handle_event: dialog_buffer is None for id: {id:?}"
);
CommonError::new_error_result(CommonErrorType::NotFound, &msg)
}
}
}
impl<S, AS> DialogComponent<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
/// The `on_dialog_press_handler` is a lambda that is called if the user presses enter
/// or escape. Typically this results in a Redux action being created and then
/// dispatched to the given store.
pub fn new(
id: FlexBoxId,
dialog_options: DialogEngineConfigOptions,
editor_options: EditorEngineConfig,
on_dialog_press_handler: OnDialogPressFn<S, AS>,
on_dialog_editor_change_handler: OnDialogEditorChangeFn<S, AS>,
) -> Self {
let dialog_engine = DialogEngine::new(dialog_options, editor_options);
Self {
data: DialogComponentData {
id,
dialog_engine,
on_dialog_press_handler: Some(on_dialog_press_handler),
on_dialog_editor_change_handler: Some(on_dialog_editor_change_handler),
..Default::default()
},
}
}
pub fn new_boxed(
id: FlexBoxId,
dialog_options: DialogEngineConfigOptions,
editor_options: EditorEngineConfig,
on_dialog_press_handler: OnDialogPressFn<S, AS>,
on_dialog_editor_change_handler: OnDialogEditorChangeFn<S, AS>,
) -> Box<Self> {
let it = DialogComponent::new(
id,
dialog_options,
editor_options,
on_dialog_press_handler,
on_dialog_editor_change_handler,
);
Box::new(it)
}
}