diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index c32bee2cbf..25977df779 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -398,19 +398,30 @@ pub fn plan_levels(units: &[Vec], room: f32) -> Vec { /// continuous units leave at their least), and the continuous units narrow by their own rule in what is left. The level /// is kept with the row's measuring run, so a pass without one (nothing the row measures changed) shows each unit once, /// at its kept level. Outside a shrinking row the unit shows at level 0. +/// +/// The unit shows on the row's line: a child of it that fills its height takes the line's height, as a direct child of +/// the row does (`fill_size`). pub fn levelled_unit(ui: &mut Ui, key: Id, levels: usize, mut show: impl FnMut(&mut Ui, usize)) { let ctx = ui.ctx().clone(); let shrink = Env::of(ui).shrink; - // The unit's content is no unit of the row: it runs in a room of its own. - let own_room = Env::of(ui).derive_local(|scoped| scoped.start_room()); + // The unit's content is no unit of the row: it runs in a room of its own, on the row's line. + let line = Env::of(ui).row_line; + let own_room = Env::of(ui).derive_local(|scoped| { + scoped.start_room(); + scoped.row_line = line; + }); match shrink { Some(Shrink::Count(row)) => { let rtl = Env::of(ui).direction.is_rtl(); let mut widths = Vec::with_capacity(levels); let mut widest = Vec2::ZERO; - // Each level measures at what it takes in no room: unbounded along x, in a region as wide as the row's. - // An infinite egui region would put right-to-left content's start edge at infinity. - let no_room = own_room.derive_local(|scoped| scoped.unbounded.0 = true); + // Each level measures at what it takes in no room: unbounded along x, in a region as wide as the row's, + // and off the row's line, which a probe is not placed on. An infinite egui region would put + // right-to-left content's start edge at infinity. + let no_room = own_room.derive_local(|scoped| { + scoped.unbounded.0 = true; + scoped.row_line = None; + }); let width = ui.max_rect().width(); for level in 0..levels.max(1) { let installed = Env::install_for(ui, no_room.clone()); diff --git a/crates/ui-contract/tests/it/layout/line_fill.rs b/crates/ui-contract/tests/it/layout/line_fill.rs new file mode 100644 index 0000000000..88a9f49a82 --- /dev/null +++ b/crates/ui-contract/tests/it/layout/line_fill.rs @@ -0,0 +1,47 @@ +//! Filling the line of a row that takes its height from its content: a levelled unit stands on the row's line. + +use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing}; +use craft_ui::layout::{BodyLayout, levelled_unit}; +use craft_ui::testing::{Frames, trial}; +use craft_ui_test_kit::contract::{Panel, Probe, Seen}; +use egui::{Id, Rect, Vec2}; +use std::cell::RefCell; + +const SNAP: f32 = 1.0; + +/// The height of the tall member that sets each row's line. +const TALL: f32 = 100.0; + +/// The rect of the row and of the probe that fills, after the first frame and once settled, for a row of a tall probe +/// beside `filler`, which shows the filling probe into `seen`. +fn line_and_fill(filler: impl Fn(&mut egui::Ui, &RefCell)) -> [(Rect, Rect); 2] { + let (tall, fill, row) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()), RefCell::new(Seen::default())); + let mut frames = Frames::new(); + let mut show = |ui: &mut egui::Ui| { + Panel::new().layout(BodyLayout::row()).seen(&row).id_salt("row").show(ui, |ui, _| { + Probe::new(&tall).size(Vec2::new(40.0, TALL)).id_salt("tall").show(ui); + filler(ui, &fill); + }); + }; + frames.frame(Vec::new(), &mut show); + let first = (row.borrow().rect, fill.borrow().rect); + frames.idle(4, &mut show); + let settled = (row.borrow().rect, fill.borrow().rect); + [first, settled] +} + +#[trial] +fn a_levelled_unit_stands_on_its_rows_line() { + let seen = line_and_fill(|ui, fill| { + levelled_unit(ui, Id::new("unit"), 2, |ui, _| { + Probe::new(fill).sizing(Sizing::Fill).id_salt("fill").show(ui); + }); + }); + for ((row, fill), when) in seen.into_iter().zip(["in the first frame", "once settled"]) { + assert!((row.height() - TALL).abs() <= SNAP, "{when}: the row {row:?} is as tall as its tall member"); + assert!( + (fill.top() - row.top()).abs() <= SNAP && (fill.bottom() - row.bottom()).abs() <= SNAP, + "{when}: the unit's filling child {fill:?} spans the row's line {row:?}" + ); + } +} diff --git a/crates/ui-contract/tests/it/layout/mod.rs b/crates/ui-contract/tests/it/layout/mod.rs index 5d04599e33..3c8d9f2c6b 100644 --- a/crates/ui-contract/tests/it/layout/mod.rs +++ b/crates/ui-contract/tests/it/layout/mod.rs @@ -1,4 +1,5 @@ pub(crate) mod body; pub(crate) mod fill; +pub(crate) mod line_fill; pub(crate) mod memory; pub(crate) mod overlay_bar;