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
use crate::{
Component,
EventHandled,
Node,
Path,
UiEvent,
};
macro_rules! basic_impl_component {
($type:ty) => {
impl Component for $type {
fn render(&mut self) -> Node {
Node::Text(self.to_string())
}
}
};
}
basic_impl_component!(&str);
impl Component for String {
fn render(&mut self) -> Node {
Node::Text(self.clone())
}
}
impl Component for &String {
fn render(&mut self) -> Node {
Node::Text((*self).to_string())
}
}
impl<T> Component for Vec<T>
where
T: Component,
{
fn render(&mut self) -> Node {
let nodes = self.iter_mut().map(|i| i.render()).collect::<Vec<Node>>();
Node::Vec(nodes)
}
fn handle_ui_event(&mut self, event: &UiEvent, path: &Path) -> EventHandled {
if let Some((first, rest)) = path.split_first() {
if let Some(target_node) = self.get_mut(*first) {
target_node.handle_ui_event(event, rest)
} else {
false
}
} else {
false
}
}
}
impl<T> Component for Option<T>
where
T: Component,
{
fn render(&mut self) -> Node {
match self {
Some(t) => t.render(),
None => Node::Comment(None),
}
}
fn handle_ui_event(&mut self, event: &UiEvent, path: &Path) -> EventHandled {
match self {
Some(t) => t.handle_ui_event(event, path),
None => false,
}
}
}
basic_impl_component!(bool);
basic_impl_component!(char);
basic_impl_component!(i8);
basic_impl_component!(i16);
basic_impl_component!(i32);
basic_impl_component!(i64);
basic_impl_component!(isize);
basic_impl_component!(u8);
basic_impl_component!(u16);
basic_impl_component!(u32);
basic_impl_component!(u64);
basic_impl_component!(usize);
basic_impl_component!(f32);
basic_impl_component!(f64);