gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5364Rust 86Markdown 12TOML 91841 Sutichaykuna

⎇ 23ff477566b0239f04d3b89a2b3a4da2495e9577 src/compute/mod.rs Rust · 91 siq'ikuna · 3.0 KB Ñawpa willay Pi imata llamkarqan ch'uya

Remove `LayoutAlgorithm` trait (#562) daa07e0f4 · Nico Burns ·

1//! The layout algorithms themselves
2
3pub(crate) mod common;
4pub(crate) mod leaf;
5
6#[cfg(feature = "block_layout")]
7pub(crate) mod block;
8
9#[cfg(feature = "flexbox")]
10pub(crate) mod flexbox;
11
12#[cfg(feature = "grid")]
13pub(crate) mod grid;
14
15pub use leaf::compute_leaf_layout;
16
17#[cfg(feature = "block_layout")]
18pub use self::block::compute_block_layout;
19
20#[cfg(feature = "flexbox")]
21pub use self::flexbox::compute_flexbox_layout;
22
23#[cfg(feature = "grid")]
24pub use self::grid::compute_grid_layout;
25
26#[cfg(feature = "taffy_tree")]
27pub(crate) mod taffy_tree;
28
29use crate::tree::{Layout, LayoutOutput, LayoutTree, NodeId};
30
31/// Creates a layout for this node and its children, recursively.
32/// Each hidden node has zero size and is placed at the origin
33pub fn compute_hidden_layout(tree: &mut impl LayoutTree, node: NodeId) -> LayoutOutput {
34    /// Recursive function to apply hidden layout to all descendents
35    fn perform_hidden_layout_inner(tree: &mut impl LayoutTree, node: NodeId, order: u32) {
36        *tree.layout_mut(node) = Layout::with_order(order);
37        for order in 0..tree.child_count(node) {
38            perform_hidden_layout_inner(tree, tree.child(node, order), order as _);
39        }
40    }
41
42    for order in 0..tree.child_count(node) {
43        perform_hidden_layout_inner(tree, tree.child(node, order), order as _);
44    }
45
46    LayoutOutput::HIDDEN
47}
48
49#[cfg(test)]
50mod tests {
51    use super::compute_hidden_layout;
52    use crate::geometry::{Point, Size};
53    use crate::style::{Display, Style};
54    use crate::tree::TaffyView;
55    use crate::Taffy;
56
57    #[test]
58    fn hidden_layout_should_hide_recursively() {
59        let mut taffy: Taffy<()> = Taffy::new();
60
61        let style: Style = Style { display: Display::Flex, size: Size::from_lengths(50.0, 50.0), ..Default::default() };
62
63        let grandchild_00 = taffy.new_leaf(style.clone()).unwrap();
64        let grandchild_01 = taffy.new_leaf(style.clone()).unwrap();
65        let child_00 = taffy.new_with_children(style.clone(), &[grandchild_00, grandchild_01]).unwrap();
66
67        let grandchild_02 = taffy.new_leaf(style.clone()).unwrap();
68        let child_01 = taffy.new_with_children(style.clone(), &[grandchild_02]).unwrap();
69
70        let root = taffy
71            .new_with_children(
72                Style { display: Display::None, size: Size::from_lengths(50.0, 50.0), ..Default::default() },
73                &[child_00, child_01],
74            )
75            .unwrap();
76
77        compute_hidden_layout(
78            &mut TaffyView { taffy: &mut taffy, measure_function: |_, _, _, _| Size::ZERO },
79            root.into(),
80        );
81
82        // Whatever size and display-mode the nodes had previously,
83        // all layouts should resolve to ZERO due to the root's DISPLAY::NONE
84        for (node, _) in taffy.nodes.iter().filter(|(node, _)| *node != root.into()) {
85            if let Ok(layout) = taffy.layout(node.into()) {
86                assert_eq!(layout.size, Size::zero());
87                assert_eq!(layout.location, Point::zero());
88            }
89        }
90    }
91}