use super::*;
use crate::TextStyle;
use nativeview_tables::layout_ext::{GridSingleTrack, GridTrack, GridTrackUnit};
use nativeview_tables::tables::{
AlignItems as NvAlignItems, BoxSizing as NvBoxSizing, Direction as NvDirection,
Display as NvDisplay, FlexDirection as NvFlexDirection, GridItem, GridLayout,
GridLine as NvGridLine, GridTracks, JustifyContent as NvJustifyContent, Layout as NvLayout,
Position as NvPosition, TextContent, Typography, Visual,
};
use nativeview_engine::Viewport;
use nativeview_store::{ChangeSet, Length, NodeId, NodeStore};
#[test]
fn taffy_store_layout_flex_grid_rtl_oof_hidden_text_and_incremental() {
let mut store = NodeStore::new();
let mut layout = TaffyStoreLayout::new();
let mut changes = ChangeSet::default();
let mut tx = store.tx();
let row = tx.create();
let text = tx.create();
let abs = tx.create();
let hidden = tx.create();
let grid = tx.create();
let c1 = tx.create();
let c2 = tx.create();
let rtl_box = tx.create();
let rtl_child = tx.create();
tx.set_children(NodeId::ROOT, &[row, grid, rtl_box]);
tx.set_children(row, &[text, abs, hidden]);
tx.set_children(grid, &[c1, c2]);
tx.set_children(rtl_box, &[rtl_child]);
tx.edit::<NvLayout>(NodeId::ROOT)
.padding_top(Length::points(10.0))
.padding_right(Length::points(10.0))
.padding_bottom(Length::points(10.0))
.padding_left(Length::points(10.0));
tx.edit::<NvLayout>(row)
.position(NvPosition::Relative)
.flex_direction(NvFlexDirection::Row)
.height(Length::points(100.0))
.align_items(NvAlignItems::Center);
tx.edit::<TextContent>(text).text("Hello NV02");
tx.edit::<Typography>(text).font_size(20.0);
tx.edit::<NvLayout>(abs)
.position(NvPosition::Absolute)
.inset_right(Length::points(8.0))
.inset_bottom(Length::points(6.0))
.width(Length::points(40.0))
.height(Length::points(24.0));
tx.edit::<NvLayout>(hidden)
.display(NvDisplay::None)
.width(Length::points(500.0))
.height(Length::points(500.0));
tx.edit::<NvLayout>(grid)
.display(NvDisplay::Grid)
.height(Length::points(80.0))
.column_gap(Length::points(10.0));
tx.edit::<GridLayout>(grid)
.template_columns(Some(&GridTracks::new(vec![
GridTrack::Px(120.0),
GridTrack::Fr(1.0),
GridTrack::Repeat(
1,
GridSingleTrack::MinMax(GridTrackUnit::Px(60.0), GridTrackUnit::Fr(1.0)),
),
])));
tx.edit::<GridItem>(c1)
.column_start(NvGridLine::Line(1))
.column_end(NvGridLine::Span(1));
tx.edit::<GridItem>(c2)
.column_start(NvGridLine::Line(2))
.column_end(NvGridLine::Span(2));
tx.edit::<NvLayout>(rtl_box)
.direction(NvDirection::Rtl)
.flex_direction(NvFlexDirection::Row)
.justify_content(NvJustifyContent::FlexStart)
.height(Length::points(50.0))
.padding_left(Length::points(24.0)) .padding_inline_logical(true);
tx.edit::<NvLayout>(rtl_child)
.width(Length::points(80.0))
.height(Length::points(30.0));
tx.commit_into(&mut changes);
let vp = Viewport {
width: 400.0,
height: 600.0,
scale_factor: 1.0,
};
layout.sync_changes(&store, &changes);
layout.compute_layout(&store, NodeId::ROOT, vp);
let r_root = layout.layout_of(NodeId::ROOT).unwrap();
assert_eq!(
(r_root.x, r_root.y, r_root.width, r_root.height),
(0.0, 0.0, 400.0, 600.0)
);
let r_row = layout.layout_of(row).unwrap();
assert_eq!(
(r_row.x, r_row.y, r_row.width, r_row.height),
(10.0, 10.0, 380.0, 100.0)
);
let r_text = layout.layout_of(text).unwrap();
assert!(
(r_text.width - 116.0).abs() <= 1.0,
"text w={}",
r_text.width
);
assert!(
(r_text.height - 27.0).abs() <= 1.0,
"text h={}",
r_text.height
);
let r_abs = layout.layout_of(abs).unwrap();
assert_eq!(
(r_abs.x, r_abs.y, r_abs.width, r_abs.height),
(342.0, 80.0, 40.0, 24.0)
);
let r_hid = layout.layout_of(hidden).unwrap();
assert_eq!((r_hid.width, r_hid.height), (0.0, 0.0));
let r_c1 = layout.layout_of(c1).unwrap();
let r_c2 = layout.layout_of(c2).unwrap();
assert_eq!(r_c1.width, 120.0);
assert_eq!(r_c2.x, r_c1.x + 120.0 + 10.0);
assert_eq!(r_c2.width, 250.0);
assert!(layout.resolved_rtl(rtl_box));
assert!(layout.resolved_rtl(rtl_child));
let r_rtl_child = layout.layout_of(rtl_child).unwrap();
assert_eq!(r_rtl_child.x, 286.0);
let mut tx = store.tx();
tx.edit::<NvLayout>(row).height(Length::points(150.0));
tx.commit_into(&mut changes);
layout.sync_changes(&store, &changes);
layout.compute_layout(&store, NodeId::ROOT, vp);
assert_eq!(layout.layout_of(row).unwrap().height, 150.0);
assert_eq!(layout.layout_of(grid).unwrap().y, 160.0);
}
#[test]
fn taffy_clears_stale_hoisted_children_and_propagates_rtl_detach() {
let mut store = NodeStore::new();
let mut layout = TaffyStoreLayout::new();
let mut changes = ChangeSet::default();
let vp = Viewport {
width: 400.0,
height: 300.0,
scale_factor: 1.0,
};
let (rtl_parent, child, host, abs_child) = {
let mut tx = store.tx();
let rtl_parent = tx.create();
let child = tx.create();
let host = tx.create();
let abs_child = tx.create();
tx.set_children(NodeId::ROOT, &[rtl_parent, host]);
tx.set_children(rtl_parent, &[child]);
tx.set_children(host, &[abs_child]);
tx.edit::<NvLayout>(rtl_parent)
.direction(NvDirection::Rtl)
.height(Length::points(40.0));
tx.edit::<NvLayout>(child)
.width(Length::points(50.0))
.height(Length::points(20.0));
tx.edit::<NvLayout>(host)
.position(NvPosition::Relative)
.width(Length::points(200.0))
.height(Length::points(100.0));
tx.edit::<NvLayout>(abs_child)
.position(NvPosition::Absolute)
.inset_left(Length::points(12.0))
.inset_top(Length::points(14.0))
.width(Length::points(30.0))
.height(Length::points(20.0));
tx.commit_into(&mut changes);
(rtl_parent, child, host, abs_child)
};
layout.sync_changes(&store, &changes);
layout.compute_layout(&store, NodeId::ROOT, vp);
assert!(layout.resolved_rtl(rtl_parent));
assert!(layout.resolved_rtl(child));
assert!(!layout.hoisted.get(host.index()).is_empty());
{
let mut tx = store.tx();
tx.detach::<NvLayout>(rtl_parent);
tx.set_children(host, &[]);
tx.set_children(NodeId::ROOT, &[rtl_parent, host, abs_child]);
tx.commit_into(&mut changes);
}
layout.sync_changes(&store, &changes);
layout.compute_layout(&store, NodeId::ROOT, vp);
assert!(!layout.resolved_rtl(rtl_parent));
assert!(!layout.resolved_rtl(child));
assert!(
layout.hoisted.get(host.index()).is_empty(),
"leaf container must clear stale hoisted children"
);
let r_abs = layout.layout_of(abs_child).unwrap();
assert_eq!((r_abs.x, r_abs.y), (12.0, 14.0));
}
const WRAPPED: &str = "word word word word word word word word word word word word word word word word \
word word word word word word word word word word word word word word word word";
#[test]
fn empty_leaf_aspect_ratio_with_flex_basis_and_flex_grow_resolves_the_cross_axis() {
type Edit = fn(&mut nativeview_store::Transaction<'_>, NodeId, NodeId);
let cases: Vec<(NvFlexDirection, Edit, (f32, f32))> = vec![
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0));
},
(100.0, 50.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c).flex_grow(1.0).aspect_ratio(Some(4.0));
},
(400.0, 100.0),
),
(
NvFlexDirection::Column,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(80.0))
.aspect_ratio(Some(2.0));
},
(160.0, 80.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0))
.padding_left(Length::points(10.0))
.padding_top(Length::points(10.0))
.padding_bottom(Length::points(4.0));
tx.edit::<Visual>(c).border_width(2.0);
},
(100.0, 50.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_grow(1.0)
.aspect_ratio(Some(8.0))
.padding_top(Length::points(6.0));
tx.edit::<Visual>(c).border_width(1.0);
},
(400.0, 50.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(40.0))
.aspect_ratio(Some(4.0))
.padding_top(Length::points(20.0));
},
(40.0, 20.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.box_sizing(NvBoxSizing::ContentBox)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0))
.padding_left(Length::points(10.0))
.padding_right(Length::points(10.0))
.padding_top(Length::points(10.0))
.padding_bottom(Length::points(10.0));
},
(120.0, 70.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.box_sizing(NvBoxSizing::ContentBox)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0))
.padding_left(Length::percent(5.0));
},
(120.0, 50.0),
),
(
NvFlexDirection::Column,
|tx, container, c| {
tx.edit::<NvLayout>(container)
.height(Length::points(300.0));
tx.edit::<NvLayout>(c).flex_grow(1.0).aspect_ratio(Some(0.5));
},
(150.0, 300.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(1.0))
.max_height(Length::points(40.0));
},
(40.0, 40.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(f32::MIN_POSITIVE));
},
(100.0, 0.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(f32::NAN));
},
(100.0, 0.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(-1.0));
},
(100.0, 0.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(f32::INFINITY));
},
(100.0, 0.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<TextContent>(c).text("hi");
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0));
},
(100.0, 50.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<TextContent>(c).text(WRAPPED);
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(8.0));
},
(100.0, -1.0),
),
(
NvFlexDirection::Column,
|tx, _, c| {
tx.edit::<TextContent>(c).text(WRAPPED);
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0));
},
(200.0, 100.0),
),
(
NvFlexDirection::Row,
|tx, _, c| {
tx.edit::<TextContent>(c).text("");
tx.edit::<NvLayout>(c)
.flex_basis(Length::points(100.0))
.aspect_ratio(Some(2.0));
},
(100.0, 50.0),
),
];
for (i, (direction, edit, expected)) in cases.into_iter().enumerate() {
let mut store = NodeStore::new();
let mut layout = TaffyStoreLayout::new();
let mut changes = ChangeSet::default();
let mut tx = store.tx();
let container = tx.create();
let leaf = tx.create();
tx.set_children(NodeId::ROOT, &[container]);
tx.set_children(container, &[leaf]);
tx.edit::<NvLayout>(container)
.flex_direction(direction)
.align_items(NvAlignItems::FlexStart)
.width(Length::points(400.0));
edit(&mut tx, container, leaf);
tx.commit_into(&mut changes);
layout.sync_changes(&store, &changes);
layout.compute_layout(
&store,
NodeId::ROOT,
Viewport {
width: 400.0,
height: 300.0,
scale_factor: 1.0,
},
);
let r = layout.layout_of(leaf).expect("leaf laid out");
if expected.1 < 0.0 {
let (_, h) =
layout
.text_measurer()
.measure(WRAPPED, &TextStyle::sized(16.0), Some(expected.0));
assert!(h > 2.0 * 16.0 * 1.25, "case {i}: wraps to several lines");
assert_eq!((r.width, r.height), (expected.0, h.round()), "case {i}");
} else {
assert_eq!((r.width, r.height), expected, "case {i}");
}
let c = layout.layout_of(container).expect("container laid out");
let (cross, leaf_cross) = if direction == NvFlexDirection::Row {
(c.height, r.height)
} else {
(c.width, r.width)
};
assert_eq!(
cross > 0.0,
leaf_cross > 0.0,
"case {i}: auto container must size to the leaf"
);
}
}
#[test]
fn text_wraps_inside_a_ratio_box_whose_padding_consumes_the_transferred_width() {
let mut store = NodeStore::new();
let mut layout = TaffyStoreLayout::new();
let mut changes = ChangeSet::default();
let mut tx = store.tx();
let container = tx.create();
let leaf = tx.create();
tx.set_children(NodeId::ROOT, &[container]);
tx.set_children(container, &[leaf]);
tx.edit::<NvLayout>(container)
.flex_direction(NvFlexDirection::Column)
.align_items(NvAlignItems::FlexStart)
.width(Length::points(400.0));
tx.edit::<TextContent>(leaf).text(WRAPPED);
tx.edit::<NvLayout>(leaf)
.flex_basis(Length::points(25.0))
.min_height(Length::points(0.0))
.aspect_ratio(Some(2.0))
.padding_left(Length::points(50.0));
tx.commit_into(&mut changes);
layout.sync_changes(&store, &changes);
let vp = Viewport {
width: 400.0,
height: 300.0,
scale_factor: 1.0,
};
assert!(layout.compute_layout(&store, NodeId::ROOT, vp));
let (word_w, _) = layout
.text_measurer()
.measure(WRAPPED, &TextStyle::sized(16.0), Some(0.0));
let r = layout.layout_of(leaf).expect("leaf laid out");
assert_eq!((r.width, r.height), ((50.0 + word_w).round(), 25.0));
assert!(
r.width < 100.0,
"wrapped at 0, not at the container: {}",
r.width
);
}
#[test]
fn an_overflowing_ratio_transfer_does_not_collapse_text_to_zero_width() {
let mut store = NodeStore::new();
let mut layout = TaffyStoreLayout::new();
let mut changes = ChangeSet::default();
let mut tx = store.tx();
let container = tx.create();
let leaf = tx.create();
tx.set_children(NodeId::ROOT, &[container]);
tx.set_children(container, &[leaf]);
tx.edit::<NvLayout>(container)
.flex_direction(NvFlexDirection::Column)
.align_items(NvAlignItems::FlexStart)
.width(Length::points(400.0));
tx.edit::<TextContent>(leaf).text(WRAPPED);
tx.edit::<NvLayout>(leaf)
.flex_basis(Length::points(100.0))
.min_height(Length::points(0.0))
.aspect_ratio(Some(1e38));
tx.commit_into(&mut changes);
layout.sync_changes(&store, &changes);
let vp = Viewport {
width: 400.0,
height: 300.0,
scale_factor: 1.0,
};
assert!(layout.compute_layout(&store, NodeId::ROOT, vp));
let (line_w, _) = layout
.text_measurer()
.measure(WRAPPED, &TextStyle::sized(16.0), Some(400.0));
let r = layout.layout_of(leaf).expect("leaf laid out");
assert!(r.width.is_finite() && r.height.is_finite(), "{r:?}");
assert_eq!((r.width, r.height), (line_w.round(), 100.0));
}