craft-ui: a levelled unit's children take no slots on its row's line

The content of a levelled unit stood on the row's line for every purpose, so each direct child of a unit took a slot
as tall as the line and reported its line to the row: a unit of two controls was two lines high. The unit's content
takes the line only to fill it (`fill_size`, `line_height`); its children are placed in the unit's own flow and
report nothing to the row (`RowLine::places`).
This commit is contained in:
aiongg
2026-11-21 07:07:12 +00:00
parent 8da82d585b
commit 61c6e3cbcd
3 changed files with 41 additions and 11 deletions
+10 -7
View File
@@ -400,14 +400,16 @@ pub fn plan_levels(units: &[Vec<f32>], room: f32) -> Vec<usize> {
/// 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`). At its level it runs in the width the shrink step gave that level, a bounded room even
/// in a row that scrolls, so its content lays out as in any room of that width: a centred row in it centres across
/// its column, not in the view the row scrolls in.
/// the row does (`fill_size`). Its children are placed in the unit's own flow, not on slots of the line, and report no
/// line to the row: a unit of two controls stays one line high. At its level it runs in the width the shrink step gave
/// that level, a bounded room even in a row that scrolls, so its content lays out as in any room of that width: a
/// centred row in it centres across its column, not in the view the row scrolls in.
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, on the row's line.
let line = Env::of(ui).row_line;
// The unit's content is no unit of the row: it runs in a room of its own, on the row's line only to fill it. Its
// children take no slots on the line and report nothing to the row: the unit's block is what stands in the row.
let line = Env::of(ui).row_line.map(|line| RowLine { places: false, ..line });
let own_room = Env::of(ui).derive_local(|scoped| {
scoped.start_room();
scoped.row_line = line;
@@ -1143,7 +1145,7 @@ pub fn place_in_flow(ui: &mut Ui, size: Vec2) -> Rect {
/// and its first line to the container whose header or body it starts.
pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect {
let env = Env::of(ui);
let (line, target) = (env.row_line, env.first_line);
let (line, target) = (env.row_line.filter(|line| line.places), env.first_line);
let child = InFlow { first: if child.line { child.first } else { one_line(child.first, size.y) }, ..child };
let above = line.and_then(|line| line.centre).zip(child.first).map_or(0.0, |(centre, first)| (centre - first).max(0.0));
let slot = Vec2::new(size.x, line.and_then(|line| line.height).map_or(above + size.y, |height| height.max(above + size.y)));
@@ -1536,6 +1538,7 @@ pub(crate) fn members_line(ctx: &egui::Context, body: Id) -> Option<RowLine> {
centre: Some(took.x),
height: Some(took.y),
settled: true,
places: true,
})
}
@@ -1552,7 +1555,7 @@ fn on_line(body: Id, own: Option<(f32, f32)>, shared: Option<f32>) -> RowLine {
(None, Some(shared)) => (Some(shared / 2.0), Some(shared)),
(None, None) => (None, None),
};
RowLine { body, centre, height, settled: own.is_some() }
RowLine { body, centre, height, settled: own.is_some(), places: true }
}
/// Starts a sweep of a body's children in a measuring run: the body's first-line `target` and, for the unwrapped
+4
View File
@@ -293,6 +293,10 @@ pub(crate) struct RowLine {
/// container that fills its height take the line (`line_height`). While the row is measured, the line it shares
/// gives `centre` and `height` but is not settled.
pub(crate) settled: bool,
/// Whether the children placed in flow under the line take their slots on it and report their lines to the row:
/// the row's direct children. The content of a levelled unit stands on the line only to fill it (`fill_size`,
/// `line_height`).
pub(crate) places: bool,
}
/// What a collection records for one of its items in a pass: the row flags the item folds into its state (step 12)
@@ -1,7 +1,7 @@
//! Filling the line of a row that takes its height from its content: a levelled unit stands on the row's line, a
//! container that fills its height there runs its body in the line's height from the first frame and measures its
//! content while the row measures, and a centred row in a unit of a row that scrolls centres across its own column, not
//! in the view.
//! Filling the line of a row that takes its height from its content: a levelled unit stands on the row's line and its
//! children take no slots on it, a container that fills its height there runs its body in the line's height from the
//! first frame and measures its content while the row measures, and a centred row in a unit of a row that scrolls
//! centres across its own column, not in the view.
use craft_ui::contract::{ContainerProps, HeaderRegion, Props as _, Scroll, Show as _, ShowContainer as _, Sizing};
use craft_ui::layout::{BodyLayout, RowAlign, levelled_unit};
@@ -50,6 +50,29 @@ fn a_levelled_unit_stands_on_its_rows_line() {
}
}
/// A levelled unit of two controls stays one line high: its children sit in the unit's own column, not each on a slot
/// as tall as the row's line.
#[trial]
fn a_levelled_unit_of_two_controls_stays_one_line_high() {
let second = RefCell::new(Seen::default());
let seen = line_and_fill(|ui, top| {
levelled_unit(ui, Id::new("unit"), 1, |ui, _| {
Probe::new(top).id_salt("first").show(ui);
Probe::new(&second).id_salt("second").show(ui);
});
});
let second = second.borrow().rect;
for ((row, top), when) in seen.into_iter().zip(["in the first frame", "once settled"]) {
assert!((row.height() - TALL).abs() <= SNAP, "{when}: the row {row:?} stays as tall as its tall member");
assert!(
top.top() >= row.top() - SNAP && top.bottom() <= row.bottom() + SNAP,
"{when}: the unit's first control {top:?} lies in the row {row:?}"
);
}
let row = seen[1].0;
assert!(second.bottom() <= row.bottom() + SNAP, "the unit's second control {second:?} lies in the row {row:?}");
}
/// A container that fills its height, on a row that shares its line with an inline header, measures its content while
/// the row measures: the shared line, shorter than the content, never caps it, and both its lines show in the row.
#[trial]