From 2ef7bbff3843f4fbe7f0af8f3461966e65fced7d Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 10:33:46 +0000 Subject: [PATCH] A field labelled above sits on a row's line by its control, as a control labelled above does A container labelled above its body reports its body's first control line as the line it sits on in a row, so a button beside a field labelled above sits level with the field's control, not its label. --- crates/ui-contract/src/contract/body/hooks.rs | 9 +++++++-- crates/ui-contract/src/layout/body.rs | 11 ++++++----- crates/ui-widgets/tests/it/layout/row_line.rs | 13 +++++++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 70f69657c..3661a1a5f 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -416,10 +416,15 @@ impl ShapeHooks for BodyHooks<'_> { layout_memory::measured(ctx, sub(id, "first")).map(|found| found.took.y) } - /// A container with no header whose body is an unwrapped row reports that row's line as its first line. + /// A container with no header whose body is an unwrapped row reports that row's line as its first line, and one + /// labelled above its body the line of its body's first control: it sits on a row's line by that control, as a + /// control labelled above does. fn first_is_row_line(&self, comp: &C) -> bool { let props = comp.props(Internal::new()); - props.header.is_none() && super::size::unwrapped_row(props) + let above = props.label.as_ref().is_some_and(|side| { + matches!(side.layout, label::FieldLayout::Above | label::FieldLayout::Caption(_)) && side.kind != label::LabelKind::Hidden + }); + props.header.is_none() && (above || super::size::unwrapped_row(props)) } fn member(&self, comp: &C) -> bool { diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index de1ac5bc8..5a7d9148a 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -1155,9 +1155,10 @@ pub(crate) struct InFlow { /// rows run along Y, or a container whose first child is one. The child is one line when that centre lies at /// half its height (`one_line`). pub first: Option, - /// `first` is the line of the child's own unwrapped `Row` body, or of the control of a labelled control's row: the - /// child sits on a row's line by it even when it is more than one line (a nested row with a child of two lines, a - /// check box whose label has a detail line under it, a control under its label). + /// `first` is the line of the child's own unwrapped `Row` body, of the control of a labelled control's row, or of + /// the first control of a container labelled above its body: the child sits on a row's line by it even when it is + /// more than one line (a nested row with a child of two lines, a check box whose label has a detail line under it, + /// a control or a field under its label). pub line: bool, } @@ -1166,8 +1167,8 @@ const ONE_LINE_SLACK: f32 = 0.5; /// The centre of the line of a child `height` tall whose first line's centre lies `first` below its top, when the /// child is one line: a control, a row of one-line children, a container around one line (a field with its label -/// beside its control). A child of more than one line (a column, a field with its label above or a help line below) -/// has its first line above its middle. +/// beside its control). A child of more than one line (a column, a field with a help line below) has its first line +/// above its middle. fn one_line(first: Option, height: f32) -> Option { first.filter(|first| (2.0 * first - height).abs() <= ONE_LINE_SLACK) } diff --git a/crates/ui-widgets/tests/it/layout/row_line.rs b/crates/ui-widgets/tests/it/layout/row_line.rs index 9ba63754d..92a3cd6b8 100644 --- a/crates/ui-widgets/tests/it/layout/row_line.rs +++ b/crates/ui-widgets/tests/it/layout/row_line.rs @@ -1,6 +1,7 @@ //! The row line: a child of an unwrapped row that is one line sits with its centre on the row's line; a child of more -//! than one line (a column, a field with its label above or a help line below) sits at the row's top. A control with -//! its label after it sits on the line by its control, as a bare control does, in a row and in a grid's cell. +//! than one line (a column, a field with a help line below) sits at the row's top. A control with its label after it +//! or above it, and a field labelled above, sit on the line by their control, as a bare control does, in a row and in +//! a grid's cell. use craft_behavior::ColumnWidth; use craft_theme::ControlSize; @@ -107,10 +108,10 @@ fn columns_sit_at_the_rows_top_and_lines_on_the_line() { assert!(four.top() <= control(&fixture, &["Go"]).top() + ROUNDING, "the four-line column starts below the button's top"); } -/// A field labelled above its control is two lines and sits at the row's top, so a button beside it sits level with -/// the label, not the control. +/// A field labelled above its control sits on the row's line by its control, as a control labelled above does: a +/// button beside it sits level with the control, and the label stands above the line. #[trial] -fn a_field_labelled_above_sits_at_the_rows_top() { +fn a_field_labelled_above_sits_on_the_row_line_by_its_control() { let fixture = settled(|ui| { Row::new().show(ui, |ui, _| { Field::new("Caption").layout(FieldLayout::Above).sizing(Sizing::Content).show(ui, |ui, _| { @@ -121,7 +122,7 @@ fn a_field_labelled_above_sits_at_the_rows_top() { }); let (caption, control, button) = (text(&fixture, "Caption"), text(&fixture, "X"), text(&fixture, "Y")); assert!(caption.bottom() < control.top(), "the label sits above its control"); - assert!(button.center().y < control.top(), "the button {button:?} sits level with the label, above the control {control:?}"); + assert!((button.center().y - control.center().y).abs() <= ROUNDING, "the button {button:?} sits on the control's line {control:?}"); } /// A check box whose label runs to a second line under it, beside a button in a row: its box sits on the button's