Skip to main content

TaffyTree

Struct TaffyTree 

Source
pub struct TaffyTree<NodeContext = ()> { /* private fields */ }
Available on crate feature taffy_tree only.
Expand description

An entire tree of UI nodes. The entry point to Taffy’s high-level API.

Allows you to build a tree of UI nodes, run Taffy’s layout algorithms over that tree, and then access the resultant layout.]

Implementations§

Source§

impl<NodeContext> TaffyTree<NodeContext>

Source

pub fn new() -> Self

Creates a new TaffyTree

The default capacity of a TaffyTree is 16 nodes.

Examples found in repository?
examples/flexbox_gap.rs (line 7)
6fn main() -> Result<(), taffy::TaffyError> {
7    let mut taffy: TaffyTree<()> = TaffyTree::new();
8
9    let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10    let child0 = taffy.new_leaf(child_style.clone())?;
11    let child1 = taffy.new_leaf(child_style.clone())?;
12    let child2 = taffy.new_leaf(child_style.clone())?;
13
14    let root = taffy.new_with_children(
15        Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
16        &[child0, child1, child2],
17    )?;
18
19    // Compute layout and print result
20    taffy.compute_layout(root, Size::MAX_CONTENT)?;
21    taffy.print_tree(root);
22
23    Ok(())
24}
More examples
Hide additional examples
examples/basic.rs (line 4)
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}
examples/grid_holy_grail.rs (line 22)
19fn main() -> Result<(), taffy::TaffyError> {
20    use taffy::prelude::*;
21
22    let mut taffy: TaffyTree<()> = TaffyTree::new();
23
24    // Setup the grid
25    let root_style = Style {
26        display: Display::Grid,
27        size: Size { width: length(800.0), height: length(600.0) },
28        grid_template_columns: vec![length(250.0), fr(1.0), length(250.0)],
29        grid_template_rows: vec![length(150.0), fr(1.0), length(150.0)],
30        ..default()
31    };
32
33    // Define the child nodes
34    let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35    let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36    let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37    let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38    let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
39
40    // Create the container with the children
41    let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
42
43    // Compute layout and print result
44    taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
45    taffy.print_tree(root);
46
47    Ok(())
48}
examples/measure.rs (line 34)
33fn main() -> Result<(), taffy::TaffyError> {
34    let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
35
36    let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38    let text_node = taffy.new_leaf_with_context(
39        Style::default(),
40        NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41    )?;
42
43    let image_node = taffy
44        .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46    let root = taffy.new_with_children(
47        Style {
48            display: Display::Flex,
49            flex_direction: FlexDirection::Column,
50            size: Size { width: length(200.0), height: auto() },
51            ..Default::default()
52        },
53        &[text_node, image_node],
54    )?;
55
56    // Compute layout and print result
57    taffy.compute_layout_with_measure(
58        root,
59        Size::MAX_CONTENT,
60        // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
61        // For example, you may wish to borrow a global font registry and pass it into your text measuring function
62        |known_dimensions, available_space, _node_id, node_context, _style| {
63            measure_function(known_dimensions, available_space, node_context, &font_metrics)
64        },
65    )?;
66    taffy.print_tree(root);
67
68    Ok(())
69}
examples/nested.rs (line 4)
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
14            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(Style {
23        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
29            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        Style {
38            size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(
45        container,
46        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
47    )?;
48
49    println!("node: {:#?}", taffy.layout(container)?);
50
51    println!("div1: {:#?}", taffy.layout(div1)?);
52    println!("div2: {:#?}", taffy.layout(div2)?);
53
54    println!("child1: {:#?}", taffy.layout(child_t1)?);
55    println!("child2: {:#?}", taffy.layout(child_t2)?);
56
57    Ok(())
58}
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new TaffyTree that can store capacity nodes before reallocation

Source

pub fn enable_rounding(&mut self)

Enable rounding of layout values. Rounding is enabled by default.

Source

pub fn disable_rounding(&mut self)

Disable rounding of layout values. Rounding is enabled by default.

Source

pub fn new_leaf(&mut self, layout: Style) -> TaffyResult<NodeId>

Creates and adds a new unattached leaf node to the tree, and returns the node of the new node

Examples found in repository?
examples/flexbox_gap.rs (line 10)
6fn main() -> Result<(), taffy::TaffyError> {
7    let mut taffy: TaffyTree<()> = TaffyTree::new();
8
9    let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10    let child0 = taffy.new_leaf(child_style.clone())?;
11    let child1 = taffy.new_leaf(child_style.clone())?;
12    let child2 = taffy.new_leaf(child_style.clone())?;
13
14    let root = taffy.new_with_children(
15        Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
16        &[child0, child1, child2],
17    )?;
18
19    // Compute layout and print result
20    taffy.compute_layout(root, Size::MAX_CONTENT)?;
21    taffy.print_tree(root);
22
23    Ok(())
24}
More examples
Hide additional examples
examples/basic.rs (lines 6-9)
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}
examples/grid_holy_grail.rs (line 34)
19fn main() -> Result<(), taffy::TaffyError> {
20    use taffy::prelude::*;
21
22    let mut taffy: TaffyTree<()> = TaffyTree::new();
23
24    // Setup the grid
25    let root_style = Style {
26        display: Display::Grid,
27        size: Size { width: length(800.0), height: length(600.0) },
28        grid_template_columns: vec![length(250.0), fr(1.0), length(250.0)],
29        grid_template_rows: vec![length(150.0), fr(1.0), length(150.0)],
30        ..default()
31    };
32
33    // Define the child nodes
34    let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35    let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36    let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37    let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38    let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
39
40    // Create the container with the children
41    let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
42
43    // Compute layout and print result
44    taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
45    taffy.print_tree(root);
46
47    Ok(())
48}
examples/nested.rs (lines 7-10)
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
14            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(Style {
23        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
29            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        Style {
38            size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(
45        container,
46        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
47    )?;
48
49    println!("node: {:#?}", taffy.layout(container)?);
50
51    println!("div1: {:#?}", taffy.layout(div1)?);
52    println!("div2: {:#?}", taffy.layout(div2)?);
53
54    println!("child1: {:#?}", taffy.layout(child_t1)?);
55    println!("child2: {:#?}", taffy.layout(child_t2)?);
56
57    Ok(())
58}
Source

pub fn new_leaf_with_context( &mut self, layout: Style, context: NodeContext, ) -> TaffyResult<NodeId>

Creates and adds a new unattached leaf node to the tree, and returns the NodeId of the new node

Creates and adds a new leaf node with a supplied context

Examples found in repository?
examples/measure.rs (lines 38-41)
33fn main() -> Result<(), taffy::TaffyError> {
34    let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
35
36    let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38    let text_node = taffy.new_leaf_with_context(
39        Style::default(),
40        NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41    )?;
42
43    let image_node = taffy
44        .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46    let root = taffy.new_with_children(
47        Style {
48            display: Display::Flex,
49            flex_direction: FlexDirection::Column,
50            size: Size { width: length(200.0), height: auto() },
51            ..Default::default()
52        },
53        &[text_node, image_node],
54    )?;
55
56    // Compute layout and print result
57    taffy.compute_layout_with_measure(
58        root,
59        Size::MAX_CONTENT,
60        // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
61        // For example, you may wish to borrow a global font registry and pass it into your text measuring function
62        |known_dimensions, available_space, _node_id, node_context, _style| {
63            measure_function(known_dimensions, available_space, node_context, &font_metrics)
64        },
65    )?;
66    taffy.print_tree(root);
67
68    Ok(())
69}
Source

pub fn new_with_children( &mut self, layout: Style, children: &[NodeId], ) -> TaffyResult<NodeId>

Creates and adds a new node, which may have any number of children

Examples found in repository?
examples/flexbox_gap.rs (lines 14-17)
6fn main() -> Result<(), taffy::TaffyError> {
7    let mut taffy: TaffyTree<()> = TaffyTree::new();
8
9    let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10    let child0 = taffy.new_leaf(child_style.clone())?;
11    let child1 = taffy.new_leaf(child_style.clone())?;
12    let child2 = taffy.new_leaf(child_style.clone())?;
13
14    let root = taffy.new_with_children(
15        Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
16        &[child0, child1, child2],
17    )?;
18
19    // Compute layout and print result
20    taffy.compute_layout(root, Size::MAX_CONTENT)?;
21    taffy.print_tree(root);
22
23    Ok(())
24}
More examples
Hide additional examples
examples/basic.rs (lines 11-18)
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}
examples/grid_holy_grail.rs (line 41)
19fn main() -> Result<(), taffy::TaffyError> {
20    use taffy::prelude::*;
21
22    let mut taffy: TaffyTree<()> = TaffyTree::new();
23
24    // Setup the grid
25    let root_style = Style {
26        display: Display::Grid,
27        size: Size { width: length(800.0), height: length(600.0) },
28        grid_template_columns: vec![length(250.0), fr(1.0), length(250.0)],
29        grid_template_rows: vec![length(150.0), fr(1.0), length(150.0)],
30        ..default()
31    };
32
33    // Define the child nodes
34    let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35    let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36    let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37    let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38    let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
39
40    // Create the container with the children
41    let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
42
43    // Compute layout and print result
44    taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
45    taffy.print_tree(root);
46
47    Ok(())
48}
examples/measure.rs (lines 46-54)
33fn main() -> Result<(), taffy::TaffyError> {
34    let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
35
36    let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38    let text_node = taffy.new_leaf_with_context(
39        Style::default(),
40        NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41    )?;
42
43    let image_node = taffy
44        .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46    let root = taffy.new_with_children(
47        Style {
48            display: Display::Flex,
49            flex_direction: FlexDirection::Column,
50            size: Size { width: length(200.0), height: auto() },
51            ..Default::default()
52        },
53        &[text_node, image_node],
54    )?;
55
56    // Compute layout and print result
57    taffy.compute_layout_with_measure(
58        root,
59        Size::MAX_CONTENT,
60        // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
61        // For example, you may wish to borrow a global font registry and pass it into your text measuring function
62        |known_dimensions, available_space, _node_id, node_context, _style| {
63            measure_function(known_dimensions, available_space, node_context, &font_metrics)
64        },
65    )?;
66    taffy.print_tree(root);
67
68    Ok(())
69}
examples/nested.rs (lines 12-19)
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
14            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(Style {
23        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
29            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        Style {
38            size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(
45        container,
46        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
47    )?;
48
49    println!("node: {:#?}", taffy.layout(container)?);
50
51    println!("div1: {:#?}", taffy.layout(div1)?);
52    println!("div2: {:#?}", taffy.layout(div2)?);
53
54    println!("child1: {:#?}", taffy.layout(child_t1)?);
55    println!("child2: {:#?}", taffy.layout(child_t2)?);
56
57    Ok(())
58}
Source

pub fn clear(&mut self)

Drops all nodes in the tree

Source

pub fn remove(&mut self, node: NodeId) -> TaffyResult<NodeId>

Remove a specific node from the tree and drop it

Returns the id of the node removed.

Source

pub fn set_node_context( &mut self, node: NodeId, measure: Option<NodeContext>, ) -> TaffyResult<()>

Sets the context data associated with the node

Source

pub fn get_node_context(&self, node: NodeId) -> Option<&NodeContext>

Gets a reference to the the context data associated with the node

Source

pub fn get_node_context_mut(&mut self, node: NodeId) -> Option<&mut NodeContext>

Gets a mutable reference to the the context data associated with the node

Source

pub fn get_disjoint_node_context_mut<const N: usize>( &mut self, keys: [NodeId; N], ) -> Option<[&mut NodeContext; N]>

Gets mutable references to the the context data associated with the nodes. All keys must be valid and disjoint, otherwise None is returned.

Source

pub fn add_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<()>

Adds a child node under the supplied parent

Source

pub fn insert_child_at_index( &mut self, parent: NodeId, child_index: usize, child: NodeId, ) -> TaffyResult<()>

Inserts a child node at the given child_index under the supplied parent, shifting all children after it to the right.

Source

pub fn set_children( &mut self, parent: NodeId, children: &[NodeId], ) -> TaffyResult<()>

Directly sets the children of the supplied parent

Source

pub fn remove_child( &mut self, parent: NodeId, child: NodeId, ) -> TaffyResult<NodeId>

Removes the child of the parent node

The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.

Source

pub fn remove_child_at_index( &mut self, parent: NodeId, child_index: usize, ) -> TaffyResult<NodeId>

Removes the child at the given index from the parent

The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.

Source

pub fn remove_children_range<R>( &mut self, parent: NodeId, range: R, ) -> TaffyResult<()>
where R: RangeBounds<usize>,

Removes children at the given range from the parent

Children are not removed from the tree entirely, they are simply no longer attached to their previous parent.

Function will panic if given range is invalid. See core::slice::range

Source

pub fn replace_child_at_index( &mut self, parent: NodeId, child_index: usize, new_child: NodeId, ) -> TaffyResult<NodeId>

Replaces the child at the given child_index from the parent node with the new child node

The child is not removed from the tree entirely, it is simply no longer attached to its previous parent.

Source

pub fn child_at_index( &self, parent: NodeId, child_index: usize, ) -> TaffyResult<NodeId>

Returns the child node of the parent node at the provided child_index

Source

pub fn total_node_count(&self) -> usize

Returns the total number of nodes in the tree

Source

pub fn parent(&self, child_id: NodeId) -> Option<NodeId>

Returns the NodeId of the parent node of the specified node (if it exists)

  • Return None if the specified node has no parent
  • Panics if the specified node does not exist
Source

pub fn children(&self, parent: NodeId) -> TaffyResult<Vec<NodeId>>

Returns a list of children that belong to the parent node

Source

pub fn set_style(&mut self, node: NodeId, style: Style) -> TaffyResult<()>

Sets the Style of the provided node

Source

pub fn style(&self, node: NodeId) -> TaffyResult<&Style>

Gets the Style of the provided node

Source

pub fn layout(&self, node: NodeId) -> TaffyResult<&Layout>

Return this node layout relative to its parent

Examples found in repository?
examples/basic.rs (line 25)
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}
More examples
Hide additional examples
examples/nested.rs (line 49)
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
14            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(Style {
23        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
29            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        Style {
38            size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(
45        container,
46        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
47    )?;
48
49    println!("node: {:#?}", taffy.layout(container)?);
50
51    println!("div1: {:#?}", taffy.layout(div1)?);
52    println!("div2: {:#?}", taffy.layout(div2)?);
53
54    println!("child1: {:#?}", taffy.layout(child_t1)?);
55    println!("child2: {:#?}", taffy.layout(child_t2)?);
56
57    Ok(())
58}
Source

pub fn unrounded_layout(&self, node: NodeId) -> &Layout

Returns this node layout with unrounded values relative to its parent.

Source

pub fn detailed_layout_info(&self, node_id: NodeId) -> &DetailedLayoutInfo

Available on crate feature detailed_layout_info only.

Get the “detailed layout info” for a node.

Currently this is only implemented for CSS Grid containers where it contains the computed size of each grid track and the computed placement of each grid item

Source

pub fn mark_dirty(&mut self, node: NodeId) -> TaffyResult<()>

Marks the layout of this node and its ancestors as outdated

Source

pub fn dirty(&self, node: NodeId) -> TaffyResult<bool>

Indicates whether the layout of this node needs to be recomputed

Source

pub fn compute_layout_with_measure<MeasureFunction>( &mut self, node_id: NodeId, available_space: Size<AvailableSpace>, measure_function: MeasureFunction, ) -> Result<(), TaffyError>

Updates the stored layout of the provided node and its children

Examples found in repository?
examples/measure.rs (lines 57-65)
33fn main() -> Result<(), taffy::TaffyError> {
34    let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
35
36    let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38    let text_node = taffy.new_leaf_with_context(
39        Style::default(),
40        NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41    )?;
42
43    let image_node = taffy
44        .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46    let root = taffy.new_with_children(
47        Style {
48            display: Display::Flex,
49            flex_direction: FlexDirection::Column,
50            size: Size { width: length(200.0), height: auto() },
51            ..Default::default()
52        },
53        &[text_node, image_node],
54    )?;
55
56    // Compute layout and print result
57    taffy.compute_layout_with_measure(
58        root,
59        Size::MAX_CONTENT,
60        // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
61        // For example, you may wish to borrow a global font registry and pass it into your text measuring function
62        |known_dimensions, available_space, _node_id, node_context, _style| {
63            measure_function(known_dimensions, available_space, node_context, &font_metrics)
64        },
65    )?;
66    taffy.print_tree(root);
67
68    Ok(())
69}
Source

pub fn compute_layout( &mut self, node: NodeId, available_space: Size<AvailableSpace>, ) -> Result<(), TaffyError>

Updates the stored layout of the provided node and its children

Examples found in repository?
examples/flexbox_gap.rs (line 20)
6fn main() -> Result<(), taffy::TaffyError> {
7    let mut taffy: TaffyTree<()> = TaffyTree::new();
8
9    let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10    let child0 = taffy.new_leaf(child_style.clone())?;
11    let child1 = taffy.new_leaf(child_style.clone())?;
12    let child2 = taffy.new_leaf(child_style.clone())?;
13
14    let root = taffy.new_with_children(
15        Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
16        &[child0, child1, child2],
17    )?;
18
19    // Compute layout and print result
20    taffy.compute_layout(root, Size::MAX_CONTENT)?;
21    taffy.print_tree(root);
22
23    Ok(())
24}
More examples
Hide additional examples
examples/basic.rs (lines 21-24)
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}
examples/grid_holy_grail.rs (line 44)
19fn main() -> Result<(), taffy::TaffyError> {
20    use taffy::prelude::*;
21
22    let mut taffy: TaffyTree<()> = TaffyTree::new();
23
24    // Setup the grid
25    let root_style = Style {
26        display: Display::Grid,
27        size: Size { width: length(800.0), height: length(600.0) },
28        grid_template_columns: vec![length(250.0), fr(1.0), length(250.0)],
29        grid_template_rows: vec![length(150.0), fr(1.0), length(150.0)],
30        ..default()
31    };
32
33    // Define the child nodes
34    let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35    let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36    let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37    let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38    let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
39
40    // Create the container with the children
41    let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
42
43    // Compute layout and print result
44    taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
45    taffy.print_tree(root);
46
47    Ok(())
48}
examples/nested.rs (lines 44-47)
3fn main() -> Result<(), taffy::TaffyError> {
4    let mut taffy: TaffyTree<()> = TaffyTree::new();
5
6    // left
7    let child_t1 = taffy.new_leaf(Style {
8        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
9        ..Default::default()
10    })?;
11
12    let div1 = taffy.new_with_children(
13        Style {
14            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
15            // justify_content: JustifyContent::Center,
16            ..Default::default()
17        },
18        &[child_t1],
19    )?;
20
21    // right
22    let child_t2 = taffy.new_leaf(Style {
23        size: Size { width: Dimension::from_length(5.0), height: Dimension::from_length(5.0) },
24        ..Default::default()
25    })?;
26
27    let div2 = taffy.new_with_children(
28        Style {
29            size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
30            // justify_content: JustifyContent::Center,
31            ..Default::default()
32        },
33        &[child_t2],
34    )?;
35
36    let container = taffy.new_with_children(
37        Style {
38            size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
39            ..Default::default()
40        },
41        &[div1, div2],
42    )?;
43
44    taffy.compute_layout(
45        container,
46        Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
47    )?;
48
49    println!("node: {:#?}", taffy.layout(container)?);
50
51    println!("div1: {:#?}", taffy.layout(div1)?);
52    println!("div2: {:#?}", taffy.layout(div2)?);
53
54    println!("child1: {:#?}", taffy.layout(child_t1)?);
55    println!("child2: {:#?}", taffy.layout(child_t2)?);
56
57    Ok(())
58}
Source

pub fn print_tree(&mut self, root: NodeId)

Available on crate feature std only.

Prints a debug representation of the tree’s layout

Examples found in repository?
examples/flexbox_gap.rs (line 21)
6fn main() -> Result<(), taffy::TaffyError> {
7    let mut taffy: TaffyTree<()> = TaffyTree::new();
8
9    let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10    let child0 = taffy.new_leaf(child_style.clone())?;
11    let child1 = taffy.new_leaf(child_style.clone())?;
12    let child2 = taffy.new_leaf(child_style.clone())?;
13
14    let root = taffy.new_with_children(
15        Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
16        &[child0, child1, child2],
17    )?;
18
19    // Compute layout and print result
20    taffy.compute_layout(root, Size::MAX_CONTENT)?;
21    taffy.print_tree(root);
22
23    Ok(())
24}
More examples
Hide additional examples
examples/grid_holy_grail.rs (line 45)
19fn main() -> Result<(), taffy::TaffyError> {
20    use taffy::prelude::*;
21
22    let mut taffy: TaffyTree<()> = TaffyTree::new();
23
24    // Setup the grid
25    let root_style = Style {
26        display: Display::Grid,
27        size: Size { width: length(800.0), height: length(600.0) },
28        grid_template_columns: vec![length(250.0), fr(1.0), length(250.0)],
29        grid_template_rows: vec![length(150.0), fr(1.0), length(150.0)],
30        ..default()
31    };
32
33    // Define the child nodes
34    let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35    let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36    let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37    let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38    let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
39
40    // Create the container with the children
41    let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
42
43    // Compute layout and print result
44    taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
45    taffy.print_tree(root);
46
47    Ok(())
48}
examples/measure.rs (line 66)
33fn main() -> Result<(), taffy::TaffyError> {
34    let mut taffy: TaffyTree<NodeContext> = TaffyTree::new();
35
36    let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38    let text_node = taffy.new_leaf_with_context(
39        Style::default(),
40        NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41    )?;
42
43    let image_node = taffy
44        .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46    let root = taffy.new_with_children(
47        Style {
48            display: Display::Flex,
49            flex_direction: FlexDirection::Column,
50            size: Size { width: length(200.0), height: auto() },
51            ..Default::default()
52        },
53        &[text_node, image_node],
54    )?;
55
56    // Compute layout and print result
57    taffy.compute_layout_with_measure(
58        root,
59        Size::MAX_CONTENT,
60        // Note: this closure is a FnMut closure and can be used to borrow external context for the duration of layout
61        // For example, you may wish to borrow a global font registry and pass it into your text measuring function
62        |known_dimensions, available_space, _node_id, node_context, _style| {
63            measure_function(known_dimensions, available_space, node_context, &font_metrics)
64        },
65    )?;
66    taffy.print_tree(root);
67
68    Ok(())
69}

Trait Implementations§

Source§

impl<NodeContext> CacheTree for TaffyTree<NodeContext>

Source§

fn cache_get( &self, node_id: NodeId, input: &LayoutInput, ) -> Option<LayoutOutput>

Try to retrieve a cached result from the cache
Source§

fn cache_store( &mut self, node_id: NodeId, input: &LayoutInput, layout_output: LayoutOutput, )

Store a computed size in the cache
Source§

fn cache_clear(&mut self, node_id: NodeId)

Clear all cache entries for the node
Source§

impl<NodeContext: Clone> Clone for TaffyTree<NodeContext>

Source§

fn clone(&self) -> TaffyTree<NodeContext>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<NodeContext: Debug> Debug for TaffyTree<NodeContext>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TaffyTree

Source§

fn default() -> TaffyTree<()>

Returns the “default value” for a type. Read more
Source§

impl<NodeContext> PrintTree for TaffyTree<NodeContext>

Source§

fn get_debug_label(&self, node_id: NodeId) -> &'static str

Get a debug label for the node (typically the type of node: flexbox, grid, text, image, etc)
Source§

fn get_final_layout(&self, node_id: NodeId) -> Layout

Get a reference to the node’s final layout
Source§

impl<NodeContext> TraversePartialTree for TaffyTree<NodeContext>

Source§

type ChildIter<'a> = TaffyTreeChildIter<'a> where Self: 'a

Type representing an iterator of the children of a node
Source§

fn child_ids(&self, parent_node_id: NodeId) -> Self::ChildIter<'_>

Get the list of children IDs for the given node
Source§

fn child_count(&self, parent_node_id: NodeId) -> usize

Get the number of children for the given node
Source§

fn get_child_id(&self, parent_node_id: NodeId, id: usize) -> NodeId

Get a specific child of a node, where the index represents the nth child
Source§

impl<NodeContext> TraverseTree for TaffyTree<NodeContext>

Auto Trait Implementations§

§

impl<NodeContext = ()> !Send for TaffyTree<NodeContext>

§

impl<NodeContext = ()> !Sync for TaffyTree<NodeContext>

§

impl<NodeContext> Freeze for TaffyTree<NodeContext>

§

impl<NodeContext> RefUnwindSafe for TaffyTree<NodeContext>
where NodeContext: RefUnwindSafe,

§

impl<NodeContext> Unpin for TaffyTree<NodeContext>
where NodeContext: Unpin,

§

impl<NodeContext> UnsafeUnpin for TaffyTree<NodeContext>

§

impl<NodeContext> UnwindSafe for TaffyTree<NodeContext>
where NodeContext: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.