From cbd5d1dc7ea905f0672192f034a7793c66529448 Mon Sep 17 00:00:00 2001 From: Yurii Nakonechnyi Date: Mon, 26 Jun 2023 00:46:10 +0300 Subject: [PATCH] Minor: changed docs for Layout::location (bottom-left -> top-left) & added test (#500) * Minor: changed docs for Layout::location (bottom-left -> top-left) & added test * Minor: in test::make_sure_layout_location_is_top_left changed child-node size to 100% to fully fill parent * formatting --- src/tree/layout.rs | 2 +- src/tree/taffy_tree/tree.rs | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/tree/layout.rs b/src/tree/layout.rs index 9d10cf045..8af8f8469 100644 --- a/src/tree/layout.rs +++ b/src/tree/layout.rs @@ -137,7 +137,7 @@ pub struct Layout { pub order: u32, /// The width and height of the node pub size: Size, - /// The bottom-left corner of the node + /// The top-left corner of the node pub location: Point, } diff --git a/src/tree/taffy_tree/tree.rs b/src/tree/taffy_tree/tree.rs index 060bb3519..a55e6f334 100644 --- a/src/tree/taffy_tree/tree.rs +++ b/src/tree/taffy_tree/tree.rs @@ -788,4 +788,48 @@ mod tests { ); assert!(layout_result.is_ok()); } + + #[test] + fn make_sure_layout_location_is_top_left() { + use crate::prelude::Rect; + + let mut taffy = Taffy::new(); + + let node = taffy + .new_leaf(Style { + size: Size { width: Dimension::Percent(1f32), height: Dimension::Percent(1f32) }, + ..Default::default() + }) + .unwrap(); + + let root = taffy + .new_with_children( + Style { + size: Size { width: Dimension::Length(100f32), height: Dimension::Length(100f32) }, + padding: Rect { + left: length(10f32), + right: length(20f32), + top: length(30f32), + bottom: length(40f32), + }, + ..Default::default() + }, + &[node], + ) + .unwrap(); + + taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(); + + // If Layout::location represents top-left coord, 'node' location + // must be (due applied 'root' padding): {x: 10, y: 30}. + // + // It's important, since result will be different for each other + // coordinate space: + // - bottom-left: {x: 10, y: 40} + // - top-right: {x: 20, y: 30} + // - bottom-right: {x: 20, y: 40} + let layout = taffy.layout(node).unwrap(); + assert_eq!(layout.location.x, 10f32); + assert_eq!(layout.location.y, 30f32); + } }