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
//! Provides the core functionality of rendering to the terminal
//! This has the event loop which calculates and process the events to the target widget
use crate::cmd::Cmd;
use crate::Event;
use crate::{command, find_node, layout, Buffer, Widget};
pub use crossterm::{
cursor,
event::{self, KeyCode, KeyEvent, KeyModifiers, MouseEvent},
execute, queue, style,
style::{Attribute, Attributes, Color, ContentStyle},
terminal::{self, ClearType},
Command, Result,
};
use expanse::{geometry::Size, number::Number};
use std::io::Write;
/// A Dispatch trait which the implementing APP will update
/// its own state based on the supplied msg.
pub trait Dispatch<MSG> {
/// dispatch the msg and passed the root node for the implementing
/// app to access it and change the state of the UI.
fn dispatch(&self, msg: MSG, root_node: &mut dyn Widget<MSG>);
}
/// This provides the render loop of the terminal UI
pub struct Renderer<'a, MSG> {
write: &'a mut dyn Write,
program: Option<&'a dyn Dispatch<MSG>>,
root_node: &'a mut dyn Widget<MSG>,
terminal_size: (u16, u16),
focused_widget_idx: Option<usize>,
}
impl<'a, MSG> Renderer<'a, MSG> {
/// create a new renderer with the supplied root_node
pub fn new(
write: &'a mut dyn Write,
program: Option<&'a dyn Dispatch<MSG>>,
root_node: &'a mut dyn Widget<MSG>,
) -> Self {
let (width, height) =
terminal::size().expect("must get the terminal size");
layout::compute_node_layout(
root_node,
Size {
width: Number::Defined(width as f32),
height: Number::Defined(height as f32),
},
);
Renderer {
write,
program,
root_node,
terminal_size: (width, height),
focused_widget_idx: None,
}
}
fn recompute_layout(&mut self) {
let (width, height) = self.terminal_size;
layout::compute_node_layout(
self.root_node,
Size {
width: Number::Defined(width as f32),
height: Number::Defined(height as f32),
},
);
}
fn dispatch_msg(&mut self, msgs: Vec<MSG>) {
if let Some(program) = self.program {
for msg in msgs {
program.dispatch(msg, self.root_node);
}
}
self.recompute_layout();
}
fn draw_widget(
buf: &mut Buffer,
widget: &dyn Widget<MSG>,
) -> Result<Vec<Cmd>>
where
MSG: 'a,
{
let mut cmds = widget.draw(buf);
if let Some(children) = widget.children() {
for child in children {
let more_cmds = Self::draw_widget(buf, child.as_ref())?;
cmds.extend(more_cmds);
}
}
Ok(cmds)
}
/// run the event loop of the renderer
pub fn run(&mut self) -> Result<()> {
command::init(&mut self.write)?;
command::reset_top(&mut self.write)?;
let (width, height) = self.terminal_size;
loop {
let mut buf = Buffer::new(width as usize, height as usize);
buf.reset();
{
let cmds = Self::draw_widget(&mut buf, self.root_node)?;
buf.render(&mut self.write)?;
cmds.iter().for_each(|cmd| {
cmd.execute(self.write).expect("must execute")
});
}
self.write.flush()?;
if let Ok(c_event) = event::read() {
let event = Event::from_crossterm(c_event);
let location = extract_location(&event);
match event {
Event::Key(key_event) => {
// To quite, press any of the following:
// - CTRL-c
// - CTRL-q
// - CTRL-d
// - CTRL-z
if key_event.modifiers.contains(KeyModifiers::CONTROL) {
match key_event.code {
KeyCode::Char(c) => match c {
'c' | 'q' | 'd' | 'z' => {
break;
}
_ => (),
},
_ => (),
}
} else {
// send the keypresses to the focused widget
if let Some(idx) = self.focused_widget_idx.as_ref()
{
let active_widget: Option<
&mut dyn Widget<MSG>,
> = find_node::find_widget_mut(
self.root_node,
*idx,
);
if let Some(focused_widget) = active_widget {
let msgs = focused_widget
.process_event(event.clone());
self.dispatch_msg(msgs);
}
}
}
}
// mouse clicks sets the focused the widget underneath
Event::Mouse(MouseEvent::Down(_btn, x, y, _modifier)) => {
self.focused_widget_idx = layout::node_hit_at(
self.root_node,
x as f32,
y as f32,
&mut 0,
)
.pop();
if let Some(idx) = self.focused_widget_idx.as_ref() {
find_node::set_focused_node(self.root_node, *idx);
}
}
Event::Resize(width, height) => {
self.terminal_size = (width, height);
self.recompute_layout();
}
_ => (),
}
// any other activities, such as mouse scroll is
// sent the widget underneath the location, regardless
// if it focused or not.
if let Some((x, y)) = extract_location(&event) {
let hits = layout::node_hit_at(
self.root_node,
x as f32,
y as f32,
&mut 0,
);
for hit in hits.iter().rev() {
let mut hit_widget: Option<&mut dyn Widget<MSG>> =
find_node::find_widget_mut(self.root_node, *hit);
if let Some(hit_widget) = &mut hit_widget {
let msgs = hit_widget.process_event(event.clone());
self.dispatch_msg(msgs);
}
}
}
}
}
command::finalize(self.write)?;
Ok(())
}
}
/// extract the x and y location of a mouse event
fn extract_location(event: &Event) -> Option<(u16, u16)> {
match event {
Event::Mouse(MouseEvent::Down(_btn, x, y, _modifier)) => Some((*x, *y)),
Event::Mouse(MouseEvent::Up(_btn, x, y, _modifier)) => Some((*x, *y)),
Event::Mouse(MouseEvent::Drag(_btn, x, y, _modifier)) => Some((*x, *y)),
Event::Mouse(MouseEvent::ScrollDown(x, y, _modifier)) => Some((*x, *y)),
Event::Mouse(MouseEvent::ScrollUp(x, y, _modifier)) => Some((*x, *y)),
Event::Key(_) => None,
Event::Resize(_, _) => None,
Event::InputEvent(_) => None,
}
}