use morphorm::*;
use morphorm_ecs::*;
#[test]
fn wrap_row_basic() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(300.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 200.0, posy: 0.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_row_with_gap() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(300.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
world.set_horizontal_gap(root, Units::Pixels(20.0));
world.set_vertical_gap(root, Units::Pixels(10.0));
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(80.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(80.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(80.0));
world.set_height(node3, Units::Pixels(50.0));
let node4 = world.add(Some(root));
world.set_width(node4, Units::Pixels(80.0));
world.set_height(node4, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 80.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 80.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 200.0, posy: 0.0, width: 80.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node4), Some(&Rect { posx: 0.0, posy: 60.0, width: 80.0, height: 50.0 }));
}
#[test]
fn wrap_column_basic() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(200.0));
world.set_height(root, Units::Pixels(250.0));
world.set_layout_type(root, LayoutType::Column);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(80.0));
world.set_height(node1, Units::Pixels(100.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(80.0));
world.set_height(node2, Units::Pixels(100.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(80.0));
world.set_height(node3, Units::Pixels(100.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 80.0, height: 100.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 0.0, posy: 100.0, width: 80.0, height: 100.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 80.0, posy: 0.0, width: 80.0, height: 100.0 }));
}
#[test]
fn wrap_row_with_stretch() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(300.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Stretch(1.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Stretch(1.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 200.0, posy: 0.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_row_no_wrap_mode() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(300.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::NoWrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 200.0, posy: 0.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_row_single_item_per_line() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(250.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(200.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(200.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(200.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 200.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 0.0, posy: 50.0, width: 200.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 0.0, posy: 100.0, width: 200.0, height: 50.0 }));
}
#[test]
fn wrap_with_alignment() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(300.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::Center);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 50.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 150.0, posy: 0.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_auto_container() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(250.0));
world.set_height(root, Units::Pixels(250.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 0.0, posy: 50.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_with_different_line_heights() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(250.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(80.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(60.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 100.0, posy: 0.0, width: 100.0, height: 80.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 0.0, posy: 80.0, width: 100.0, height: 60.0 }));
}
#[test]
fn wrap_row_with_padding() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(320.0));
world.set_height(root, Units::Pixels(320.0));
world.set_padding_left(root, Units::Pixels(10.0));
world.set_padding_right(root, Units::Pixels(10.0));
world.set_padding_top(root, Units::Pixels(10.0));
world.set_padding_bottom(root, Units::Pixels(10.0));
world.set_layout_type(root, LayoutType::Row);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
let node4 = world.add(Some(root));
world.set_width(node4, Units::Pixels(100.0));
world.set_height(node4, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 10.0, posy: 10.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 110.0, posy: 10.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 210.0, posy: 10.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node4), Some(&Rect { posx: 10.0, posy: 60.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_row_rtl() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(250.0));
world.set_height(root, Units::Pixels(300.0));
world.set_layout_type(root, LayoutType::Row);
world.set_direction(root, Direction::RightToLeft);
world.set_wrap(root, LayoutWrap::Wrap);
world.set_alignment(root, Alignment::TopLeft);
let node1 = world.add(Some(root));
world.set_width(node1, Units::Pixels(100.0));
world.set_height(node1, Units::Pixels(50.0));
let node2 = world.add(Some(root));
world.set_width(node2, Units::Pixels(100.0));
world.set_height(node2, Units::Pixels(50.0));
let node3 = world.add(Some(root));
world.set_width(node3, Units::Pixels(100.0));
world.set_height(node3, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node1), Some(&Rect { posx: 150.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node2), Some(&Rect { posx: 50.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(node3), Some(&Rect { posx: 150.0, posy: 50.0, width: 100.0, height: 50.0 }));
}
#[test]
fn wrap_row_auto_height_includes_lines_gap_and_padding() {
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::Column);
world.set_alignment(root, Alignment::TopLeft);
let wrap = world.add(Some(root));
world.set_width(wrap, Units::Pixels(250.0));
world.set_height(wrap, Units::Auto);
world.set_layout_type(wrap, LayoutType::Row);
world.set_wrap(wrap, LayoutWrap::Wrap);
world.set_alignment(wrap, Alignment::TopLeft);
world.set_padding_top(wrap, Units::Pixels(5.0));
world.set_padding_bottom(wrap, Units::Pixels(5.0));
world.set_vertical_gap(wrap, Units::Pixels(10.0));
let a = world.add(Some(wrap));
world.set_width(a, Units::Pixels(100.0));
world.set_height(a, Units::Pixels(50.0));
let b = world.add(Some(wrap));
world.set_width(b, Units::Pixels(100.0));
world.set_height(b, Units::Pixels(50.0));
let c = world.add(Some(wrap));
world.set_width(c, Units::Pixels(100.0));
world.set_height(c, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(wrap), Some(&Rect { posx: 0.0, posy: 0.0, width: 250.0, height: 120.0 }));
assert_eq!(world.cache.bounds(a), Some(&Rect { posx: 0.0, posy: 5.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(b), Some(&Rect { posx: 100.0, posy: 5.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(c), Some(&Rect { posx: 0.0, posy: 65.0, width: 100.0, height: 50.0 }));
}