1#[cfg(not(feature = "std"))]
3use slotmap::SecondaryMap;
4#[cfg(feature = "std")]
5use slotmap::SparseSecondaryMap as SecondaryMap;
6use slotmap::{DefaultKey, SlotMap};
7
8use crate::geometry::Size;
9use crate::style::{AvailableSpace, Display, Style};
10use crate::tree::{
11 Cache, Layout, LayoutInput, LayoutOutput, LayoutPartialTree, NodeId, PrintTree, RoundTree, RunMode,
12 TraversePartialTree, TraverseTree,
13};
14use crate::util::debug::{debug_log, debug_log_node};
15use crate::util::sys::{new_vec_with_capacity, ChildrenVec, Vec};
16
17use crate::compute::{
18 compute_cached_layout, compute_hidden_layout, compute_leaf_layout, compute_root_layout, round_layout,
19};
20use crate::CacheTree;
21#[cfg(feature = "block_layout")]
22use crate::{compute::compute_block_layout, LayoutBlockContainer};
23#[cfg(feature = "flexbox")]
24use crate::{compute::compute_flexbox_layout, LayoutFlexboxContainer};
25#[cfg(feature = "grid")]
26use crate::{compute::compute_grid_layout, LayoutGridContainer};
27
28#[cfg(all(feature = "detailed_layout_info", feature = "grid"))]
29use crate::compute::grid::DetailedGridInfo;
30#[cfg(feature = "detailed_layout_info")]
31use crate::tree::layout::DetailedLayoutInfo;
32
33pub type TaffyResult<T> = Result<T, TaffyError>;
35
36#[derive(Debug, Clone, PartialEq, Eq)]
38pub enum TaffyError {
39 ChildIndexOutOfBounds {
41 parent: NodeId,
43 child_index: usize,
45 child_count: usize,
47 },
48 InvalidParentNode(NodeId),
50 InvalidChildNode(NodeId),
52 InvalidInputNode(NodeId),
54}
55
56impl core::fmt::Display for TaffyError {
57 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
58 match self {
59 TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => {
60 write!(f, "Index (is {child_index}) should be < child_count ({child_count}) for parent node {parent:?}")
61 }
62 TaffyError::InvalidParentNode(parent) => {
63 write!(f, "Parent Node {parent:?} is not in the TaffyTree instance")
64 }
65 TaffyError::InvalidChildNode(child) => write!(f, "Child Node {child:?} is not in the TaffyTree instance"),
66 TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {node:?} is not in the TaffyTree instance"),
67 }
68 }
69}
70
71#[cfg(feature = "std")]
72impl std::error::Error for TaffyError {}
73
74#[derive(Debug, Clone, Copy)]
76pub(crate) struct TaffyConfig {
77 pub(crate) use_rounding: bool,
79}
80
81impl Default for TaffyConfig {
82 fn default() -> Self {
83 Self { use_rounding: true }
84 }
85}
86
87#[derive(Debug, Clone, PartialEq)]
91struct NodeData {
92 pub(crate) style: Style,
94
95 pub(crate) unrounded_layout: Layout,
98
99 pub(crate) final_layout: Layout,
102
103 pub(crate) has_context: bool,
105
106 pub(crate) cache: Cache,
108
109 #[cfg(feature = "detailed_layout_info")]
111 pub(crate) detailed_layout_info: DetailedLayoutInfo,
112}
113
114impl NodeData {
115 #[must_use]
117 pub const fn new(style: Style) -> Self {
118 Self {
119 style,
120 cache: Cache::new(),
121 unrounded_layout: Layout::new(),
122 final_layout: Layout::new(),
123 has_context: false,
124 #[cfg(feature = "detailed_layout_info")]
125 detailed_layout_info: DetailedLayoutInfo::None,
126 }
127 }
128
129 #[inline]
133 pub fn mark_dirty(&mut self) {
134 self.cache.clear()
135 }
136}
137
138#[derive(Debug, Clone)]
142pub struct TaffyTree<NodeContext = ()> {
143 nodes: SlotMap<DefaultKey, NodeData>,
145
146 node_context_data: SecondaryMap<DefaultKey, NodeContext>,
148
149 children: SlotMap<DefaultKey, ChildrenVec<NodeId>>,
153
154 parents: SlotMap<DefaultKey, Option<NodeId>>,
158
159 config: TaffyConfig,
161}
162
163impl Default for TaffyTree {
164 fn default() -> TaffyTree<()> {
165 TaffyTree::new()
166 }
167}
168
169pub struct TaffyTreeChildIter<'a>(core::slice::Iter<'a, NodeId>);
171impl Iterator for TaffyTreeChildIter<'_> {
172 type Item = NodeId;
173
174 #[inline]
175 fn next(&mut self) -> Option<Self::Item> {
176 self.0.next().copied()
177 }
178}
179
180impl<NodeContext> TraversePartialTree for TaffyTree<NodeContext> {
182 type ChildIter<'a>
183 = TaffyTreeChildIter<'a>
184 where
185 Self: 'a;
186
187 #[inline(always)]
188 fn child_ids(&self, parent_node_id: NodeId) -> Self::ChildIter<'_> {
189 TaffyTreeChildIter(self.children[parent_node_id.into()].iter())
190 }
191
192 #[inline(always)]
193 fn child_count(&self, parent_node_id: NodeId) -> usize {
194 self.children[parent_node_id.into()].len()
195 }
196
197 #[inline(always)]
198 fn get_child_id(&self, parent_node_id: NodeId, id: usize) -> NodeId {
199 self.children[parent_node_id.into()][id]
200 }
201}
202
203impl<NodeContext> TraverseTree for TaffyTree<NodeContext> {}
205
206impl<NodeContext> CacheTree for TaffyTree<NodeContext> {
208 fn cache_get(
209 &self,
210 node_id: NodeId,
211 known_dimensions: Size<Option<f32>>,
212 available_space: Size<AvailableSpace>,
213 run_mode: RunMode,
214 ) -> Option<LayoutOutput> {
215 self.nodes[node_id.into()].cache.get(known_dimensions, available_space, run_mode)
216 }
217
218 fn cache_store(
219 &mut self,
220 node_id: NodeId,
221 known_dimensions: Size<Option<f32>>,
222 available_space: Size<AvailableSpace>,
223 run_mode: RunMode,
224 layout_output: LayoutOutput,
225 ) {
226 self.nodes[node_id.into()].cache.store(known_dimensions, available_space, run_mode, layout_output)
227 }
228
229 fn cache_clear(&mut self, node_id: NodeId) {
230 self.nodes[node_id.into()].cache.clear()
231 }
232}
233
234impl<NodeContext> PrintTree for TaffyTree<NodeContext> {
236 #[inline(always)]
237 fn get_debug_label(&self, node_id: NodeId) -> &'static str {
238 let node = &self.nodes[node_id.into()];
239 let display = node.style.display;
240 let num_children = self.child_count(node_id);
241
242 match (num_children, display) {
243 (_, Display::None) => "NONE",
244 (0, _) => "LEAF",
245 #[cfg(feature = "block_layout")]
246 (_, Display::Block) => "BLOCK",
247 #[cfg(feature = "flexbox")]
248 (_, Display::Flex) => {
249 use crate::FlexDirection;
250 match node.style.flex_direction {
251 FlexDirection::Row | FlexDirection::RowReverse => "FLEX ROW",
252 FlexDirection::Column | FlexDirection::ColumnReverse => "FLEX COL",
253 }
254 }
255 #[cfg(feature = "grid")]
256 (_, Display::Grid) => "GRID",
257 }
258 }
259
260 #[inline(always)]
261 fn get_final_layout(&self, node_id: NodeId) -> &Layout {
262 if self.config.use_rounding {
263 &self.nodes[node_id.into()].final_layout
264 } else {
265 &self.nodes[node_id.into()].unrounded_layout
266 }
267 }
268}
269
270pub(crate) struct TaffyView<'t, NodeContext, MeasureFunction>
274where
275 MeasureFunction:
276 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
277{
278 pub(crate) taffy: &'t mut TaffyTree<NodeContext>,
280 pub(crate) measure_function: MeasureFunction,
282}
283
284impl<NodeContext, MeasureFunction> TraversePartialTree for TaffyView<'_, NodeContext, MeasureFunction>
286where
287 MeasureFunction:
288 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
289{
290 type ChildIter<'a>
291 = TaffyTreeChildIter<'a>
292 where
293 Self: 'a;
294
295 #[inline(always)]
296 fn child_ids(&self, parent_node_id: NodeId) -> Self::ChildIter<'_> {
297 self.taffy.child_ids(parent_node_id)
298 }
299
300 #[inline(always)]
301 fn child_count(&self, parent_node_id: NodeId) -> usize {
302 self.taffy.child_count(parent_node_id)
303 }
304
305 #[inline(always)]
306 fn get_child_id(&self, parent_node_id: NodeId, child_index: usize) -> NodeId {
307 self.taffy.get_child_id(parent_node_id, child_index)
308 }
309}
310
311impl<NodeContext, MeasureFunction> TraverseTree for TaffyView<'_, NodeContext, MeasureFunction> where
313 MeasureFunction:
314 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>
315{
316}
317
318impl<NodeContext, MeasureFunction> LayoutPartialTree for TaffyView<'_, NodeContext, MeasureFunction>
320where
321 MeasureFunction:
322 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
323{
324 type CoreContainerStyle<'a>
325 = &'a Style
326 where
327 Self: 'a;
328
329 #[inline(always)]
330 fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
331 &self.taffy.nodes[node_id.into()].style
332 }
333
334 #[inline(always)]
335 fn set_unrounded_layout(&mut self, node_id: NodeId, layout: &Layout) {
336 self.taffy.nodes[node_id.into()].unrounded_layout = *layout;
337 }
338
339 #[inline(always)]
340 fn compute_child_layout(&mut self, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
341 if inputs.run_mode == RunMode::PerformHiddenLayout {
344 debug_log!("HIDDEN");
345 return compute_hidden_layout(self, node);
346 }
347
348 compute_cached_layout(self, node, inputs, |tree, node, inputs| {
354 let display_mode = tree.taffy.nodes[node.into()].style.display;
355 let has_children = tree.child_count(node) > 0;
356
357 debug_log!(display_mode);
358 debug_log_node!(
359 inputs.known_dimensions,
360 inputs.parent_size,
361 inputs.available_space,
362 inputs.run_mode,
363 inputs.sizing_mode
364 );
365
366 match (display_mode, has_children) {
368 (Display::None, _) => compute_hidden_layout(tree, node),
369 #[cfg(feature = "block_layout")]
370 (Display::Block, true) => compute_block_layout(tree, node, inputs),
371 #[cfg(feature = "flexbox")]
372 (Display::Flex, true) => compute_flexbox_layout(tree, node, inputs),
373 #[cfg(feature = "grid")]
374 (Display::Grid, true) => compute_grid_layout(tree, node, inputs),
375 (_, false) => {
376 let node_key = node.into();
377 let style = &tree.taffy.nodes[node_key].style;
378 let has_context = tree.taffy.nodes[node_key].has_context;
379 let node_context = has_context.then(|| tree.taffy.node_context_data.get_mut(node_key)).flatten();
380 let measure_function = |known_dimensions, available_space| {
381 (tree.measure_function)(known_dimensions, available_space, node, node_context, style)
382 };
383 compute_leaf_layout(inputs, style, measure_function)
384 }
385 }
386 })
387 }
388}
389
390impl<NodeContext, MeasureFunction> CacheTree for TaffyView<'_, NodeContext, MeasureFunction>
391where
392 MeasureFunction:
393 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
394{
395 fn cache_get(
396 &self,
397 node_id: NodeId,
398 known_dimensions: Size<Option<f32>>,
399 available_space: Size<AvailableSpace>,
400 run_mode: RunMode,
401 ) -> Option<LayoutOutput> {
402 self.taffy.nodes[node_id.into()].cache.get(known_dimensions, available_space, run_mode)
403 }
404
405 fn cache_store(
406 &mut self,
407 node_id: NodeId,
408 known_dimensions: Size<Option<f32>>,
409 available_space: Size<AvailableSpace>,
410 run_mode: RunMode,
411 layout_output: LayoutOutput,
412 ) {
413 self.taffy.nodes[node_id.into()].cache.store(known_dimensions, available_space, run_mode, layout_output)
414 }
415
416 fn cache_clear(&mut self, node_id: NodeId) {
417 self.taffy.nodes[node_id.into()].cache.clear()
418 }
419}
420
421#[cfg(feature = "block_layout")]
422impl<NodeContext, MeasureFunction> LayoutBlockContainer for TaffyView<'_, NodeContext, MeasureFunction>
423where
424 MeasureFunction:
425 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
426{
427 type BlockContainerStyle<'a>
428 = &'a Style
429 where
430 Self: 'a;
431 type BlockItemStyle<'a>
432 = &'a Style
433 where
434 Self: 'a;
435
436 #[inline(always)]
437 fn get_block_container_style(&self, node_id: NodeId) -> Self::BlockContainerStyle<'_> {
438 self.get_core_container_style(node_id)
439 }
440
441 #[inline(always)]
442 fn get_block_child_style(&self, child_node_id: NodeId) -> Self::BlockItemStyle<'_> {
443 self.get_core_container_style(child_node_id)
444 }
445}
446
447#[cfg(feature = "flexbox")]
448impl<NodeContext, MeasureFunction> LayoutFlexboxContainer for TaffyView<'_, NodeContext, MeasureFunction>
449where
450 MeasureFunction:
451 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
452{
453 type FlexboxContainerStyle<'a>
454 = &'a Style
455 where
456 Self: 'a;
457 type FlexboxItemStyle<'a>
458 = &'a Style
459 where
460 Self: 'a;
461
462 #[inline(always)]
463 fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
464 &self.taffy.nodes[node_id.into()].style
465 }
466
467 #[inline(always)]
468 fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
469 &self.taffy.nodes[child_node_id.into()].style
470 }
471}
472
473#[cfg(feature = "grid")]
474impl<NodeContext, MeasureFunction> LayoutGridContainer for TaffyView<'_, NodeContext, MeasureFunction>
475where
476 MeasureFunction:
477 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
478{
479 type GridContainerStyle<'a>
480 = &'a Style
481 where
482 Self: 'a;
483 type GridItemStyle<'a>
484 = &'a Style
485 where
486 Self: 'a;
487
488 #[inline(always)]
489 fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
490 &self.taffy.nodes[node_id.into()].style
491 }
492
493 #[inline(always)]
494 fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
495 &self.taffy.nodes[child_node_id.into()].style
496 }
497
498 #[inline(always)]
499 #[cfg(feature = "detailed_layout_info")]
500 fn set_detailed_grid_info(&mut self, node_id: NodeId, detailed_grid_info: DetailedGridInfo) {
501 self.taffy.nodes[node_id.into()].detailed_layout_info = DetailedLayoutInfo::Grid(Box::new(detailed_grid_info));
502 }
503}
504
505impl<NodeContext, MeasureFunction> RoundTree for TaffyView<'_, NodeContext, MeasureFunction>
507where
508 MeasureFunction:
509 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
510{
511 #[inline(always)]
512 fn get_unrounded_layout(&self, node: NodeId) -> &Layout {
513 &self.taffy.nodes[node.into()].unrounded_layout
514 }
515
516 #[inline(always)]
517 fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
518 self.taffy.nodes[node_id.into()].final_layout = *layout;
519 }
520}
521
522#[allow(clippy::iter_cloned_collect)] impl<NodeContext> TaffyTree<NodeContext> {
524 #[must_use]
528 pub fn new() -> Self {
529 Self::with_capacity(16)
530 }
531
532 #[must_use]
534 pub fn with_capacity(capacity: usize) -> Self {
535 TaffyTree {
536 nodes: SlotMap::with_capacity(capacity),
539 children: SlotMap::with_capacity(capacity),
540 parents: SlotMap::with_capacity(capacity),
541 node_context_data: SecondaryMap::with_capacity(capacity),
542 config: TaffyConfig::default(),
543 }
544 }
545
546 pub fn enable_rounding(&mut self) {
548 self.config.use_rounding = true;
549 }
550
551 pub fn disable_rounding(&mut self) {
553 self.config.use_rounding = false;
554 }
555
556 pub fn new_leaf(&mut self, layout: Style) -> TaffyResult<NodeId> {
558 let id = self.nodes.insert(NodeData::new(layout));
559 let _ = self.children.insert(new_vec_with_capacity(0));
560 let _ = self.parents.insert(None);
561
562 Ok(id.into())
563 }
564
565 pub fn new_leaf_with_context(&mut self, layout: Style, context: NodeContext) -> TaffyResult<NodeId> {
569 let mut data = NodeData::new(layout);
570 data.has_context = true;
571
572 let id = self.nodes.insert(data);
573 self.node_context_data.insert(id, context);
574
575 let _ = self.children.insert(new_vec_with_capacity(0));
576 let _ = self.parents.insert(None);
577
578 Ok(id.into())
579 }
580
581 pub fn new_with_children(&mut self, layout: Style, children: &[NodeId]) -> TaffyResult<NodeId> {
583 let id = NodeId::from(self.nodes.insert(NodeData::new(layout)));
584
585 for child in children {
586 self.parents[(*child).into()] = Some(id);
587 }
588
589 let _ = self.children.insert(children.iter().copied().collect::<_>());
590 let _ = self.parents.insert(None);
591
592 Ok(id)
593 }
594
595 pub fn clear(&mut self) {
597 self.nodes.clear();
598 self.children.clear();
599 self.parents.clear();
600 }
601
602 pub fn remove(&mut self, node: NodeId) -> TaffyResult<NodeId> {
606 let key = node.into();
607 if let Some(parent) = self.parents[key] {
608 if let Some(children) = self.children.get_mut(parent.into()) {
609 children.retain(|f| *f != node);
610 }
611 }
612
613 if let Some(children) = self.children.get(key) {
615 for child in children.iter().copied() {
616 self.parents[child.into()] = None;
617 }
618 }
619
620 let _ = self.children.remove(key);
621 let _ = self.parents.remove(key);
622 let _ = self.nodes.remove(key);
623
624 Ok(node)
625 }
626
627 #[inline]
629 pub fn set_node_context(&mut self, node: NodeId, measure: Option<NodeContext>) -> TaffyResult<()> {
630 let key = node.into();
631 if let Some(measure) = measure {
632 self.nodes[key].has_context = true;
633 self.node_context_data.insert(key, measure);
634 } else {
635 self.nodes[key].has_context = false;
636 self.node_context_data.remove(key);
637 }
638
639 self.mark_dirty(node)?;
640
641 Ok(())
642 }
643
644 #[inline]
646 pub fn get_node_context(&self, node: NodeId) -> Option<&NodeContext> {
647 self.node_context_data.get(node.into())
648 }
649
650 #[inline]
652 pub fn get_node_context_mut(&mut self, node: NodeId) -> Option<&mut NodeContext> {
653 self.node_context_data.get_mut(node.into())
654 }
655
656 pub fn get_disjoint_node_context_mut<const N: usize>(
658 &mut self,
659 keys: [NodeId; N],
660 ) -> Option<[&mut NodeContext; N]> {
661 self.node_context_data.get_disjoint_mut(keys.map(|k| k.into()))
662 }
663
664 pub fn add_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<()> {
666 let parent_key = parent.into();
667 let child_key = child.into();
668 self.parents[child_key] = Some(parent);
669 self.children[parent_key].push(child);
670 self.mark_dirty(parent)?;
671
672 Ok(())
673 }
674
675 pub fn insert_child_at_index(&mut self, parent: NodeId, child_index: usize, child: NodeId) -> TaffyResult<()> {
677 let parent_key = parent.into();
678
679 let child_count = self.children[parent_key].len();
680 if child_index > child_count {
681 return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
682 }
683
684 self.parents[child.into()] = Some(parent);
685 self.children[parent_key].insert(child_index, child);
686 self.mark_dirty(parent)?;
687
688 Ok(())
689 }
690
691 pub fn set_children(&mut self, parent: NodeId, children: &[NodeId]) -> TaffyResult<()> {
693 let parent_key = parent.into();
694
695 for child in &self.children[parent_key] {
697 self.parents[(*child).into()] = None;
698 }
699
700 for &child in children {
702 if let Some(previous_parent) = self.parents[child.into()] {
704 self.remove_child(previous_parent, child).unwrap();
705 }
706 self.parents[child.into()] = Some(parent);
707 }
708
709 let parent_children = &mut self.children[parent_key];
710 parent_children.clear();
711 children.iter().for_each(|child| parent_children.push(*child));
712
713 self.mark_dirty(parent)?;
714
715 Ok(())
716 }
717
718 pub fn remove_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<NodeId> {
722 let index = self.children[parent.into()].iter().position(|n| *n == child).unwrap();
723 self.remove_child_at_index(parent, index)
724 }
725
726 pub fn remove_child_at_index(&mut self, parent: NodeId, child_index: usize) -> TaffyResult<NodeId> {
730 let parent_key = parent.into();
731 let child_count = self.children[parent_key].len();
732 if child_index >= child_count {
733 return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
734 }
735
736 let child = self.children[parent_key].remove(child_index);
737 self.parents[child.into()] = None;
738
739 self.mark_dirty(parent)?;
740
741 Ok(child)
742 }
743
744 pub fn remove_children_range<R>(&mut self, parent: NodeId, range: R) -> TaffyResult<()>
750 where
751 R: core::ops::RangeBounds<usize>,
752 {
753 let parent_key = parent.into();
754 for child in self.children[parent_key].drain(range) {
755 self.parents[child.into()] = None;
756 }
757
758 self.mark_dirty(parent)?;
759 Ok(())
760 }
761
762 pub fn replace_child_at_index(
766 &mut self,
767 parent: NodeId,
768 child_index: usize,
769 new_child: NodeId,
770 ) -> TaffyResult<NodeId> {
771 let parent_key = parent.into();
772
773 let child_count = self.children[parent_key].len();
774 if child_index >= child_count {
775 return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
776 }
777
778 self.parents[new_child.into()] = Some(parent);
779 let old_child = core::mem::replace(&mut self.children[parent_key][child_index], new_child);
780 self.parents[old_child.into()] = None;
781
782 self.mark_dirty(parent)?;
783
784 Ok(old_child)
785 }
786
787 #[inline]
789 pub fn child_at_index(&self, parent: NodeId, child_index: usize) -> TaffyResult<NodeId> {
790 let parent_key = parent.into();
791 let child_count = self.children[parent_key].len();
792 if child_index >= child_count {
793 return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
794 }
795
796 Ok(self.children[parent_key][child_index])
797 }
798
799 #[inline]
801 pub fn total_node_count(&self) -> usize {
802 self.nodes.len()
803 }
804
805 #[inline]
810 pub fn parent(&self, child_id: NodeId) -> Option<NodeId> {
811 self.parents[child_id.into()]
812 }
813
814 pub fn children(&self, parent: NodeId) -> TaffyResult<Vec<NodeId>> {
816 Ok(self.children[parent.into()].clone())
817 }
818
819 #[inline]
821 pub fn set_style(&mut self, node: NodeId, style: Style) -> TaffyResult<()> {
822 self.nodes[node.into()].style = style;
823 self.mark_dirty(node)?;
824 Ok(())
825 }
826
827 #[inline]
829 pub fn style(&self, node: NodeId) -> TaffyResult<&Style> {
830 Ok(&self.nodes[node.into()].style)
831 }
832
833 #[inline]
835 pub fn layout(&self, node: NodeId) -> TaffyResult<&Layout> {
836 if self.config.use_rounding {
837 Ok(&self.nodes[node.into()].final_layout)
838 } else {
839 Ok(&self.nodes[node.into()].unrounded_layout)
840 }
841 }
842
843 #[inline]
845 pub fn unrounded_layout(&self, node: NodeId) -> &Layout {
846 &self.nodes[node.into()].unrounded_layout
847 }
848
849 #[cfg(feature = "detailed_layout_info")]
854 #[inline]
855 pub fn detailed_layout_info(&self, node_id: NodeId) -> &DetailedLayoutInfo {
856 &self.nodes[node_id.into()].detailed_layout_info
857 }
858
859 pub fn mark_dirty(&mut self, node: NodeId) -> TaffyResult<()> {
863 fn mark_dirty_recursive(
865 nodes: &mut SlotMap<DefaultKey, NodeData>,
866 parents: &SlotMap<DefaultKey, Option<NodeId>>,
867 node_key: DefaultKey,
868 ) {
869 nodes[node_key].mark_dirty();
870
871 if let Some(Some(node)) = parents.get(node_key) {
872 mark_dirty_recursive(nodes, parents, (*node).into());
873 }
874 }
875
876 mark_dirty_recursive(&mut self.nodes, &self.parents, node.into());
877
878 Ok(())
879 }
880
881 #[inline]
883 pub fn dirty(&self, node: NodeId) -> TaffyResult<bool> {
884 Ok(self.nodes[node.into()].cache.is_empty())
885 }
886
887 pub fn compute_layout_with_measure<MeasureFunction>(
889 &mut self,
890 node_id: NodeId,
891 available_space: Size<AvailableSpace>,
892 measure_function: MeasureFunction,
893 ) -> Result<(), TaffyError>
894 where
895 MeasureFunction:
896 FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>, &Style) -> Size<f32>,
897 {
898 let use_rounding = self.config.use_rounding;
899 let mut taffy_view = TaffyView { taffy: self, measure_function };
900 compute_root_layout(&mut taffy_view, node_id, available_space);
901 if use_rounding {
902 round_layout(&mut taffy_view, node_id);
903 }
904 Ok(())
905 }
906
907 pub fn compute_layout(&mut self, node: NodeId, available_space: Size<AvailableSpace>) -> Result<(), TaffyError> {
909 self.compute_layout_with_measure(node, available_space, |_, _, _, _, _| Size::ZERO)
910 }
911
912 #[cfg(feature = "std")]
914 pub fn print_tree(&mut self, root: NodeId) {
915 crate::util::print_tree(self, root)
916 }
917
918 #[cfg(test)]
920 pub(crate) fn as_layout_tree(&mut self) -> impl LayoutPartialTree + CacheTree + '_ {
921 TaffyView { taffy: self, measure_function: |_, _, _, _, _| Size::ZERO }
922 }
923}
924
925#[cfg(test)]
926mod tests {
927
928 use super::*;
929 use crate::style::{Dimension, Display, FlexDirection};
930 use crate::style_helpers::*;
931 use crate::util::sys;
932
933 fn size_measure_function(
934 known_dimensions: Size<Option<f32>>,
935 _available_space: Size<AvailableSpace>,
936 _node_id: NodeId,
937 node_context: Option<&mut Size<f32>>,
938 _style: &Style,
939 ) -> Size<f32> {
940 known_dimensions.unwrap_or(node_context.cloned().unwrap_or(Size::ZERO))
941 }
942
943 #[test]
944 fn new_should_allocate_default_capacity() {
945 const DEFAULT_CAPACITY: usize = 16; let taffy: TaffyTree<()> = TaffyTree::new();
947
948 assert!(taffy.children.capacity() >= DEFAULT_CAPACITY);
949 assert!(taffy.parents.capacity() >= DEFAULT_CAPACITY);
950 assert!(taffy.nodes.capacity() >= DEFAULT_CAPACITY);
951 }
952
953 #[test]
954 fn test_with_capacity() {
955 const CAPACITY: usize = 8;
956 let taffy: TaffyTree<()> = TaffyTree::with_capacity(CAPACITY);
957
958 assert!(taffy.children.capacity() >= CAPACITY);
959 assert!(taffy.parents.capacity() >= CAPACITY);
960 assert!(taffy.nodes.capacity() >= CAPACITY);
961 }
962
963 #[test]
964 fn test_new_leaf() {
965 let mut taffy: TaffyTree<()> = TaffyTree::new();
966
967 let res = taffy.new_leaf(Style::default());
968 assert!(res.is_ok());
969 let node = res.unwrap();
970
971 assert!(taffy.child_count(node) == 0);
973 }
974
975 #[test]
976 fn new_leaf_with_context() {
977 let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
978
979 let res = taffy.new_leaf_with_context(Style::default(), Size::ZERO);
980 assert!(res.is_ok());
981 let node = res.unwrap();
982
983 assert!(taffy.child_count(node) == 0);
985 }
986
987 #[test]
989 fn test_new_with_children() {
990 let mut taffy: TaffyTree<()> = TaffyTree::new();
991 let child0 = taffy.new_leaf(Style::default()).unwrap();
992 let child1 = taffy.new_leaf(Style::default()).unwrap();
993 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
994
995 assert_eq!(taffy.child_count(node), 2);
997 assert_eq!(taffy.children(node).unwrap()[0], child0);
998 assert_eq!(taffy.children(node).unwrap()[1], child1);
999 }
1000
1001 #[test]
1002 fn remove_node_should_remove() {
1003 let mut taffy: TaffyTree<()> = TaffyTree::new();
1004
1005 let node = taffy.new_leaf(Style::default()).unwrap();
1006
1007 let _ = taffy.remove(node).unwrap();
1008 }
1009
1010 #[test]
1011 fn remove_node_should_detach_hierarchy() {
1012 let mut taffy: TaffyTree<()> = TaffyTree::new();
1013
1014 let node2 = taffy.new_leaf(Style::default()).unwrap();
1016 let node1 = taffy.new_with_children(Style::default(), &[node2]).unwrap();
1017 let node0 = taffy.new_with_children(Style::default(), &[node1]).unwrap();
1018
1019 assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
1021 assert_eq!(taffy.children(node1).unwrap().as_slice(), &[node2]);
1022
1023 let _ = taffy.remove(node1).unwrap();
1025
1026 assert!(taffy.children(node0).unwrap().is_empty());
1028 assert!(taffy.children(node2).unwrap().is_empty());
1029 }
1030
1031 #[test]
1032 fn remove_last_node() {
1033 let mut taffy: TaffyTree<()> = TaffyTree::new();
1034
1035 let parent = taffy.new_leaf(Style::default()).unwrap();
1036 let child = taffy.new_leaf(Style::default()).unwrap();
1037 taffy.add_child(parent, child).unwrap();
1038
1039 taffy.remove(child).unwrap();
1040 taffy.remove(parent).unwrap();
1041 }
1042
1043 #[test]
1044 fn set_measure() {
1045 let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
1046 let node = taffy.new_leaf_with_context(Style::default(), Size { width: 200.0, height: 200.0 }).unwrap();
1047 taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
1048 assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
1049
1050 taffy.set_node_context(node, Some(Size { width: 100.0, height: 100.0 })).unwrap();
1051 taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
1052 assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
1053 }
1054
1055 #[test]
1056 fn set_measure_of_previously_unmeasured_node() {
1057 let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
1058 let node = taffy.new_leaf(Style::default()).unwrap();
1059 taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
1060 assert_eq!(taffy.layout(node).unwrap().size.width, 0.0);
1061
1062 taffy.set_node_context(node, Some(Size { width: 100.0, height: 100.0 })).unwrap();
1063 taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
1064 assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
1065 }
1066
1067 #[test]
1069 fn add_child() {
1070 let mut taffy: TaffyTree<()> = TaffyTree::new();
1071 let node = taffy.new_leaf(Style::default()).unwrap();
1072 assert_eq!(taffy.child_count(node), 0);
1073
1074 let child0 = taffy.new_leaf(Style::default()).unwrap();
1075 taffy.add_child(node, child0).unwrap();
1076 assert_eq!(taffy.child_count(node), 1);
1077
1078 let child1 = taffy.new_leaf(Style::default()).unwrap();
1079 taffy.add_child(node, child1).unwrap();
1080 assert_eq!(taffy.child_count(node), 2);
1081 }
1082
1083 #[test]
1084 fn insert_child_at_index() {
1085 let mut taffy: TaffyTree<()> = TaffyTree::new();
1086
1087 let child0 = taffy.new_leaf(Style::default()).unwrap();
1088 let child1 = taffy.new_leaf(Style::default()).unwrap();
1089 let child2 = taffy.new_leaf(Style::default()).unwrap();
1090
1091 let node = taffy.new_leaf(Style::default()).unwrap();
1092 assert_eq!(taffy.child_count(node), 0);
1093
1094 taffy.insert_child_at_index(node, 0, child0).unwrap();
1095 assert_eq!(taffy.child_count(node), 1);
1096 assert_eq!(taffy.children(node).unwrap()[0], child0);
1097
1098 taffy.insert_child_at_index(node, 0, child1).unwrap();
1099 assert_eq!(taffy.child_count(node), 2);
1100 assert_eq!(taffy.children(node).unwrap()[0], child1);
1101 assert_eq!(taffy.children(node).unwrap()[1], child0);
1102
1103 taffy.insert_child_at_index(node, 1, child2).unwrap();
1104 assert_eq!(taffy.child_count(node), 3);
1105 assert_eq!(taffy.children(node).unwrap()[0], child1);
1106 assert_eq!(taffy.children(node).unwrap()[1], child2);
1107 assert_eq!(taffy.children(node).unwrap()[2], child0);
1108 }
1109
1110 #[test]
1111 fn set_children() {
1112 let mut taffy: TaffyTree<()> = TaffyTree::new();
1113
1114 let child0 = taffy.new_leaf(Style::default()).unwrap();
1115 let child1 = taffy.new_leaf(Style::default()).unwrap();
1116 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1117
1118 assert_eq!(taffy.child_count(node), 2);
1119 assert_eq!(taffy.children(node).unwrap()[0], child0);
1120 assert_eq!(taffy.children(node).unwrap()[1], child1);
1121
1122 let child2 = taffy.new_leaf(Style::default()).unwrap();
1123 let child3 = taffy.new_leaf(Style::default()).unwrap();
1124 taffy.set_children(node, &[child2, child3]).unwrap();
1125
1126 assert_eq!(taffy.child_count(node), 2);
1127 assert_eq!(taffy.children(node).unwrap()[0], child2);
1128 assert_eq!(taffy.children(node).unwrap()[1], child3);
1129 }
1130
1131 #[test]
1133 fn remove_child() {
1134 let mut taffy: TaffyTree<()> = TaffyTree::new();
1135 let child0 = taffy.new_leaf(Style::default()).unwrap();
1136 let child1 = taffy.new_leaf(Style::default()).unwrap();
1137 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1138
1139 assert_eq!(taffy.child_count(node), 2);
1140
1141 taffy.remove_child(node, child0).unwrap();
1142 assert_eq!(taffy.child_count(node), 1);
1143 assert_eq!(taffy.children(node).unwrap()[0], child1);
1144
1145 taffy.remove_child(node, child1).unwrap();
1146 assert_eq!(taffy.child_count(node), 0);
1147 }
1148
1149 #[test]
1150 fn remove_child_at_index() {
1151 let mut taffy: TaffyTree<()> = TaffyTree::new();
1152 let child0 = taffy.new_leaf(Style::default()).unwrap();
1153 let child1 = taffy.new_leaf(Style::default()).unwrap();
1154 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1155
1156 assert_eq!(taffy.child_count(node), 2);
1157
1158 taffy.remove_child_at_index(node, 0).unwrap();
1159 assert_eq!(taffy.child_count(node), 1);
1160 assert_eq!(taffy.children(node).unwrap()[0], child1);
1161
1162 taffy.remove_child_at_index(node, 0).unwrap();
1163 assert_eq!(taffy.child_count(node), 0);
1164 }
1165
1166 #[test]
1167 fn remove_children_range() {
1168 let mut taffy: TaffyTree<()> = TaffyTree::new();
1169 let child0 = taffy.new_leaf(Style::default()).unwrap();
1170 let child1 = taffy.new_leaf(Style::default()).unwrap();
1171 let child2 = taffy.new_leaf(Style::default()).unwrap();
1172 let child3 = taffy.new_leaf(Style::default()).unwrap();
1173 let node = taffy.new_with_children(Style::default(), &[child0, child1, child2, child3]).unwrap();
1174
1175 assert_eq!(taffy.child_count(node), 4);
1176
1177 taffy.remove_children_range(node, 1..=2).unwrap();
1178 assert_eq!(taffy.child_count(node), 2);
1179 assert_eq!(taffy.children(node).unwrap(), [child0, child3]);
1180 for child in [child0, child3] {
1181 assert_eq!(taffy.parent(child), Some(node));
1182 }
1183 for child in [child1, child2] {
1184 assert_eq!(taffy.parent(child), None);
1185 }
1186 }
1187
1188 #[test]
1190 fn remove_child_updates_parents() {
1191 let mut taffy: TaffyTree<()> = TaffyTree::new();
1192
1193 let parent = taffy.new_leaf(Style::default()).unwrap();
1194 let child = taffy.new_leaf(Style::default()).unwrap();
1195
1196 taffy.add_child(parent, child).unwrap();
1197
1198 taffy.remove(parent).unwrap();
1199
1200 assert!(taffy.set_children(child, &[]).is_ok());
1202 }
1203
1204 #[test]
1205 fn replace_child_at_index() {
1206 let mut taffy: TaffyTree<()> = TaffyTree::new();
1207
1208 let child0 = taffy.new_leaf(Style::default()).unwrap();
1209 let child1 = taffy.new_leaf(Style::default()).unwrap();
1210
1211 let node = taffy.new_with_children(Style::default(), &[child0]).unwrap();
1212 assert_eq!(taffy.child_count(node), 1);
1213 assert_eq!(taffy.children(node).unwrap()[0], child0);
1214
1215 taffy.replace_child_at_index(node, 0, child1).unwrap();
1216 assert_eq!(taffy.child_count(node), 1);
1217 assert_eq!(taffy.children(node).unwrap()[0], child1);
1218 }
1219 #[test]
1220 fn test_child_at_index() {
1221 let mut taffy: TaffyTree<()> = TaffyTree::new();
1222 let child0 = taffy.new_leaf(Style::default()).unwrap();
1223 let child1 = taffy.new_leaf(Style::default()).unwrap();
1224 let child2 = taffy.new_leaf(Style::default()).unwrap();
1225 let node = taffy.new_with_children(Style::default(), &[child0, child1, child2]).unwrap();
1226
1227 assert!(if let Ok(result) = taffy.child_at_index(node, 0) { result == child0 } else { false });
1228 assert!(if let Ok(result) = taffy.child_at_index(node, 1) { result == child1 } else { false });
1229 assert!(if let Ok(result) = taffy.child_at_index(node, 2) { result == child2 } else { false });
1230 }
1231 #[test]
1232 fn test_child_count() {
1233 let mut taffy: TaffyTree<()> = TaffyTree::new();
1234 let child0 = taffy.new_leaf(Style::default()).unwrap();
1235 let child1 = taffy.new_leaf(Style::default()).unwrap();
1236 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1237
1238 assert!(taffy.child_count(node) == 2);
1239 assert!(taffy.child_count(child0) == 0);
1240 assert!(taffy.child_count(child1) == 0);
1241 }
1242
1243 #[allow(clippy::vec_init_then_push)]
1244 #[test]
1245 fn test_children() {
1246 let mut taffy: TaffyTree<()> = TaffyTree::new();
1247 let child0 = taffy.new_leaf(Style::default()).unwrap();
1248 let child1 = taffy.new_leaf(Style::default()).unwrap();
1249 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1250
1251 let mut children = sys::Vec::new();
1252 children.push(child0);
1253 children.push(child1);
1254
1255 let children_result = taffy.children(node).unwrap();
1256 assert_eq!(children_result, children);
1257
1258 assert!(taffy.children(child0).unwrap().is_empty());
1259 }
1260 #[test]
1261 fn test_set_style() {
1262 let mut taffy: TaffyTree<()> = TaffyTree::new();
1263
1264 let node = taffy.new_leaf(Style::default()).unwrap();
1265 assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
1266
1267 taffy.set_style(node, Style { display: Display::None, ..Style::default() }).unwrap();
1268 assert_eq!(taffy.style(node).unwrap().display, Display::None);
1269 }
1270 #[test]
1271 fn test_style() {
1272 let mut taffy: TaffyTree<()> = TaffyTree::new();
1273
1274 let style = Style { display: Display::None, flex_direction: FlexDirection::RowReverse, ..Default::default() };
1275
1276 let node = taffy.new_leaf(style.clone()).unwrap();
1277
1278 let res = taffy.style(node);
1279 assert!(res.is_ok());
1280 assert!(res.unwrap() == &style);
1281 }
1282 #[test]
1283 fn test_layout() {
1284 let mut taffy: TaffyTree<()> = TaffyTree::new();
1285 let node = taffy.new_leaf(Style::default()).unwrap();
1286
1287 let res = taffy.layout(node);
1289 assert!(res.is_ok());
1290 }
1291
1292 #[test]
1293 fn test_mark_dirty() {
1294 let mut taffy: TaffyTree<()> = TaffyTree::new();
1295 let child0 = taffy.new_leaf(Style::default()).unwrap();
1296 let child1 = taffy.new_leaf(Style::default()).unwrap();
1297 let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
1298
1299 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
1300
1301 assert_eq!(taffy.dirty(child0), Ok(false));
1302 assert_eq!(taffy.dirty(child1), Ok(false));
1303 assert_eq!(taffy.dirty(node), Ok(false));
1304
1305 taffy.mark_dirty(node).unwrap();
1306 assert_eq!(taffy.dirty(child0), Ok(false));
1307 assert_eq!(taffy.dirty(child1), Ok(false));
1308 assert_eq!(taffy.dirty(node), Ok(true));
1309
1310 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
1311 taffy.mark_dirty(child0).unwrap();
1312 assert_eq!(taffy.dirty(child0), Ok(true));
1313 assert_eq!(taffy.dirty(child1), Ok(false));
1314 assert_eq!(taffy.dirty(node), Ok(true));
1315 }
1316
1317 #[test]
1318 fn compute_layout_should_produce_valid_result() {
1319 let mut taffy: TaffyTree<()> = TaffyTree::new();
1320 let node_result = taffy.new_leaf(Style {
1321 size: Size { width: Dimension::Length(10f32), height: Dimension::Length(10f32) },
1322 ..Default::default()
1323 });
1324 assert!(node_result.is_ok());
1325 let node = node_result.unwrap();
1326 let layout_result = taffy.compute_layout(
1327 node,
1328 Size { width: AvailableSpace::Definite(100.), height: AvailableSpace::Definite(100.) },
1329 );
1330 assert!(layout_result.is_ok());
1331 }
1332
1333 #[test]
1334 fn make_sure_layout_location_is_top_left() {
1335 use crate::prelude::Rect;
1336
1337 let mut taffy: TaffyTree<()> = TaffyTree::new();
1338
1339 let node = taffy
1340 .new_leaf(Style {
1341 size: Size { width: Dimension::Percent(1f32), height: Dimension::Percent(1f32) },
1342 ..Default::default()
1343 })
1344 .unwrap();
1345
1346 let root = taffy
1347 .new_with_children(
1348 Style {
1349 size: Size { width: Dimension::Length(100f32), height: Dimension::Length(100f32) },
1350 padding: Rect {
1351 left: length(10f32),
1352 right: length(20f32),
1353 top: length(30f32),
1354 bottom: length(40f32),
1355 },
1356 ..Default::default()
1357 },
1358 &[node],
1359 )
1360 .unwrap();
1361
1362 taffy.compute_layout(root, Size::MAX_CONTENT).unwrap();
1363
1364 let layout = taffy.layout(node).unwrap();
1373 assert_eq!(layout.location.x, 10f32);
1374 assert_eq!(layout.location.y, 30f32);
1375 }
1376
1377 #[test]
1378 fn set_children_reparents() {
1379 let mut taffy: TaffyTree<()> = TaffyTree::new();
1380 let child = taffy.new_leaf(Style::default()).unwrap();
1381 let old_parent = taffy.new_with_children(Style::default(), &[child]).unwrap();
1382
1383 let new_parent = taffy.new_leaf(Style::default()).unwrap();
1384 taffy.set_children(new_parent, &[child]).unwrap();
1385
1386 assert!(taffy.children(old_parent).unwrap().is_empty());
1387 }
1388}