-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathcustom_tree_vec.rs
More file actions
331 lines (278 loc) · 9.76 KB
/
Copy pathcustom_tree_vec.rs
File metadata and controls
331 lines (278 loc) · 9.76 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
mod common {
pub mod image;
pub mod text;
}
use common::image::{image_measure_function, ImageContext};
use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
use taffy::util::print_tree;
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, round_layout, Cache, CacheTree,
};
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
enum NodeKind {
Flexbox,
Grid,
Text,
Image,
}
struct Node {
kind: NodeKind,
style: Style,
text_data: Option<TextContext>,
image_data: Option<ImageContext>,
cache: Cache,
unrounded_layout: Layout,
final_layout: Layout,
children: Vec<usize>,
hoisted_children: Vec<NodeId>,
}
impl Default for Node {
fn default() -> Self {
Node {
kind: NodeKind::Flexbox,
style: Style::default(),
text_data: None,
image_data: None,
cache: Cache::new(),
unrounded_layout: Layout::with_order(0),
final_layout: Layout::with_order(0),
children: Vec::new(),
hoisted_children: Vec::new(),
}
}
}
#[allow(dead_code)]
impl Node {
pub fn new_row(style: Style) -> Node {
Node {
kind: NodeKind::Flexbox,
style: Style { display: Display::Flex, flex_direction: FlexDirection::Row, ..style },
..Node::default()
}
}
pub fn new_column(style: Style) -> Node {
Node {
kind: NodeKind::Flexbox,
style: Style { display: Display::Flex, flex_direction: FlexDirection::Column, ..style },
..Node::default()
}
}
pub fn new_grid(style: Style) -> Node {
Node { kind: NodeKind::Grid, style: Style { display: Display::Grid, ..style }, ..Node::default() }
}
pub fn new_text(style: Style, text_data: TextContext) -> Node {
Node { kind: NodeKind::Text, style, text_data: Some(text_data), ..Node::default() }
}
pub fn new_image(style: Style, image_data: ImageContext) -> Node {
Node { kind: NodeKind::Image, style, image_data: Some(image_data), ..Node::default() }
}
}
struct Tree {
nodes: Vec<Node>,
}
impl Tree {
pub fn new() -> Tree {
Tree { nodes: Vec::new() }
}
pub fn add_node(&mut self, node: Node) -> usize {
self.nodes.push(node);
self.nodes.len() - 1
}
pub fn append_child(&mut self, parent: usize, child: usize) {
self.nodes[parent].children.push(child);
}
#[inline(always)]
fn node_from_id(&self, node_id: NodeId) -> &Node {
&self.nodes[usize::from(node_id)]
}
#[inline(always)]
fn node_from_id_mut(&mut self, node_id: NodeId) -> &mut Node {
&mut self.nodes[usize::from(node_id)]
}
pub fn compute_layout(&mut self, root: usize, available_space: Size<AvailableSpace>, use_rounding: bool) {
compute_root_layout(self, NodeId::from(root), available_space);
if use_rounding {
round_layout(self, NodeId::from(root))
}
}
pub fn print_tree(&mut self, root: usize) {
print_tree(self, NodeId::from(root));
}
}
struct ChildIter<'a>(std::slice::Iter<'a, usize>);
impl Iterator for ChildIter<'_> {
type Item = NodeId;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().copied().map(NodeId::from)
}
}
impl taffy::TraversePartialTree for Tree {
type ChildIter<'a> = ChildIter<'a>;
fn child_ids(&self, node_id: NodeId) -> Self::ChildIter<'_> {
ChildIter(self.node_from_id(node_id).children.iter())
}
fn child_count(&self, node_id: NodeId) -> usize {
self.node_from_id(node_id).children.len()
}
fn get_child_id(&self, node_id: NodeId, index: usize) -> NodeId {
NodeId::from(self.node_from_id(node_id).children[index])
}
}
impl taffy::TraverseTree for Tree {}
impl taffy::LayoutPartialTree for Tree {
type CustomIdent = String;
type CoreContainerStyle<'a>
= &'a Style
where
Self: 'a;
fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
}
fn set_unrounded_layout(&mut self, node_id: NodeId, layout: &Layout) {
self.node_from_id_mut(node_id).unrounded_layout = *layout;
}
fn resolve_calc_value(&self, _val: *const (), _basis: f32) -> f32 {
0.0
}
fn compute_child_layout(&mut self, node_id: NodeId, inputs: taffy::tree::LayoutInput) -> taffy::tree::LayoutOutput {
compute_cached_layout(self, node_id, inputs, |tree, node_id, inputs| {
let node = &mut tree.nodes[usize::from(node_id)];
let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
match node.kind {
NodeKind::Flexbox => compute_flexbox_layout(tree, node_id, inputs),
NodeKind::Grid => compute_grid_layout(tree, node_id, inputs),
NodeKind::Text => compute_leaf_layout(
inputs,
&node.style,
|_val, _basis| 0.0,
|known_dimensions, available_space| {
text_measure_function(
known_dimensions,
available_space,
node.text_data.as_ref().unwrap(),
&font_metrics,
)
},
),
NodeKind::Image => compute_leaf_layout(
inputs,
&node.style,
|_val, _basis| 0.0,
|known_dimensions, _available_space| {
image_measure_function(known_dimensions, node.image_data.as_ref().unwrap())
},
),
}
})
}
}
impl taffy::LayoutContainingBlock for Tree {
type OofItemStyle<'a>
= &'a Style
where
Self: 'a;
fn get_oof_item_style(&self, node_id: NodeId) -> Self::OofItemStyle<'_> {
&self.node_from_id(node_id).style
}
fn set_hoisted_children(&mut self, node_id: NodeId, hoisted: &[NodeId]) {
let vec = &mut self.node_from_id_mut(node_id).hoisted_children;
vec.clear();
vec.extend_from_slice(hoisted);
}
fn add_hoisted_children(&mut self, node_id: NodeId, hoisted: &[NodeId]) {
self.node_from_id_mut(node_id).hoisted_children.extend_from_slice(hoisted);
}
}
impl CacheTree for Tree {
fn cache_get(&mut self, node_id: NodeId, inputs: &taffy::LayoutInput) -> Option<taffy::LayoutOutput> {
self.node_from_id_mut(node_id).cache.get(inputs)
}
fn cache_store(&mut self, node_id: NodeId, inputs: &taffy::LayoutInput, layout_output: taffy::LayoutOutput) {
self.node_from_id_mut(node_id).cache.store(inputs, layout_output)
}
fn cache_clear(&mut self, node_id: NodeId) {
self.node_from_id_mut(node_id).cache.clear();
}
}
impl taffy::LayoutFlexboxContainer for Tree {
type FlexboxContainerStyle<'a>
= &'a Style
where
Self: 'a;
type FlexboxItemStyle<'a>
= &'a Style
where
Self: 'a;
fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
&self.node_from_id(node_id).style
}
fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}
impl taffy::LayoutGridContainer for Tree {
type GridContainerStyle<'a>
= &'a Style
where
Self: 'a;
type GridItemStyle<'a>
= &'a Style
where
Self: 'a;
fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
&self.node_from_id(node_id).style
}
fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}
impl taffy::RoundTree for Tree {
fn get_unrounded_layout(&self, node_id: NodeId) -> Layout {
self.node_from_id(node_id).unrounded_layout
}
fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
self.node_from_id_mut(node_id).final_layout = *layout;
}
fn is_hoisted(&self, node_id: NodeId) -> bool {
self.node_from_id(node_id).style.position.is_out_of_flow()
}
fn hoisted_child_count(&self, node_id: NodeId) -> usize {
self.node_from_id(node_id).hoisted_children.len()
}
fn get_hoisted_child_id(&self, node_id: NodeId, index: usize) -> NodeId {
self.node_from_id(node_id).hoisted_children[index]
}
}
impl taffy::PrintTree for Tree {
fn get_debug_label(&self, node_id: NodeId) -> &'static str {
match self.node_from_id(node_id).kind {
NodeKind::Flexbox => "FLEX",
NodeKind::Grid => "GRID",
NodeKind::Text => "TEXT",
NodeKind::Image => "IMAGE",
}
}
fn get_final_layout(&self, node_id: NodeId) -> Layout {
self.node_from_id(node_id).final_layout
}
}
fn main() -> Result<(), taffy::TaffyError> {
let mut tree = Tree::new();
let root = Node::new_column(Style::DEFAULT);
let root_id = tree.add_node(root);
let text_node = Node::new_text(
Style::default(),
TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal },
);
let text_node_id = tree.add_node(text_node);
tree.append_child(root_id, text_node_id);
let image_node = Node::new_image(Style::default(), ImageContext { width: 400.0, height: 300.0 });
let image_node_id = tree.add_node(image_node);
tree.append_child(root_id, image_node_id);
// Compute layout and print result
tree.compute_layout(root_id, Size::MAX_CONTENT, true);
tree.print_tree(root_id);
Ok(())
}