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
cbd5d1dc7ea905f0672192f034a7793c66529448 · by Yurii Nakonechnyi · 2023-06-25T21:46:10Z (3 years ago) · parent 868b85d2e · browse the tree at this commit
Semantic delta · 1
src/tree/taffy_tree/tree.rs
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<f32>,
- /// The bottom-left corner of the node
+ /// The top-left corner of the node
pub location: Point<f32>,
}
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);
+ }
}