use rux_layout::*;
fn boxed(style: Style, children: Vec<Node>) -> Node {
let mut n = Node::new(style);
n.children = children;
n
}
fn all_rects(root: &Node, w: f32, h: f32) -> Vec<(f32, f32, f32, f32)> {
let mut measure = |_: &rux_layout::TextContent, _: Option<f32>| (0.0, 0.0);
layout(root, w, h, &mut measure)
.paints
.iter()
.filter_map(|p| match p {
Paint::Rect(r) => Some((r.x, r.y, r.width, r.height)),
_ => None,
})
.collect()
}
#[test]
fn wrapped_grid_reserves_height_for_every_row() {
let thumb = || {
boxed(
Style {
width: Some(Len::Px(64.0)),
height: Some(Len::Px(64.0)),
shrink: 0.0,
background: Some(Background::Color(Rgba::new(0.5, 0.5, 0.5, 1.0))),
..Default::default()
},
vec![],
)
};
let grid = boxed(
Style {
display: Display::Flex,
axis: Axis::Row,
wrap: true,
gap: 8.0,
width: Some(Len::Pct(1.0)),
max_width: Some(Len::Px(520.0)),
..Default::default()
},
(0..8).map(|_| thumb()).collect(),
);
let sentinel = boxed(
Style {
width: Some(Len::Px(200.0)),
height: Some(Len::Px(20.0)),
background: Some(Background::Color(Rgba::new(1.0, 0.0, 0.0, 1.0))),
..Default::default()
},
vec![],
);
let screen = boxed(
Style {
display: Display::Flex,
axis: Axis::Column,
gap: 12.0,
..Default::default()
},
vec![grid, sentinel],
);
let rects = all_rects(&screen, 1260.0, 790.0);
let thumbs: Vec<_> = rects.iter().filter(|r| r.2 == 64.0).collect();
let sentinel = *rects.iter().find(|r| r.2 == 200.0).expect("sentinel");
assert_eq!(thumbs.len(), 8, "all eight thumbs should paint");
let thumbs_bottom = thumbs
.iter()
.map(|t| t.1 + t.3)
.fold(0.0_f32, f32::max);
assert!(
thumbs_bottom > 100.0,
"expected the thumbs to wrap onto a second row (bottom {thumbs_bottom})"
);
assert!(
sentinel.1 >= thumbs_bottom - 0.5,
"sentinel (y={}) overlaps the wrapped thumbnails (bottom {thumbs_bottom})",
sentinel.1
);
}