1//! UI [`Node`] types and related data structures.
2//!
3//! Layouts are composed of multiple nodes, which live in a tree-like data structure.
4use slotmap::{DefaultKey, SlotMap, SparseSecondaryMap};
5
6/// A node in a layout.
7pub type Node = slotmap::DefaultKey;
8
9use crate::error::{TaffyError, TaffyResult};
10use crate::geometry::Size;
11use crate::layout::{Cache, Layout};
12use crate::prelude::LayoutTree;
13use crate::style::{AvailableSpace, Style};
14#[cfg(any(feature = "std", feature = "alloc"))]
15use crate::sys::Box;
16use crate::sys::{new_vec_with_capacity, ChildrenVec, Vec};
17use crate::{data::NodeData, error};
18
19/// A function type that can be used in a [`MeasureFunc`]
20///
21/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
22pub trait Measurable: Send + Sync + Fn(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32> {}
23impl<F: Send + Sync + Fn(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32>> Measurable for F {}
24
25/// A function that can be used to compute the intrinsic size of a node
26pub enum MeasureFunc {
27 /// Stores an unboxed function
28 Raw(fn(Size<Option<f32>>, Size<AvailableSpace>) -> Size<f32>),
29
30 /// Stores a boxed function
31 #[cfg(any(feature = "std", feature = "alloc"))]
32 Boxed(Box<dyn Measurable>),
33}
34
35/// A tree of UI [`Nodes`](`Node`), suitable for UI layout
36pub struct Taffy {
37 /// The [`NodeData`] for each node stored in this tree
38 pub(crate) nodes: SlotMap<Node, NodeData>,
39
40 /// The mapping from the Size<AvailableSpace> (in real units) to Size<f32> (in points) for this node
41 pub(crate) measure_funcs: SparseSecondaryMap<Node, MeasureFunc>,
42
43 /// The children of each node
44 ///
45 /// The indexes in the outer vector correspond to the position of the parent [`NodeData`]
46 pub(crate) children: SlotMap<Node, ChildrenVec<Node>>,
47
48 /// The parents of each node
49 ///
50 /// The indexes in the outer vector correspond to the position of the child [`NodeData`]
51 pub(crate) parents: SlotMap<Node, Option<Node>>,
52}
53
54impl Default for Taffy {
55 fn default() -> Self {
56 Taffy::new()
57 }
58}
59
60impl LayoutTree for Taffy {
61 type ChildIter<'a> = std::slice::Iter<'a, DefaultKey>;
62
63 fn children(&self, node: Node) -> Self::ChildIter<'_> {
64 self.children[node].iter()
65 }
66
67 fn child_count(&self, node: Node) -> usize {
68 self.children[node].len()
69 }
70
71 fn is_childless(&self, node: Node) -> bool {
72 self.children[node].is_empty()
73 }
74
75 fn parent(&self, node: Node) -> Option<Node> {
76 self.parents.get(node).copied().flatten()
77 }
78
79 fn style(&self, node: Node) -> &Style {
80 &self.nodes[node].style
81 }
82
83 fn layout(&self, node: Node) -> &Layout {
84 &self.nodes[node].layout
85 }
86
87 fn layout_mut(&mut self, node: Node) -> &mut Layout {
88 &mut self.nodes[node].layout
89 }
90
91 #[inline(always)]
92 fn mark_dirty(&mut self, node: Node) -> TaffyResult<()> {
93 self.mark_dirty_internal(node)
94 }
95
96 fn measure_node(
97 &self,
98 node: Node,
99 known_dimensions: Size<Option<f32>>,
100 available_space: Size<AvailableSpace>,
101 ) -> Size<f32> {
102 match &self.measure_funcs[node] {
103 MeasureFunc::Raw(measure) => measure(known_dimensions, available_space),
104
105 #[cfg(any(feature = "std", feature = "alloc"))]
106 MeasureFunc::Boxed(measure) => (measure as &dyn Fn(_, _) -> _)(known_dimensions, available_space),
107 }
108 }
109
110 fn needs_measure(&self, node: Node) -> bool {
111 self.nodes[node].needs_measure && self.measure_funcs.get(node).is_some()
112 }
113
114 fn cache_mut(&mut self, node: Node, index: usize) -> &mut Option<Cache> {
115 &mut self.nodes[node].size_cache[index]
116 }
117
118 fn child(&self, node: Node, id: usize) -> Node {
119 self.children[node][id]
120 }
121}
122
123#[allow(clippy::iter_cloned_collect)] // due to no-std support, we need to use `iter_cloned` instead of `collect`
124impl Taffy {
125 /// Creates a new [`Taffy`]
126 ///
127 /// The default capacity of a [`Taffy`] is 16 nodes.
128 #[must_use]
129 pub fn new() -> Self {
130 Self::with_capacity(16)
131 }
132
133 /// Creates a new [`Taffy`] that can store `capacity` nodes before reallocation
134 #[must_use]
135 pub fn with_capacity(capacity: usize) -> Self {
136 Self {
137 // TODO: make this method const upstream,
138 // so constructors here can be const
139 nodes: SlotMap::with_capacity(capacity),
140 children: SlotMap::with_capacity(capacity),
141 parents: SlotMap::with_capacity(capacity),
142 measure_funcs: SparseSecondaryMap::with_capacity(capacity),
143 }
144 }
145
146 /// Creates and adds a new unattached leaf node to the tree, and returns the [`NodeId`] of the new node
147 pub fn new_leaf(&mut self, layout: Style) -> TaffyResult<Node> {
148 let id = self.nodes.insert(NodeData::new(layout));
149 let _ = self.children.insert(new_vec_with_capacity(0));
150 let _ = self.parents.insert(None);
151
152 Ok(id)
153 }
154
155 /// Creates and adds a new unattached leaf node to the tree, and returns the [`NodeId`] of the new node
156 ///
157 /// Creates and adds a new leaf node with a supplied [`MeasureFunc`]
158 pub fn new_leaf_with_measure(&mut self, layout: Style, measure: MeasureFunc) -> TaffyResult<Node> {
159 let mut data = NodeData::new(layout);
160 data.needs_measure = true;
161
162 let id = self.nodes.insert(data);
163 self.measure_funcs.insert(id, measure);
164
165 let _ = self.children.insert(new_vec_with_capacity(0));
166 let _ = self.parents.insert(None);
167
168 Ok(id)
169 }
170
171 /// Creates and adds a new node, which may have any number of `children`
172 pub fn new_with_children(&mut self, layout: Style, children: &[Node]) -> TaffyResult<Node> {
173 let id = self.nodes.insert(NodeData::new(layout));
174
175 for child in children {
176 self.parents[*child] = Some(id);
177 }
178
179 let _ = self.children.insert(children.iter().copied().collect::<_>());
180 let _ = self.parents.insert(None);
181
182 Ok(id)
183 }
184
185 /// Removes all nodes
186 ///
187 /// All associated [`Id`] will be rendered invalid.
188 pub fn clear(&mut self) {
189 self.nodes.clear();
190 self.children.clear();
191 self.parents.clear();
192 }
193
194 /// Remove a specific [`Node`] from the tree
195 ///
196 /// Its [`Id`] is marked as invalid. Returns the id of the node removed.
197 pub fn remove(&mut self, node: Node) -> TaffyResult<Node> {
198 if let Some(parent) = self.parents[node] {
199 if let Some(children) = self.children.get_mut(parent) {
200 children.retain(|f| *f != node);
201 }
202 }
203
204 let _ = self.children.remove(node);
205 let _ = self.parents.remove(node);
206 let _ = self.nodes.remove(node);
207
208 Ok(node)
209 }
210
211 /// Sets the [`MeasureFunc`] of the associated node
212 pub fn set_measure(&mut self, node: Node, measure: Option<MeasureFunc>) -> TaffyResult<()> {
213 if let Some(measure) = measure {
214 self.nodes[node].needs_measure = true;
215 self.measure_funcs.insert(node, measure);
216 } else {
217 self.nodes[node].needs_measure = false;
218 self.measure_funcs.remove(node);
219 }
220
221 self.mark_dirty_internal(node)?;
222
223 Ok(())
224 }
225
226 /// Adds a `child` [`Node`] under the supplied `parent`
227 pub fn add_child(&mut self, parent: Node, child: Node) -> TaffyResult<()> {
228 self.parents[child] = Some(parent);
229 self.children[parent].push(child);
230 self.mark_dirty_internal(parent)?;
231
232 Ok(())
233 }
234
235 /// Directly sets the `children` of the supplied `parent`
236 pub fn set_children(&mut self, parent: Node, children: &[Node]) -> TaffyResult<()> {
237 // Remove node as parent from all its current children.
238 for child in &self.children[parent] {
239 self.parents[*child] = None;
240 }
241
242 // Build up relation node <-> child
243 for child in children {
244 self.parents[*child] = Some(parent);
245 }
246
247 self.children[parent] = children.iter().copied().collect::<_>();
248
249 self.mark_dirty_internal(parent)?;
250
251 Ok(())
252 }
253
254 /// Removes the `child` of the parent `node`
255 ///
256 /// The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.
257 pub fn remove_child(&mut self, parent: Node, child: Node) -> TaffyResult<Node> {
258 let index = self.children[parent].iter().position(|n| *n == child).unwrap();
259 self.remove_child_at_index(parent, index)
260 }
261
262 /// Removes the child at the given `index` from the `parent`
263 ///
264 /// The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.
265 pub fn remove_child_at_index(&mut self, parent: Node, child_index: usize) -> TaffyResult<Node> {
266 let child_count = self.children[parent].len();
267 if child_index >= child_count {
268 return Err(error::TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
269 }
270
271 let child = self.children[parent].remove(child_index);
272 self.parents[child] = None;
273
274 self.mark_dirty_internal(parent)?;
275
276 Ok(child)
277 }
278
279 /// Replaces the child at the given `child_index` from the `parent` node with the new `child` node
280 ///
281 /// The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.
282 pub fn replace_child_at_index(&mut self, parent: Node, child_index: usize, new_child: Node) -> TaffyResult<Node> {
283 let child_count = self.children[parent].len();
284 if child_index >= child_count {
285 return Err(error::TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
286 }
287
288 self.parents[new_child] = Some(parent);
289 let old_child = core::mem::replace(&mut self.children[parent][child_index], new_child);
290 self.parents[old_child] = None;
291
292 self.mark_dirty_internal(parent)?;
293
294 Ok(old_child)
295 }
296
297 /// Returns the child [`Node`] of the parent `node` at the provided `child_index`
298 pub fn child_at_index(&self, parent: Node, child_index: usize) -> TaffyResult<Node> {
299 let child_count = self.children[parent].len();
300 if child_index >= child_count {
301 return Err(error::TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
302 }
303
304 Ok(self.children[parent][child_index])
305 }
306
307 /// Returns the number of children of the `parent` [`Node`]
308 pub fn child_count(&self, parent: Node) -> TaffyResult<usize> {
309 Ok(self.children[parent].len())
310 }
311
312 /// Returns a list of children that belong to the [`Parent`]
313 pub fn children(&self, parent: Node) -> TaffyResult<Vec<Node>> {
314 Ok(self.children[parent].iter().copied().collect::<_>())
315 }
316
317 /// Sets the [`Style`] of the provided `node`
318 pub fn set_style(&mut self, node: Node, style: Style) -> TaffyResult<()> {
319 self.nodes[node].style = style;
320 self.mark_dirty_internal(node)?;
321 Ok(())
322 }
323
324 /// Gets the [`Style`] of the provided `node`
325 pub fn style(&self, node: Node) -> TaffyResult<&Style> {
326 Ok(&self.nodes[node].style)
327 }
328
329 /// Return this node layout relative to its parent
330 pub fn layout(&self, node: Node) -> TaffyResult<&Layout> {
331 Ok(&self.nodes[node].layout)
332 }
333
334 /// Marks the layout computation of this node and its children as outdated
335 ///
336 /// Performs a recursive depth-first search up the tree until the root node is reached
337 ///
338 /// WARNING: this will stack-overflow if the tree contains a cycle
339 fn mark_dirty_internal(&mut self, node: Node) -> TaffyResult<()> {
340 /// WARNING: this will stack-overflow if the tree contains a cycle
341 fn mark_dirty_recursive(
342 nodes: &mut SlotMap<Node, NodeData>,
343 parents: &SlotMap<Node, Option<Node>>,
344 node_id: Node,
345 ) {
346 nodes[node_id].mark_dirty();
347
348 if let Some(Some(node)) = parents.get(node_id) {
349 mark_dirty_recursive(nodes, parents, *node);
350 }
351 }
352
353 mark_dirty_recursive(&mut self.nodes, &self.parents, node);
354
355 Ok(())
356 }
357
358 /// Indicates whether the layout of this node (and its children) need to be recomputed
359 pub fn dirty(&self, node: Node) -> TaffyResult<bool> {
360 Ok(self.nodes[node].size_cache.iter().all(|entry| entry.is_none()))
361 }
362
363 /// Updates the stored layout of the provided `node` and its children
364 pub fn compute_layout(&mut self, node: Node, available_space: Size<AvailableSpace>) -> Result<(), TaffyError> {
365 crate::compute::compute_layout(self, node, available_space)
366 }
367}
368
369#[cfg(test)]
370mod tests {
371 #![allow(clippy::bool_assert_comparison)]
372
373 use super::*;
374 use crate::style::{Dimension, Display, FlexDirection};
375 use crate::style_helpers::*;
376 use crate::sys;
377
378 #[test]
379 fn new_should_allocate_default_capacity() {
380 const DEFAULT_CAPACITY: usize = 16; // This is the capacity defined in the `impl Default`
381 let taffy = Taffy::new();
382
383 assert!(taffy.children.capacity() >= DEFAULT_CAPACITY);
384 assert!(taffy.parents.capacity() >= DEFAULT_CAPACITY);
385 assert!(taffy.nodes.capacity() >= DEFAULT_CAPACITY);
386 }
387
388 #[test]
389 fn test_with_capacity() {
390 const CAPACITY: usize = 8;
391 let taffy = Taffy::with_capacity(CAPACITY);
392
393 assert!(taffy.children.capacity() >= CAPACITY);
394 assert!(taffy.parents.capacity() >= CAPACITY);
395 assert!(taffy.nodes.capacity() >= CAPACITY);
396 }
397
398 #[test]
399 fn test_new_leaf() {
400 let mut taffy = Taffy::new();
401
402 let res = taffy.new_leaf(Style::default());
403 assert!(res.is_ok());
404 let node = res.unwrap();
405
406 // node should be in the taffy tree and have no children
407 assert!(taffy.child_count(node).unwrap() == 0);
408 }
409
410 #[test]
411 fn new_leaf_with_measure() {
412 let mut taffy = Taffy::new();
413
414 let res = taffy.new_leaf_with_measure(Style::default(), MeasureFunc::Raw(|_, _| Size::ZERO));
415 assert!(res.is_ok());
416 let node = res.unwrap();
417
418 // node should be in the taffy tree and have no children
419 assert!(taffy.child_count(node).unwrap() == 0);
420 }
421
422 /// Test that new_with_children works as expected
423 #[test]
424 fn test_new_with_children() {
425 let mut taffy = Taffy::new();
426 let child0 = taffy.new_leaf(Style::default()).unwrap();
427 let child1 = taffy.new_leaf(Style::default()).unwrap();
428 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
429
430 // node should have two children
431 assert_eq!(taffy.child_count(node).unwrap(), 2);
432 assert_eq!(taffy.children(node).unwrap()[0], child0);
433 assert_eq!(taffy.children(node).unwrap()[1], child1);
434 }
435
436 #[test]
437 fn remove_node_should_remove() {
438 let mut taffy = Taffy::new();
439
440 let node = taffy.new_leaf(Style::default()).unwrap();
441
442 let _ = taffy.remove(node).unwrap();
443 }
444
445 #[test]
446 fn remove_node_should_detach_herarchy() {
447 let mut taffy = Taffy::new();
448
449 // Build a linear tree layout: <0> <- <1> <- <2>
450 let node2 = taffy.new_leaf(Style::default()).unwrap();
451 let node1 = taffy.new_with_children(Style::default(), &[node2]).unwrap();
452 let node0 = taffy.new_with_children(Style::default(), &[node1]).unwrap();
453
454 // Both node0 and node1 should have 1 child nodes
455 assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
456 assert_eq!(taffy.children(node1).unwrap().as_slice(), &[node2]);
457
458 // Disconnect the tree: <0> <2>
459 let _ = taffy.remove(node1).unwrap();
460
461 // Both remaining nodes should have no child nodes
462 assert!(taffy.children(node0).unwrap().is_empty());
463 assert!(taffy.children(node2).unwrap().is_empty());
464 }
465
466 #[test]
467 fn remove_last_node() {
468 let mut taffy = Taffy::new();
469
470 let parent = taffy.new_leaf(Style::default()).unwrap();
471 let child = taffy.new_leaf(Style::default()).unwrap();
472 taffy.add_child(parent, child).unwrap();
473
474 taffy.remove(child).unwrap();
475 taffy.remove(parent).unwrap();
476 }
477
478 #[test]
479 fn set_measure() {
480 let mut taffy = Taffy::new();
481 let node = taffy
482 .new_leaf_with_measure(Style::default(), MeasureFunc::Raw(|_, _| Size { width: 200.0, height: 200.0 }))
483 .unwrap();
484 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
485 assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
486
487 taffy.set_measure(node, Some(MeasureFunc::Raw(|_, _| Size { width: 100.0, height: 100.0 }))).unwrap();
488 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
489 assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
490 }
491
492 #[test]
493 fn set_measure_of_previously_unmeasured_node() {
494 let mut taffy = Taffy::new();
495 let node = taffy.new_leaf(Style::default()).unwrap();
496 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
497 assert_eq!(taffy.layout(node).unwrap().size.width, 0.0);
498
499 taffy.set_measure(node, Some(MeasureFunc::Raw(|_, _| Size { width: 100.0, height: 100.0 }))).unwrap();
500 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
501 assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
502 }
503
504 /// Test that adding `add_child()` works
505 #[test]
506 fn add_child() {
507 let mut taffy = Taffy::new();
508 let node = taffy.new_leaf(Style::default()).unwrap();
509 assert_eq!(taffy.child_count(node).unwrap(), 0);
510
511 let child0 = taffy.new_leaf(Style::default()).unwrap();
512 taffy.add_child(node, child0).unwrap();
513 assert_eq!(taffy.child_count(node).unwrap(), 1);
514
515 let child1 = taffy.new_leaf(Style::default()).unwrap();
516 taffy.add_child(node, child1).unwrap();
517 assert_eq!(taffy.child_count(node).unwrap(), 2);
518 }
519
520 #[test]
521 fn set_children() {
522 let mut taffy = Taffy::new();
523
524 let child0 = taffy.new_leaf(Style::default()).unwrap();
525 let child1 = taffy.new_leaf(Style::default()).unwrap();
526 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
527
528 assert_eq!(taffy.child_count(node).unwrap(), 2);
529 assert_eq!(taffy.children(node).unwrap()[0], child0);
530 assert_eq!(taffy.children(node).unwrap()[1], child1);
531
532 let child2 = taffy.new_leaf(Style::default()).unwrap();
533 let child3 = taffy.new_leaf(Style::default()).unwrap();
534 taffy.set_children(node, &[child2, child3]).unwrap();
535
536 assert_eq!(taffy.child_count(node).unwrap(), 2);
537 assert_eq!(taffy.children(node).unwrap()[0], child2);
538 assert_eq!(taffy.children(node).unwrap()[1], child3);
539 }
540
541 /// Test that removing a child works
542 #[test]
543 fn remove_child() {
544 let mut taffy = Taffy::new();
545 let child0 = taffy.new_leaf(Style::default()).unwrap();
546 let child1 = taffy.new_leaf(Style::default()).unwrap();
547 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
548
549 assert_eq!(taffy.child_count(node).unwrap(), 2);
550
551 taffy.remove_child(node, child0).unwrap();
552 assert_eq!(taffy.child_count(node).unwrap(), 1);
553 assert_eq!(taffy.children(node).unwrap()[0], child1);
554
555 taffy.remove_child(node, child1).unwrap();
556 assert_eq!(taffy.child_count(node).unwrap(), 0);
557 }
558
559 #[test]
560 fn remove_child_at_index() {
561 let mut taffy = Taffy::new();
562 let child0 = taffy.new_leaf(Style::default()).unwrap();
563 let child1 = taffy.new_leaf(Style::default()).unwrap();
564 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
565
566 assert_eq!(taffy.child_count(node).unwrap(), 2);
567
568 taffy.remove_child_at_index(node, 0).unwrap();
569 assert_eq!(taffy.child_count(node).unwrap(), 1);
570 assert_eq!(taffy.children(node).unwrap()[0], child1);
571
572 taffy.remove_child_at_index(node, 0).unwrap();
573 assert_eq!(taffy.child_count(node).unwrap(), 0);
574 }
575
576 #[test]
577 fn replace_child_at_index() {
578 let mut taffy = Taffy::new();
579
580 let child0 = taffy.new_leaf(Style::default()).unwrap();
581 let child1 = taffy.new_leaf(Style::default()).unwrap();
582
583 let node = taffy.new_with_children(Style::default(), &[child0]).unwrap();
584 assert_eq!(taffy.child_count(node).unwrap(), 1);
585 assert_eq!(taffy.children(node).unwrap()[0], child0);
586
587 taffy.replace_child_at_index(node, 0, child1).unwrap();
588 assert_eq!(taffy.child_count(node).unwrap(), 1);
589 assert_eq!(taffy.children(node).unwrap()[0], child1);
590 }
591 #[test]
592 fn test_child_at_index() {
593 let mut taffy = Taffy::new();
594 let child0 = taffy.new_leaf(Style::default()).unwrap();
595 let child1 = taffy.new_leaf(Style::default()).unwrap();
596 let child2 = taffy.new_leaf(Style::default()).unwrap();
597 let node = taffy.new_with_children(Style::default(), &[child0, child1, child2]).unwrap();
598
599 assert!(if let Ok(result) = taffy.child_at_index(node, 0) { result == child0 } else { false });
600 assert!(if let Ok(result) = taffy.child_at_index(node, 1) { result == child1 } else { false });
601 assert!(if let Ok(result) = taffy.child_at_index(node, 2) { result == child2 } else { false });
602 }
603 #[test]
604 fn test_child_count() {
605 let mut taffy = Taffy::new();
606 let child0 = taffy.new_leaf(Style::default()).unwrap();
607 let child1 = taffy.new_leaf(Style::default()).unwrap();
608 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
609
610 assert!(if let Ok(count) = taffy.child_count(node) { count == 2 } else { false });
611 assert!(if let Ok(count) = taffy.child_count(child0) { count == 0 } else { false });
612 assert!(if let Ok(count) = taffy.child_count(child1) { count == 0 } else { false });
613 }
614
615 #[allow(clippy::vec_init_then_push)]
616 #[test]
617 fn test_children() {
618 let mut taffy = Taffy::new();
619 let child0 = taffy.new_leaf(Style::default()).unwrap();
620 let child1 = taffy.new_leaf(Style::default()).unwrap();
621 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
622
623 let mut children: sys::Vec<Node> = sys::Vec::new();
624 children.push(child0);
625 children.push(child1);
626
627 let children_result = taffy.children(node).unwrap();
628 assert_eq!(children_result, children);
629
630 assert!(taffy.children(child0).unwrap().is_empty());
631 }
632 #[test]
633 fn test_set_style() {
634 let mut taffy = Taffy::new();
635
636 let node = taffy.new_leaf(Style::default()).unwrap();
637 assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
638
639 taffy.set_style(node, Style { display: Display::None, ..Style::default() }).unwrap();
640 assert_eq!(taffy.style(node).unwrap().display, Display::None);
641 }
642 #[test]
643 fn test_style() {
644 let mut taffy = Taffy::new();
645
646 let style = Style { display: Display::None, flex_direction: FlexDirection::RowReverse, ..Default::default() };
647
648 let node = taffy.new_leaf(style.clone()).unwrap();
649
650 let res = taffy.style(node);
651 assert!(res.is_ok());
652 assert!(res.unwrap() == &style);
653 }
654 #[test]
655 fn test_layout() {
656 let mut taffy = Taffy::new();
657 let node = taffy.new_leaf(Style::default()).unwrap();
658
659 // TODO: Improve this test?
660 let res = taffy.layout(node);
661 assert!(res.is_ok());
662 }
663
664 #[test]
665 fn test_mark_dirty() {
666 let mut taffy = Taffy::new();
667 let child0 = taffy.new_leaf(Style::default()).unwrap();
668 let child1 = taffy.new_leaf(Style::default()).unwrap();
669 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
670
671 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
672
673 assert_eq!(taffy.dirty(child0).unwrap(), false);
674 assert_eq!(taffy.dirty(child1).unwrap(), false);
675 assert_eq!(taffy.dirty(node).unwrap(), false);
676
677 taffy.mark_dirty(node).unwrap();
678 assert_eq!(taffy.dirty(child0).unwrap(), false);
679 assert_eq!(taffy.dirty(child1).unwrap(), false);
680 assert_eq!(taffy.dirty(node).unwrap(), true);
681
682 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
683 taffy.mark_dirty(child0).unwrap();
684 assert_eq!(taffy.dirty(child0).unwrap(), true);
685 assert_eq!(taffy.dirty(child1).unwrap(), false);
686 assert_eq!(taffy.dirty(node).unwrap(), true);
687 }
688
689 #[test]
690 fn compute_layout_should_produce_valid_result() {
691 let mut taffy = Taffy::new();
692 let node_result = taffy.new_leaf(Style {
693 size: Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
694 ..Default::default()
695 });
696 assert!(node_result.is_ok());
697 let node = node_result.unwrap();
698 let layout_result = taffy.compute_layout(
699 node,
700 Size { width: AvailableSpace::Definite(100.), height: AvailableSpace::Definite(100.) },
701 );
702 assert!(layout_result.is_ok());
703 }
704
705 #[test]
706 fn measure_func_is_send_and_sync() {
707 fn is_send_and_sync<T: Send + Sync>() {}
708 is_send_and_sync::<MeasureFunc>();
709 }
710}