use morphorm::*;
use morphorm_ecs::*;
#[test]
fn auto_column_single_pixels_child() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(200.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto]);
world.set_grid_rows(root, vec![Units::Pixels(200.0)]);
let child = world.add(Some(root));
world.set_column_start(child, 0);
world.set_row_start(child, 0);
world.set_width(child, Units::Pixels(120.0));
world.set_height(child, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 120.0, height: 200.0 }));
}
#[test]
fn auto_columns_two_columns_different_widths() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(200.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto, Units::Auto]);
world.set_grid_rows(root, vec![Units::Pixels(200.0)]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(80.0));
world.set_height(child0, Units::Pixels(50.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 1);
world.set_row_start(child1, 0);
world.set_width(child1, Units::Pixels(150.0));
world.set_height(child1, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 80.0, height: 200.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 80.0, posy: 0.0, width: 150.0, height: 200.0 }));
}
#[test]
fn auto_column_mixed_with_pixels_column() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(200.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto, Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Pixels(200.0)]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(100.0));
world.set_height(child0, Units::Pixels(50.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 1);
world.set_row_start(child1, 0);
world.set_width(child1, Units::Stretch(1.0));
world.set_height(child1, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 200.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 100.0, posy: 0.0, width: 200.0, height: 200.0 }));
}
#[test]
fn auto_column_mixed_with_stretch_column() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(200.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto, Units::Stretch(1.0)]);
world.set_grid_rows(root, vec![Units::Pixels(200.0)]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(100.0));
world.set_height(child0, Units::Pixels(50.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 1);
world.set_row_start(child1, 0);
world.set_width(child1, Units::Stretch(1.0));
world.set_height(child1, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 200.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 100.0, posy: 0.0, width: 500.0, height: 200.0 }));
}
#[test]
fn auto_column_with_gap() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(200.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto, Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Pixels(200.0)]);
world.set_horizontal_gap(root, Units::Pixels(20.0));
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(100.0));
world.set_height(child0, Units::Pixels(50.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 1);
world.set_row_start(child1, 0);
world.set_width(child1, Units::Stretch(1.0));
world.set_height(child1, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 200.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 120.0, posy: 0.0, width: 200.0, height: 200.0 }));
}
#[test]
fn auto_column_multiple_children_max_width() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto]);
world.set_grid_rows(root, vec![Units::Pixels(100.0), Units::Pixels(100.0), Units::Pixels(100.0)]);
for (row, width) in [(0usize, 60.0f32), (1, 120.0f32), (2, 90.0f32)] {
let child = world.add(Some(root));
world.set_column_start(child, 0);
world.set_row_start(child, row);
world.set_width(child, Units::Pixels(width));
world.set_height(child, Units::Pixels(50.0));
}
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
let children: Vec<_> = root.children(&world.tree).filter(|c| c.visible(&world.store)).collect();
assert_eq!(world.cache.bounds(*children[0]), Some(&Rect { posx: 0.0, posy: 0.0, width: 120.0, height: 100.0 }));
assert_eq!(world.cache.bounds(*children[1]), Some(&Rect { posx: 0.0, posy: 100.0, width: 120.0, height: 100.0 }));
assert_eq!(world.cache.bounds(*children[2]), Some(&Rect { posx: 0.0, posy: 200.0, width: 120.0, height: 100.0 }));
}
#[test]
fn auto_row_single_pixels_child() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(400.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto]);
let child = world.add(Some(root));
world.set_column_start(child, 0);
world.set_row_start(child, 0);
world.set_width(child, Units::Pixels(80.0));
world.set_height(child, Units::Pixels(75.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 75.0 }));
}
#[test]
fn auto_rows_two_rows_different_heights() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(600.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto, Units::Auto]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(80.0));
world.set_height(child0, Units::Pixels(60.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 0);
world.set_row_start(child1, 1);
world.set_width(child1, Units::Pixels(80.0));
world.set_height(child1, Units::Pixels(40.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 60.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 60.0, width: 200.0, height: 40.0 }));
}
#[test]
fn auto_row_mixed_with_pixels_row() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(600.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto, Units::Pixels(100.0)]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(80.0));
world.set_height(child0, Units::Pixels(55.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 0);
world.set_row_start(child1, 1);
world.set_width(child1, Units::Pixels(80.0));
world.set_height(child1, Units::Stretch(1.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 55.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 55.0, width: 200.0, height: 100.0 }));
}
#[test]
fn auto_row_mixed_with_stretch_row() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(500.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto, Units::Stretch(1.0)]);
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(80.0));
world.set_height(child0, Units::Pixels(80.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 0);
world.set_row_start(child1, 1);
world.set_width(child1, Units::Pixels(80.0));
world.set_height(child1, Units::Stretch(1.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 80.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 80.0, width: 200.0, height: 420.0 }));
}
#[test]
fn auto_row_with_gap() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(400.0));
world.set_height(root, Units::Pixels(600.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto, Units::Pixels(100.0)]);
world.set_vertical_gap(root, Units::Pixels(10.0));
let child0 = world.add(Some(root));
world.set_column_start(child0, 0);
world.set_row_start(child0, 0);
world.set_width(child0, Units::Pixels(80.0));
world.set_height(child0, Units::Pixels(50.0));
let child1 = world.add(Some(root));
world.set_column_start(child1, 0);
world.set_row_start(child1, 1);
world.set_width(child1, Units::Pixels(80.0));
world.set_height(child1, Units::Stretch(1.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child0), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 60.0, width: 200.0, height: 100.0 }));
}
#[test]
fn auto_row_multiple_children_max_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(100.0), Units::Pixels(100.0), Units::Pixels(100.0)]);
world.set_grid_rows(root, vec![Units::Auto]);
for (col, height) in [(0usize, 40.0f32), (1, 80.0f32), (2, 55.0f32)] {
let child = world.add(Some(root));
world.set_column_start(child, col);
world.set_row_start(child, 0);
world.set_width(child, Units::Pixels(80.0));
world.set_height(child, Units::Pixels(height));
}
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
let children: Vec<_> = root.children(&world.tree).filter(|c| c.visible(&world.store)).collect();
assert_eq!(world.cache.bounds(*children[0]), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 80.0 }));
assert_eq!(world.cache.bounds(*children[1]), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 80.0 }));
assert_eq!(world.cache.bounds(*children[2]), Some(&Rect { posx: 200.0, posy: 0.0, width: 100.0, height: 80.0 }));
}
#[test]
fn auto_columns_and_rows_2x2() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto, Units::Auto]);
world.set_grid_rows(root, vec![Units::Auto, Units::Auto]);
for &(col, row, w, h) in
&[(0usize, 0usize, 100.0f32, 60.0f32), (1, 0, 80.0, 60.0), (0, 1, 100.0, 40.0), (1, 1, 80.0, 40.0)]
{
let child = world.add(Some(root));
world.set_column_start(child, col);
world.set_row_start(child, row);
world.set_width(child, Units::Pixels(w));
world.set_height(child, Units::Pixels(h));
}
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
let children: Vec<_> = root.children(&world.tree).filter(|c| c.visible(&world.store)).collect();
assert_eq!(world.cache.bounds(*children[0]), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 60.0 }));
assert_eq!(world.cache.bounds(*children[1]), Some(&Rect { posx: 100.0, posy: 0.0, width: 80.0, height: 60.0 }));
assert_eq!(world.cache.bounds(*children[2]), Some(&Rect { posx: 0.0, posy: 60.0, width: 100.0, height: 40.0 }));
assert_eq!(world.cache.bounds(*children[3]), Some(&Rect { posx: 100.0, posy: 60.0, width: 80.0, height: 40.0 }));
}
#[test]
fn auto_column_content_size_child() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Auto]);
world.set_grid_rows(root, vec![Units::Pixels(100.0)]);
let child = world.add(Some(root));
world.set_column_start(child, 0);
world.set_row_start(child, 0);
world.set_width(child, Units::Auto);
world.set_height(child, Units::Auto);
world.set_layout_type(child, LayoutType::Row);
world.set_content_size(child, |_, width, height| (width.unwrap_or(180.0).max(180.0), height.unwrap_or(40.0)));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 180.0, height: 100.0 }));
}
#[test]
fn auto_row_height_depends_on_column_width() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
world.set_layout_type(root, LayoutType::Grid);
world.set_alignment(root, Alignment::TopLeft);
world.set_grid_columns(root, vec![Units::Pixels(200.0)]);
world.set_grid_rows(root, vec![Units::Auto]);
let child = world.add(Some(root));
world.set_column_start(child, 0);
world.set_row_start(child, 0);
world.set_width(child, Units::Stretch(1.0));
world.set_height(child, Units::Auto);
world.set_layout_type(child, LayoutType::Row);
world.set_content_size(child, |_, width, _| {
let w = width.unwrap_or(1.0).max(1.0);
(w, (2000.0f32 / w).ceil())
});
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 10.0 }));
}