Skip to content

Commit

Permalink
Minor: changed docs for Layout::location (bottom-left -> top-left) & …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
inobelar committed Jun 25, 2023
1 parent 868b85d commit cbd5d1d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tree/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand Down
44 changes: 44 additions & 0 deletions src/tree/taffy_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit cbd5d1d

Please sign in to comment.