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
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use super::algorithm::{Algorithm, Flexbox, Node};
use crate::geometry::{Dimension, Point, Size};
use crate::styles::ViewStyle;
#[derive(Clone)]
pub enum MeasureFunc {
Boxed(Arc<dyn Fn(Size<Dimension<f32>>) -> Size<f32> + Send + Sync>),
}
impl std::fmt::Debug for MeasureFunc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("MeasureFunc").finish()
}
}
pub struct Layout {
pub origin: Point<f32>,
pub size: Size<f32>,
}
pub struct LayoutTree {
flexbox: Flexbox,
parents: HashMap<Node, Node>,
roots: Vec<Node>,
}
impl LayoutTree {
pub fn new() -> LayoutTree {
LayoutTree {
flexbox: Flexbox::new(),
parents: HashMap::new(),
roots: vec![],
}
}
pub fn flexbox(&self) -> &Flexbox {
&self.flexbox
}
pub fn flexbox_mut(&mut self) -> &mut Flexbox {
&mut self.flexbox
}
pub fn roots(&self) -> &[Node] {
self.roots.as_slice()
}
pub fn roots_mut(&mut self) -> &mut Vec<Node> {
&mut self.roots
}
pub fn add_child(&mut self, parent: Node, child: Node) {
self.parents.insert(child, parent);
self.flexbox.add_child(parent, child);
}
pub fn remove(&mut self, node: Node) {
if let Some(parent) = self.parents.remove(&node) {
self.flexbox.remove_child(parent, node);
}
assert_eq!(self.flexbox.child_count(node), 0);
self.flexbox.remove(node);
}
pub fn recompute_roots(&mut self) {
for node in self.roots().to_owned() {
let size = self.flexbox().layout(node).size;
self.flexbox_mut().compute_layout(
node,
Size {
width: Dimension::Points(size.width),
height: Dimension::Points(size.height),
},
);
}
}
}
#[derive(Clone)]
pub struct LayoutNode {
layouter: Arc<RwLock<LayoutTree>>,
node: Node,
}
impl LayoutNode {
pub fn new(layouter: Arc<RwLock<LayoutTree>>) -> LayoutNode {
let node = layouter
.write()
.unwrap()
.flexbox_mut()
.new_node(Default::default(), &[]);
LayoutNode { layouter, node }
}
pub fn leaf(layouter: Arc<RwLock<LayoutTree>>) -> LayoutNode {
let node = layouter.write().unwrap().flexbox_mut().new_leaf(
Default::default(),
MeasureFunc::Boxed(Arc::new(|_| Size {
width: 0.0,
height: 0.0,
})),
);
LayoutNode { layouter, node }
}
pub fn layouter(&self) -> &Arc<RwLock<LayoutTree>> {
&self.layouter
}
pub fn node(&self) -> Node {
self.node
}
pub fn set_measure(&self, measure: MeasureFunc) {
self.layouter
.write()
.unwrap()
.flexbox_mut()
.set_measure(self.node, measure);
}
pub fn set_style(&self, style: ViewStyle) {
self.layouter
.write()
.unwrap()
.flexbox_mut()
.set_style(self.node, style)
}
pub fn compute(&mut self, size: Option<(f32, f32)>) {
let size = match size {
Some((width, height)) => Size {
width: Dimension::Points(width),
height: Dimension::Points(height),
},
None => Size {
width: Dimension::Undefined,
height: Dimension::Undefined,
},
};
self.layouter
.write()
.unwrap()
.flexbox_mut()
.compute_layout(self.node, size);
}
pub fn current(&self) -> Layout {
self.layouter.read().unwrap().flexbox().layout(self.node)
}
}