pub struct TaffyTree<NodeContext = ()> { /* private fields */ }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>
impl<NodeContext> TaffyTree<NodeContext>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
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
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}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}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}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}Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new TaffyTree that can store capacity nodes before reallocation
Sourcepub fn enable_rounding(&mut self)
pub fn enable_rounding(&mut self)
Enable rounding of layout values. Rounding is enabled by default.
Sourcepub fn disable_rounding(&mut self)
pub fn disable_rounding(&mut self)
Disable rounding of layout values. Rounding is enabled by default.
Sourcepub fn new_leaf(&mut self, layout: Style) -> TaffyResult<NodeId>
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?
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
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}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}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}Sourcepub fn new_leaf_with_context(
&mut self,
layout: Style,
context: NodeContext,
) -> TaffyResult<NodeId>
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?
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}Sourcepub fn new_with_children(
&mut self,
layout: Style,
children: &[NodeId],
) -> TaffyResult<NodeId>
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?
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
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}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}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}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}Sourcepub fn remove(&mut self, node: NodeId) -> TaffyResult<NodeId>
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.
Sourcepub fn set_node_context(
&mut self,
node: NodeId,
measure: Option<NodeContext>,
) -> TaffyResult<()>
pub fn set_node_context( &mut self, node: NodeId, measure: Option<NodeContext>, ) -> TaffyResult<()>
Sets the context data associated with the node
Sourcepub fn get_node_context(&self, node: NodeId) -> Option<&NodeContext>
pub fn get_node_context(&self, node: NodeId) -> Option<&NodeContext>
Gets a reference to the the context data associated with the node
Sourcepub fn get_node_context_mut(&mut self, node: NodeId) -> Option<&mut NodeContext>
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
Sourcepub fn get_disjoint_node_context_mut<const N: usize>(
&mut self,
keys: [NodeId; N],
) -> Option<[&mut NodeContext; N]>
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.
Sourcepub fn add_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<()>
pub fn add_child(&mut self, parent: NodeId, child: NodeId) -> TaffyResult<()>
Adds a child node under the supplied parent
Sourcepub fn insert_child_at_index(
&mut self,
parent: NodeId,
child_index: usize,
child: NodeId,
) -> TaffyResult<()>
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.
Sourcepub fn set_children(
&mut self,
parent: NodeId,
children: &[NodeId],
) -> TaffyResult<()>
pub fn set_children( &mut self, parent: NodeId, children: &[NodeId], ) -> TaffyResult<()>
Directly sets the children of the supplied parent
Sourcepub fn remove_child(
&mut self,
parent: NodeId,
child: NodeId,
) -> TaffyResult<NodeId>
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.
Sourcepub fn remove_child_at_index(
&mut self,
parent: NodeId,
child_index: usize,
) -> TaffyResult<NodeId>
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.
Sourcepub fn remove_children_range<R>(
&mut self,
parent: NodeId,
range: R,
) -> TaffyResult<()>where
R: RangeBounds<usize>,
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
Sourcepub fn replace_child_at_index(
&mut self,
parent: NodeId,
child_index: usize,
new_child: NodeId,
) -> TaffyResult<NodeId>
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.
Sourcepub fn child_at_index(
&self,
parent: NodeId,
child_index: usize,
) -> TaffyResult<NodeId>
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
Sourcepub fn total_node_count(&self) -> usize
pub fn total_node_count(&self) -> usize
Returns the total number of nodes in the tree
Sourcepub fn parent(&self, child_id: NodeId) -> Option<NodeId>
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
Sourcepub fn children(&self, parent: NodeId) -> TaffyResult<Vec<NodeId>>
pub fn children(&self, parent: NodeId) -> TaffyResult<Vec<NodeId>>
Returns a list of children that belong to the parent node
Sourcepub fn set_style(&mut self, node: NodeId, style: Style) -> TaffyResult<()>
pub fn set_style(&mut self, node: NodeId, style: Style) -> TaffyResult<()>
Sets the Style of the provided node
Sourcepub fn layout(&self, node: NodeId) -> TaffyResult<&Layout>
pub fn layout(&self, node: NodeId) -> TaffyResult<&Layout>
Return this node layout relative to its parent
Examples found in repository?
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
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}Sourcepub fn unrounded_layout(&self, node: NodeId) -> &Layout
pub fn unrounded_layout(&self, node: NodeId) -> &Layout
Returns this node layout with unrounded values relative to its parent.
Sourcepub fn detailed_layout_info(&self, node_id: NodeId) -> &DetailedLayoutInfo
Available on crate feature detailed_layout_info only.
pub fn detailed_layout_info(&self, node_id: NodeId) -> &DetailedLayoutInfo
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
Sourcepub fn mark_dirty(&mut self, node: NodeId) -> TaffyResult<()>
pub fn mark_dirty(&mut self, node: NodeId) -> TaffyResult<()>
Marks the layout of this node and its ancestors as outdated
Sourcepub fn dirty(&self, node: NodeId) -> TaffyResult<bool>
pub fn dirty(&self, node: NodeId) -> TaffyResult<bool>
Indicates whether the layout of this node needs to be recomputed
Sourcepub fn compute_layout_with_measure<MeasureFunction>(
&mut self,
node_id: NodeId,
available_space: Size<AvailableSpace>,
measure_function: MeasureFunction,
) -> Result<(), TaffyError>
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?
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}Sourcepub fn compute_layout(
&mut self,
node: NodeId,
available_space: Size<AvailableSpace>,
) -> Result<(), TaffyError>
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?
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
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}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}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}Sourcepub fn print_tree(&mut self, root: NodeId)
Available on crate feature std only.
pub fn print_tree(&mut self, root: NodeId)
std only.Prints a debug representation of the tree’s layout
Examples found in repository?
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
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}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}