use slotmap::{DefaultKey, SlotMap, SparseSecondaryMap};
use crate::geometry::Size;
use crate::style::{AvailableSpace, Display, Style};
use crate::tree::{Cache, Layout, LayoutInput, LayoutOutput, LayoutTree, NodeData, NodeId, PartialLayoutTree, RunMode};
use crate::util::debug::{debug_log, debug_log_node};
use crate::util::sys::{new_vec_with_capacity, ChildrenVec, Vec};
#[cfg(feature = "block_layout")]
use crate::compute::compute_block_layout;
#[cfg(feature = "flexbox")]
use crate::compute::compute_flexbox_layout;
#[cfg(feature = "grid")]
use crate::compute::compute_grid_layout;
use crate::compute::{compute_cached_layout, compute_hidden_layout, compute_layout, compute_leaf_layout, round_layout};
pub type TaffyResult<T> = Result<T, TaffyError>;
#[derive(Debug)]
pub enum TaffyError {
ChildIndexOutOfBounds {
parent: NodeId,
child_index: usize,
child_count: usize,
},
InvalidParentNode(NodeId),
InvalidChildNode(NodeId),
InvalidInputNode(NodeId),
}
impl core::fmt::Display for TaffyError {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count } => {
write!(f, "Index (is {child_index}) should be < child_count ({child_count}) for parent node {parent:?}")
}
TaffyError::InvalidParentNode(parent) => {
write!(f, "Parent Node {parent:?} is not in the TaffyTree instance")
}
TaffyError::InvalidChildNode(child) => write!(f, "Child Node {child:?} is not in the TaffyTree instance"),
TaffyError::InvalidInputNode(node) => write!(f, "Supplied Node {node:?} is not in the TaffyTree instance"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for TaffyError {}
pub(crate) struct TaffyConfig {
pub(crate) use_rounding: bool,
}
impl Default for TaffyConfig {
fn default() -> Self {
Self { use_rounding: true }
}
}
pub struct TaffyTree<NodeContext = ()> {
pub(crate) nodes: SlotMap<DefaultKey, NodeData>,
pub(crate) node_context_data: SparseSecondaryMap<DefaultKey, NodeContext>,
pub(crate) children: SlotMap<DefaultKey, ChildrenVec<NodeId>>,
pub(crate) parents: SlotMap<DefaultKey, Option<NodeId>>,
pub(crate) config: TaffyConfig,
}
impl Default for TaffyTree {
fn default() -> TaffyTree<()> {
TaffyTree::new()
}
}
pub(crate) struct TaffyChildIter<'a>(core::slice::Iter<'a, NodeId>);
impl<'a> Iterator for TaffyChildIter<'a> {
type Item = NodeId;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().copied()
}
}
pub(crate) struct TaffyView<'t, NodeContext, MeasureFunction>
where
MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
{
pub(crate) taffy: &'t mut TaffyTree<NodeContext>,
pub(crate) measure_function: MeasureFunction,
}
impl<'t, NodeContext, MeasureFunction> PartialLayoutTree for TaffyView<'t, NodeContext, MeasureFunction>
where
MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
{
type ChildIter<'a> = TaffyChildIter<'a> where Self: 'a;
#[inline(always)]
fn child_ids(&self, node: NodeId) -> Self::ChildIter<'_> {
TaffyChildIter(self.taffy.children[node.into()].iter())
}
#[inline(always)]
fn child_count(&self, node: NodeId) -> usize {
self.taffy.children[node.into()].len()
}
#[inline(always)]
fn get_child_id(&self, node: NodeId, id: usize) -> NodeId {
self.taffy.children[node.into()][id]
}
#[inline(always)]
fn get_style(&self, node: NodeId) -> &Style {
&self.taffy.nodes[node.into()].style
}
#[inline(always)]
fn get_cache_mut(&mut self, node: NodeId) -> &mut Cache {
&mut self.taffy.nodes[node.into()].cache
}
#[inline(always)]
fn set_unrounded_layout(&mut self, node_id: NodeId, layout: &Layout) {
self.taffy.nodes[node_id.into()].unrounded_layout = *layout;
}
#[inline(always)]
fn compute_child_layout(&mut self, node: NodeId, inputs: LayoutInput) -> LayoutOutput {
if inputs.run_mode == RunMode::PerformHiddenLayout {
debug_log!("HIDDEN");
return compute_hidden_layout(self, node);
}
compute_cached_layout(self, node, inputs, |tree, node, inputs| {
let display_mode = tree.get_style(node).display;
let has_children = tree.child_count(node) > 0;
debug_log!(display_mode);
debug_log_node!(
inputs.known_dimensions,
inputs.parent_size,
inputs.available_space,
inputs.run_mode,
inputs.sizing_mode
);
match (display_mode, has_children) {
(Display::None, _) => compute_hidden_layout(tree, node),
#[cfg(feature = "block_layout")]
(Display::Block, true) => compute_block_layout(tree, node, inputs),
#[cfg(feature = "flexbox")]
(Display::Flex, true) => compute_flexbox_layout(tree, node, inputs),
#[cfg(feature = "grid")]
(Display::Grid, true) => compute_grid_layout(tree, node, inputs),
(_, false) => {
let node_key = node.into();
let style = &tree.taffy.nodes[node_key].style;
let needs_measure = tree.taffy.nodes[node_key].needs_measure;
let measure_function = needs_measure.then_some(|known_dimensions, available_space| {
let node_context = tree.taffy.node_context_data.get_mut(node_key);
(tree.measure_function)(known_dimensions, available_space, node, node_context)
});
compute_leaf_layout(inputs, style, measure_function)
}
}
})
}
}
impl<'t, NodeContext, MeasureFunction> LayoutTree for TaffyView<'t, NodeContext, MeasureFunction>
where
MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
{
#[inline(always)]
fn get_unrounded_layout(&self, node: NodeId) -> &Layout {
&self.taffy.nodes[node.into()].unrounded_layout
}
#[inline(always)]
fn get_final_layout(&self, node: NodeId) -> &Layout {
&self.taffy.nodes[node.into()].final_layout
}
#[inline(always)]
fn set_final_layout(&mut self, node_id: NodeId, layout: &Layout) {
self.taffy.nodes[node_id.into()].final_layout = *layout;
}
}
#[allow(clippy::iter_cloned_collect)] impl<NodeContext> TaffyTree<NodeContext> {
#[must_use]
pub fn new() -> Self {
Self::with_capacity(16)
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
TaffyTree {
nodes: SlotMap::with_capacity(capacity),
children: SlotMap::with_capacity(capacity),
parents: SlotMap::with_capacity(capacity),
node_context_data: SparseSecondaryMap::with_capacity(capacity),
config: TaffyConfig::default(),
}
}
pub fn enable_rounding(&mut self) {
self.config.use_rounding = true;
}
pub fn disable_rounding(&mut self) {
self.config.use_rounding = false;
}
pub fn new_leaf(&mut self, layout: Style) -> TaffyResult<NodeId> {
let id = self.nodes.insert(NodeData::new(layout));
let _ = self.children.insert(new_vec_with_capacity(0));
let _ = self.parents.insert(None);
Ok(id.into())
}
pub fn new_leaf_with_context(&mut self, layout: Style, context: NodeContext) -> TaffyResult<NodeId> {
let mut data = NodeData::new(layout);
data.needs_measure = true;
let id = self.nodes.insert(data);
self.node_context_data.insert(id, context);
let _ = self.children.insert(new_vec_with_capacity(0));
let _ = self.parents.insert(None);
Ok(id.into())
}
pub fn new_with_children(&mut self, layout: Style, children: &[NodeId]) -> TaffyResult<NodeId> {
let id = NodeId::from(self.nodes.insert(NodeData::new(layout)));
for child in children {
self.parents[(*child).into()] = Some(id);
}
let _ = self.children.insert(children.iter().copied().collect::<_>());
let _ = self.parents.insert(None);
Ok(id)
}
pub fn clear(&mut self) {
self.nodes.clear();
self.children.clear();
self.parents.clear();
}
pub fn remove(&mut self, node: NodeId) -> TaffyResult<NodeId> {
let key = node.into();
if let Some(parent) = self.parents[key] {
if let Some(children) = self.children.get_mut(parent.into()) {
children.retain(|f| *f != node);
}
}
if let Some(children) = self.children.get(key) {
for child in children.iter().copied() {
self.parents[child.into()] = None;
}
}
let _ = self.children.remove(key);
let _ = self.parents.remove(key);
let _ = self.nodes.remove(key);
Ok(node)
}
pub fn set_node_context(&mut self, node: NodeId, measure: Option<NodeContext>) -> TaffyResult<()> {
let key = node.into();
if let Some(measure) = measure {
self.nodes[key].needs_measure = true;
self.node_context_data.insert(key, measure);
} else {
self.nodes[key].needs_measure = false;
self.node_context_data.remove(key);
}
self.mark_dirty(node)?;
Ok(())
}
pub fn get_node_context(&self, node: NodeId) -> Option<&NodeContext> {
self.node_context_data.get(node.into())
}
pub fn get_node_context_mut(&mut self, node: NodeId) -> Option<&mut NodeContext> {
self.node_context_data.get_mut(node.into())
}
pub fn add_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<()> {
let parent_key = parent.into();
let child_key = child.into();
self.parents[child_key] = Some(parent);
self.children[parent_key].push(child);
self.mark_dirty(parent)?;
Ok(())
}
pub fn insert_child_at_index(&mut self, parent: NodeId, child_index: usize, child: NodeId) -> TaffyResult<()> {
let parent_key = parent.into();
let child_count = self.children[parent_key].len();
if child_index > child_count {
return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
}
self.parents[child.into()] = Some(parent);
self.children[parent_key].insert(child_index, child);
self.mark_dirty(parent)?;
Ok(())
}
pub fn set_children(&mut self, parent: NodeId, children: &[NodeId]) -> TaffyResult<()> {
let parent_key = parent.into();
for child in &self.children[parent_key] {
self.parents[(*child).into()] = None;
}
for child in children {
self.parents[(*child).into()] = Some(parent);
}
let parent_children = &mut self.children[parent_key];
parent_children.clear();
children.iter().for_each(|child| parent_children.push(*child));
self.mark_dirty(parent)?;
Ok(())
}
pub fn remove_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<NodeId> {
let index = self.children[parent.into()].iter().position(|n| *n == child).unwrap();
self.remove_child_at_index(parent, index)
}
pub fn remove_child_at_index(&mut self, parent: NodeId, child_index: usize) -> TaffyResult<NodeId> {
let parent_key = parent.into();
let child_count = self.children[parent_key].len();
if child_index >= child_count {
return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
}
let child = self.children[parent_key].remove(child_index);
self.parents[child.into()] = None;
self.mark_dirty(parent)?;
Ok(child)
}
pub fn replace_child_at_index(
&mut self,
parent: NodeId,
child_index: usize,
new_child: NodeId,
) -> TaffyResult<NodeId> {
let parent_key = parent.into();
let child_count = self.children[parent_key].len();
if child_index >= child_count {
return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
}
self.parents[new_child.into()] = Some(parent);
let old_child = core::mem::replace(&mut self.children[parent_key][child_index], new_child);
self.parents[old_child.into()] = None;
self.mark_dirty(parent)?;
Ok(old_child)
}
pub fn child_at_index(&self, parent: NodeId, child_index: usize) -> TaffyResult<NodeId> {
let parent_key = parent.into();
let child_count = self.children[parent_key].len();
if child_index >= child_count {
return Err(TaffyError::ChildIndexOutOfBounds { parent, child_index, child_count });
}
Ok(self.children[parent_key][child_index])
}
pub fn total_node_count(&self) -> usize {
self.nodes.len()
}
pub fn child_count(&self, parent: NodeId) -> TaffyResult<usize> {
Ok(self.children[parent.into()].len())
}
pub fn children(&self, parent: NodeId) -> TaffyResult<Vec<NodeId>> {
Ok(self.children[parent.into()].iter().copied().collect::<_>())
}
pub fn set_style(&mut self, node: NodeId, style: Style) -> TaffyResult<()> {
self.nodes[node.into()].style = style;
self.mark_dirty(node)?;
Ok(())
}
pub fn style(&self, node: NodeId) -> TaffyResult<&Style> {
Ok(&self.nodes[node.into()].style)
}
pub fn layout(&self, node: NodeId) -> TaffyResult<&Layout> {
if self.config.use_rounding {
Ok(&self.nodes[node.into()].final_layout)
} else {
Ok(&self.nodes[node.into()].unrounded_layout)
}
}
pub fn mark_dirty(&mut self, node: NodeId) -> TaffyResult<()> {
fn mark_dirty_recursive(
nodes: &mut SlotMap<DefaultKey, NodeData>,
parents: &SlotMap<DefaultKey, Option<NodeId>>,
node_key: DefaultKey,
) {
nodes[node_key].mark_dirty();
if let Some(Some(node)) = parents.get(node_key) {
mark_dirty_recursive(nodes, parents, (*node).into());
}
}
mark_dirty_recursive(&mut self.nodes, &self.parents, node.into());
Ok(())
}
pub fn dirty(&self, node: NodeId) -> TaffyResult<bool> {
Ok(self.nodes[node.into()].cache.is_empty())
}
pub fn compute_layout_with_measure<MeasureFunction>(
&mut self,
node_id: NodeId,
available_space: Size<AvailableSpace>,
measure_function: MeasureFunction,
) -> Result<(), TaffyError>
where
MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
{
let use_rounding = self.config.use_rounding;
let mut taffy_view = TaffyView { taffy: self, measure_function };
compute_layout(&mut taffy_view, node_id, available_space);
if use_rounding {
round_layout(&mut taffy_view, node_id);
}
Ok(())
}
pub fn compute_layout(&mut self, node: NodeId, available_space: Size<AvailableSpace>) -> Result<(), TaffyError> {
self.compute_layout_with_measure(node, available_space, |_, _, _, _| Size::ZERO)
}
#[cfg(feature = "std")]
pub fn print_tree(&mut self, root: NodeId) {
let taffy_view = TaffyView { taffy: self, measure_function: |_, _, _, _| Size::ZERO };
crate::util::print_tree(&taffy_view, root)
}
#[cfg(test)]
pub(crate) fn as_layout_tree(&mut self) -> impl LayoutTree + '_ {
TaffyView { taffy: self, measure_function: |_, _, _, _| Size::ZERO }
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::bool_assert_comparison)]
use super::*;
use crate::style::{Dimension, Display, FlexDirection};
use crate::style_helpers::*;
use crate::util::sys;
fn size_measure_function(
known_dimensions: Size<Option<f32>>,
_available_space: Size<AvailableSpace>,
_node_id: NodeId,
node_context: Option<&mut Size<f32>>,
) -> Size<f32> {
known_dimensions.unwrap_or(node_context.cloned().unwrap_or(Size::ZERO))
}
#[test]
fn new_should_allocate_default_capacity() {
const DEFAULT_CAPACITY: usize = 16; let taffy: TaffyTree<()> = TaffyTree::new();
assert!(taffy.children.capacity() >= DEFAULT_CAPACITY);
assert!(taffy.parents.capacity() >= DEFAULT_CAPACITY);
assert!(taffy.nodes.capacity() >= DEFAULT_CAPACITY);
}
#[test]
fn test_with_capacity() {
const CAPACITY: usize = 8;
let taffy: TaffyTree<()> = TaffyTree::with_capacity(CAPACITY);
assert!(taffy.children.capacity() >= CAPACITY);
assert!(taffy.parents.capacity() >= CAPACITY);
assert!(taffy.nodes.capacity() >= CAPACITY);
}
#[test]
fn test_new_leaf() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let res = taffy.new_leaf(Style::default());
assert!(res.is_ok());
let node = res.unwrap();
assert!(taffy.child_count(node).unwrap() == 0);
}
#[test]
fn new_leaf_with_context() {
let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
let res = taffy.new_leaf_with_context(Style::default(), Size::ZERO);
assert!(res.is_ok());
let node = res.unwrap();
assert!(taffy.child_count(node).unwrap() == 0);
}
#[test]
fn test_new_with_children() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
assert_eq!(taffy.children(node).unwrap()[0], child0);
assert_eq!(taffy.children(node).unwrap()[1], child1);
}
#[test]
fn remove_node_should_remove() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node = taffy.new_leaf(Style::default()).unwrap();
let _ = taffy.remove(node).unwrap();
}
#[test]
fn remove_node_should_detach_herarchy() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node2 = taffy.new_leaf(Style::default()).unwrap();
let node1 = taffy.new_with_children(Style::default(), &[node2]).unwrap();
let node0 = taffy.new_with_children(Style::default(), &[node1]).unwrap();
assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
assert_eq!(taffy.children(node1).unwrap().as_slice(), &[node2]);
let _ = taffy.remove(node1).unwrap();
assert!(taffy.children(node0).unwrap().is_empty());
assert!(taffy.children(node2).unwrap().is_empty());
}
#[test]
fn remove_last_node() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let parent = taffy.new_leaf(Style::default()).unwrap();
let child = taffy.new_leaf(Style::default()).unwrap();
taffy.add_child(parent, child).unwrap();
taffy.remove(child).unwrap();
taffy.remove(parent).unwrap();
}
#[test]
fn set_measure() {
let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
let node = taffy.new_leaf_with_context(Style::default(), Size { width: 200.0, height: 200.0 }).unwrap();
taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
taffy.set_node_context(node, Some(Size { width: 100.0, height: 100.0 })).unwrap();
taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
}
#[test]
fn set_measure_of_previously_unmeasured_node() {
let mut taffy: TaffyTree<Size<f32>> = TaffyTree::new();
let node = taffy.new_leaf(Style::default()).unwrap();
taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
assert_eq!(taffy.layout(node).unwrap().size.width, 0.0);
taffy.set_node_context(node, Some(Size { width: 100.0, height: 100.0 })).unwrap();
taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, size_measure_function).unwrap();
assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
}
#[test]
fn add_child() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node = taffy.new_leaf(Style::default()).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 0);
let child0 = taffy.new_leaf(Style::default()).unwrap();
taffy.add_child(node, child0).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
let child1 = taffy.new_leaf(Style::default()).unwrap();
taffy.add_child(node, child1).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
}
#[test]
fn insert_child_at_index() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let child2 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_leaf(Style::default()).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 0);
taffy.insert_child_at_index(node, 0, child0).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
assert_eq!(taffy.children(node).unwrap()[0], child0);
taffy.insert_child_at_index(node, 0, child1).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
assert_eq!(taffy.children(node).unwrap()[0], child1);
assert_eq!(taffy.children(node).unwrap()[1], child0);
taffy.insert_child_at_index(node, 1, child2).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 3);
assert_eq!(taffy.children(node).unwrap()[0], child1);
assert_eq!(taffy.children(node).unwrap()[1], child2);
assert_eq!(taffy.children(node).unwrap()[2], child0);
}
#[test]
fn set_children() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
assert_eq!(taffy.children(node).unwrap()[0], child0);
assert_eq!(taffy.children(node).unwrap()[1], child1);
let child2 = taffy.new_leaf(Style::default()).unwrap();
let child3 = taffy.new_leaf(Style::default()).unwrap();
taffy.set_children(node, &[child2, child3]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
assert_eq!(taffy.children(node).unwrap()[0], child2);
assert_eq!(taffy.children(node).unwrap()[1], child3);
}
#[test]
fn remove_child() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
taffy.remove_child(node, child0).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
assert_eq!(taffy.children(node).unwrap()[0], child1);
taffy.remove_child(node, child1).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 0);
}
#[test]
fn remove_child_at_index() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 2);
taffy.remove_child_at_index(node, 0).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
assert_eq!(taffy.children(node).unwrap()[0], child1);
taffy.remove_child_at_index(node, 0).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 0);
}
#[test]
fn remove_child_updates_parents() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let parent = taffy.new_leaf(Style::default()).unwrap();
let child = taffy.new_leaf(Style::default()).unwrap();
taffy.add_child(parent, child).unwrap();
taffy.remove(parent).unwrap();
assert!(taffy.set_children(child, &[]).is_ok());
}
#[test]
fn replace_child_at_index() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0]).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
assert_eq!(taffy.children(node).unwrap()[0], child0);
taffy.replace_child_at_index(node, 0, child1).unwrap();
assert_eq!(taffy.child_count(node).unwrap(), 1);
assert_eq!(taffy.children(node).unwrap()[0], child1);
}
#[test]
fn test_child_at_index() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let child2 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1, child2]).unwrap();
assert!(if let Ok(result) = taffy.child_at_index(node, 0) { result == child0 } else { false });
assert!(if let Ok(result) = taffy.child_at_index(node, 1) { result == child1 } else { false });
assert!(if let Ok(result) = taffy.child_at_index(node, 2) { result == child2 } else { false });
}
#[test]
fn test_child_count() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
assert!(if let Ok(count) = taffy.child_count(node) { count == 2 } else { false });
assert!(if let Ok(count) = taffy.child_count(child0) { count == 0 } else { false });
assert!(if let Ok(count) = taffy.child_count(child1) { count == 0 } else { false });
}
#[allow(clippy::vec_init_then_push)]
#[test]
fn test_children() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
let mut children = sys::Vec::new();
children.push(child0);
children.push(child1);
let children_result = taffy.children(node).unwrap();
assert_eq!(children_result, children);
assert!(taffy.children(child0).unwrap().is_empty());
}
#[test]
fn test_set_style() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node = taffy.new_leaf(Style::default()).unwrap();
assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
taffy.set_style(node, Style { display: Display::None, ..Style::default() }).unwrap();
assert_eq!(taffy.style(node).unwrap().display, Display::None);
}
#[test]
fn test_style() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let style = Style { display: Display::None, flex_direction: FlexDirection::RowReverse, ..Default::default() };
let node = taffy.new_leaf(style.clone()).unwrap();
let res = taffy.style(node);
assert!(res.is_ok());
assert!(res.unwrap() == &style);
}
#[test]
fn test_layout() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node = taffy.new_leaf(Style::default()).unwrap();
let res = taffy.layout(node);
assert!(res.is_ok());
}
#[test]
fn test_mark_dirty() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let child0 = taffy.new_leaf(Style::default()).unwrap();
let child1 = taffy.new_leaf(Style::default()).unwrap();
let node = taffy.new_with_children(Style::default(), &[child0, child1]).unwrap();
taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
assert_eq!(taffy.dirty(child0).unwrap(), false);
assert_eq!(taffy.dirty(child1).unwrap(), false);
assert_eq!(taffy.dirty(node).unwrap(), false);
taffy.mark_dirty(node).unwrap();
assert_eq!(taffy.dirty(child0).unwrap(), false);
assert_eq!(taffy.dirty(child1).unwrap(), false);
assert_eq!(taffy.dirty(node).unwrap(), true);
taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
taffy.mark_dirty(child0).unwrap();
assert_eq!(taffy.dirty(child0).unwrap(), true);
assert_eq!(taffy.dirty(child1).unwrap(), false);
assert_eq!(taffy.dirty(node).unwrap(), true);
}
#[test]
fn compute_layout_should_produce_valid_result() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node_result = taffy.new_leaf(Style {
size: Size { width: Dimension::Length(10f32), height: Dimension::Length(10f32) },
..Default::default()
});
assert!(node_result.is_ok());
let node = node_result.unwrap();
let layout_result = taffy.compute_layout(
node,
Size { width: AvailableSpace::Definite(100.), height: AvailableSpace::Definite(100.) },
);
assert!(layout_result.is_ok());
}
#[test]
fn make_sure_layout_location_is_top_left() {
use crate::prelude::Rect;
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node = taffy
.new_leaf(Style {
size: Size { width: Dimension::Percent(1f32), height: Dimension::Percent(1f32) },
..Default::default()
})
.unwrap();
let root = taffy
.new_with_children(
Style {
size: Size { width: Dimension::Length(100f32), height: Dimension::Length(100f32) },
padding: Rect {
left: length(10f32),
right: length(20f32),
top: length(30f32),
bottom: length(40f32),
},
..Default::default()
},
&[node],
)
.unwrap();
taffy.compute_layout(root, Size::MAX_CONTENT).unwrap();
let layout = taffy.layout(node).unwrap();
assert_eq!(layout.location.x, 10f32);
assert_eq!(layout.location.y, 30f32);
}
}