#[test] #[allow(non_snake_case)] fn grid_margins_percent_stretch__border_box() { #[allow(unused_imports)] use taffy::{prelude::*, Layout}; let mut taffy = crate::new_test_tree(); let node0 = taffy .new_leaf(taffy::style::Style { align_self: Some(taffy::style::AlignSelf::Stretch), justify_self: Some(taffy::style::JustifySelf::Stretch), size: taffy::geometry::Size { width: taffy::style::Dimension::from_length(20f32), height: taffy::style::Dimension::from_length(20f32), }, margin: taffy::geometry::Rect { left: percent(0.2f32), right: percent(0.1f32), top: percent(0.05f32), bottom: percent(0.15f32), }, ..Default::default() }) .unwrap(); let node1 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap(); let node2 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap(); let node3 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap(); let node4 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap(); let node5 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap(); let node = taffy .new_with_children( taffy::style::Style { display: taffy::style::Display::Grid, grid_template_rows: vec![length(40f32), length(40f32), length(40f32)], grid_template_columns: vec![length(20f32), length(20f32), length(20f32)], ..Default::default() }, &[node0, node1, node2, node3, node4, node5], ) .unwrap(); taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap(); println!("\nComputed tree:"); taffy.print_tree(node); println!(); let layout = taffy.layout(node).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 60f32, "width of node {:?}. Expected {}. Actual {}", node, 60f32, size.width); assert_eq!(size.height, 120f32, "height of node {:?}. Expected {}. Actual {}", node, 120f32, size.height); assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y); let layout = taffy.layout(node0).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0, 20f32, size.width); assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0, 20f32, size.height); assert_eq!(location.x, 4f32, "x of node {:?}. Expected {}. Actual {}", node0, 4f32, location.x); assert_eq!(location.y, 1f32, "y of node {:?}. Expected {}. Actual {}", node0, 1f32, location.y); let layout = taffy.layout(node1).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node1, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node1, 40f32, size.height); assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node1, 20f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node1, 0f32, location.y); let layout = taffy.layout(node2).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node2, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height); assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node2, 40f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node2, 0f32, location.y); let layout = taffy.layout(node3).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node3, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height); assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node3, 0f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node3, 40f32, location.y); let layout = taffy.layout(node4).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node4, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height); assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node4, 20f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node4, 40f32, location.y); let layout = taffy.layout(node5).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node5, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height); assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node5, 40f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node5, 40f32, location.y); } #[test] #[allow(non_snake_case)] fn grid_margins_percent_stretch__content_box() { #[allow(unused_imports)] use taffy::{prelude::*, Layout}; let mut taffy = crate::new_test_tree(); let node0 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, align_self: Some(taffy::style::AlignSelf::Stretch), justify_self: Some(taffy::style::JustifySelf::Stretch), size: taffy::geometry::Size { width: taffy::style::Dimension::from_length(20f32), height: taffy::style::Dimension::from_length(20f32), }, margin: taffy::geometry::Rect { left: percent(0.2f32), right: percent(0.1f32), top: percent(0.05f32), bottom: percent(0.15f32), }, ..Default::default() }) .unwrap(); let node1 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() }) .unwrap(); let node2 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() }) .unwrap(); let node3 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() }) .unwrap(); let node4 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() }) .unwrap(); let node5 = taffy .new_leaf(taffy::style::Style { box_sizing: taffy::style::BoxSizing::ContentBox, ..Default::default() }) .unwrap(); let node = taffy .new_with_children( taffy::style::Style { display: taffy::style::Display::Grid, box_sizing: taffy::style::BoxSizing::ContentBox, grid_template_rows: vec![length(40f32), length(40f32), length(40f32)], grid_template_columns: vec![length(20f32), length(20f32), length(20f32)], ..Default::default() }, &[node0, node1, node2, node3, node4, node5], ) .unwrap(); taffy.compute_layout_with_measure(node, taffy::geometry::Size::MAX_CONTENT, crate::test_measure_function).unwrap(); println!("\nComputed tree:"); taffy.print_tree(node); println!(); let layout = taffy.layout(node).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 60f32, "width of node {:?}. Expected {}. Actual {}", node, 60f32, size.width); assert_eq!(size.height, 120f32, "height of node {:?}. Expected {}. Actual {}", node, 120f32, size.height); assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y); let layout = taffy.layout(node0).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node0, 20f32, size.width); assert_eq!(size.height, 20f32, "height of node {:?}. Expected {}. Actual {}", node0, 20f32, size.height); assert_eq!(location.x, 4f32, "x of node {:?}. Expected {}. Actual {}", node0, 4f32, location.x); assert_eq!(location.y, 1f32, "y of node {:?}. Expected {}. Actual {}", node0, 1f32, location.y); let layout = taffy.layout(node1).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node1, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node1, 40f32, size.height); assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node1, 20f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node1, 0f32, location.y); let layout = taffy.layout(node2).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node2, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node2, 40f32, size.height); assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node2, 40f32, location.x); assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node2, 0f32, location.y); let layout = taffy.layout(node3).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node3, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node3, 40f32, size.height); assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node3, 0f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node3, 40f32, location.y); let layout = taffy.layout(node4).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node4, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node4, 40f32, size.height); assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node4, 20f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node4, 40f32, location.y); let layout = taffy.layout(node5).unwrap(); let Layout { size, location, .. } = layout; assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node5, 20f32, size.width); assert_eq!(size.height, 40f32, "height of node {:?}. Expected {}. Actual {}", node5, 40f32, size.height); assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node5, 40f32, location.x); assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node5, 40f32, location.y); }