use taffy::prelude::*; use taffy_test_helpers::new_test_tree; #[test] fn relayout() { let mut taffy = new_test_tree(); let node1 = taffy .new_leaf(taffy::style::Style { size: taffy::geometry::Size { width: length(8.0), height: length(80.0) }, ..Default::default() }) .unwrap(); let node0 = taffy .new_with_children( taffy::style::Style { align_self: Some(taffy::prelude::AlignSelf::CENTER), size: taffy::geometry::Size { width: Dimension::AUTO, height: Dimension::AUTO }, // size: taffy::geometry::Size { width: Dimension::Percent(1.0), height: Dimension::Percent(1.0) }, ..Default::default() }, &[node1], ) .unwrap(); let node = taffy .new_with_children( taffy::style::Style { size: taffy::geometry::Size { width: Dimension::from_percent(1f32), height: Dimension::from_percent(1f32), }, ..Default::default() }, &[node0], ) .unwrap(); taffy .compute_layout( node, taffy::geometry::Size { width: AvailableSpace::Definite(100f32), height: AvailableSpace::Definite(100f32) }, ) .unwrap(); let initial = taffy.layout(node).unwrap().location; let initial0 = taffy.layout(node0).unwrap().location; let initial1 = taffy.layout(node1).unwrap().location; for _ in 1..10 { taffy .compute_layout( node, taffy::geometry::Size { width: AvailableSpace::Definite(100f32), height: AvailableSpace::Definite(100f32), }, ) .unwrap(); assert_eq!(taffy.layout(node).unwrap().location, initial); assert_eq!(taffy.layout(node0).unwrap().location, initial0); assert_eq!(taffy.layout(node1).unwrap().location, initial1); } } #[test] fn toggle_root_display_none() { let hidden_style = Style { display: Display::None, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; let flex_style = Style { display: Display::Flex, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; // Setup let mut taffy = new_test_tree(); let node = taffy.new_leaf(hidden_style.clone()).unwrap(); // Layout 1 (None) taffy.compute_layout(node, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); // Layout 2 (Flex) taffy.set_style(node, flex_style).unwrap(); taffy.compute_layout(node, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 100.0); assert_eq!(layout.size.height, 100.0); // Layout 3 (None) taffy.set_style(node, hidden_style).unwrap(); taffy.compute_layout(node, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); } #[test] fn toggle_root_display_none_with_children() { use taffy::prelude::*; let mut taffy = new_test_tree(); let child = taffy .new_leaf(Style { size: Size { width: length(800.0), height: length(100.0) }, ..Default::default() }) .unwrap(); let parent = taffy .new_with_children( Style { size: Size { width: length(800.0), height: length(100.0) }, ..Default::default() }, &[child], ) .unwrap(); let root = taffy.new_with_children(Style::default(), &[parent]).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); assert_eq!(taffy.layout(child).unwrap().size.width, 800.0); assert_eq!(taffy.layout(child).unwrap().size.height, 100.0); taffy.set_style(root, Style { display: Display::None, ..Default::default() }).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); assert_eq!(taffy.layout(child).unwrap().size.width, 0.0); assert_eq!(taffy.layout(child).unwrap().size.height, 0.0); taffy.set_style(root, Style::default()).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); assert_eq!(taffy.layout(parent).unwrap().size.width, 800.0); assert_eq!(taffy.layout(parent).unwrap().size.height, 100.0); assert_eq!(taffy.layout(child).unwrap().size.width, 800.0); assert_eq!(taffy.layout(child).unwrap().size.height, 100.0); } #[test] fn toggle_flex_child_display_none() { let hidden_style = Style { display: Display::None, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; let flex_style = Style { display: Display::Flex, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; // Setup let mut taffy = new_test_tree(); let node = taffy.new_leaf(hidden_style.clone()).unwrap(); let root = taffy.new_with_children(flex_style.clone(), &[node]).unwrap(); // Layout 1 (None) taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); // Layout 2 (Flex) taffy.set_style(node, flex_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 100.0); assert_eq!(layout.size.height, 100.0); // Layout 3 (None) taffy.set_style(node, hidden_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); } #[test] fn toggle_flex_container_display_none() { let hidden_style = Style { display: Display::None, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; let flex_style = Style { display: Display::Flex, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; // Setup let mut taffy = new_test_tree(); let node = taffy.new_leaf(hidden_style.clone()).unwrap(); let root = taffy.new_with_children(hidden_style.clone(), &[node]).unwrap(); // Layout 1 (None) taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); // Layout 2 (Flex) taffy.set_style(root, flex_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 100.0); assert_eq!(layout.size.height, 100.0); // Layout 3 (None) taffy.set_style(root, hidden_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); } #[test] fn toggle_grid_child_display_none() { let hidden_style = Style { display: Display::None, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; let grid_style = Style { display: Display::Grid, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; // Setup let mut taffy = new_test_tree(); let node = taffy.new_leaf(hidden_style.clone()).unwrap(); let root = taffy.new_with_children(grid_style.clone(), &[node]).unwrap(); // Layout 1 (None) taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); // Layout 2 (Flex) taffy.set_style(node, grid_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 100.0); assert_eq!(layout.size.height, 100.0); // Layout 3 (None) taffy.set_style(node, hidden_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(node).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); } #[test] fn toggle_grid_container_display_none() { let hidden_style = Style { display: Display::None, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; let grid_style = Style { display: Display::Grid, size: Size { width: length(100.0), height: length(100.0) }, ..Default::default() }; // Setup let mut taffy = new_test_tree(); let node = taffy.new_leaf(hidden_style.clone()).unwrap(); let root = taffy.new_with_children(hidden_style.clone(), &[node]).unwrap(); // Layout 1 (None) taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); // Layout 2 (Flex) taffy.set_style(root, grid_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 100.0); assert_eq!(layout.size.height, 100.0); // Layout 3 (None) taffy.set_style(root, hidden_style).unwrap(); taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); let layout = taffy.layout(root).unwrap(); assert_eq!(layout.location.x, 0.0); assert_eq!(layout.location.y, 0.0); assert_eq!(layout.size.width, 0.0); assert_eq!(layout.size.height, 0.0); } #[test] fn relayout_is_stable_with_rounding() { let mut taffy = new_test_tree(); taffy.enable_rounding(); //