Skip to main content

basic/
basic.rs

1use taffy::prelude::*;
2
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    let child = taffy.new_leaf(Style {
7        size: Size { width: Dimension::from_percent(0.5), height: Dimension::AUTO },
8        ..Default::default()
9    })?;
10
11    let node = taffy.new_with_children(
12        Style {
13            size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
14            justify_content: Some(JustifyContent::CENTER),
15            ..Default::default()
16        },
17        &[child],
18    )?;
19
20    println!("Compute layout with 100x100 viewport:");
21    taffy.compute_layout(
22        node,
23        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
24    )?;
25    println!("node: {:#?}", taffy.layout(node)?);
26    println!("child: {:#?}", taffy.layout(child)?);
27
28    println!("Compute layout with undefined (infinite) viewport:");
29    taffy.compute_layout(node, Size::MAX_CONTENT)?;
30    println!("node: {:#?}", taffy.layout(node)?);
31    println!("child: {:#?}", taffy.layout(child)?);
32
33    Ok(())
34}