diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index ed6a7ca82..035a67130 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -1350,10 +1350,8 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option, + /// The screen y of the first baseline of the first control on the first line. baseline: Option, /// The screen y of the centre line of the first control, for a control that paints no text. centre: Option, @@ -195,6 +197,7 @@ pub(crate) fn begin(ctx: &Context, container: Id, owner: Id, kind: Kind) { nodes: Vec::new(), open: Vec::new(), control_top: 0.0, + first_bottom: None, baseline: None, centre: None, reset: None, @@ -266,21 +269,23 @@ pub(crate) fn place_controls(ctx: &Context, container: Id, top: f32) { }); } -/// Records the screen y of a control's first baseline in the innermost label scope. The first control to report wins. -pub(crate) fn record_baseline(ctx: &Context, baseline: f32) { +/// Records the line of a control at `rect` in the innermost label scope: its first baseline, or for a control that +/// paints no text its centre line. The label lines up with the first line of what it labels: a baseline counts from a +/// control that starts above the bottom of the first control that reported, so text that wraps onto a later line (a +/// note after a row of chips) does not move the label down; otherwise the label centres on the first control. +pub(crate) fn record_line(ctx: &Context, rect: Rect, baseline: Option) { pass_state::with::(ctx, |stack| { if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) { - scope.baseline.get_or_insert(baseline); - } - }); -} - -/// Records the screen y of the centre line of a control that paints no text (a box, a track) in the innermost label -/// scope; the label centres on it. The first control to report wins. -pub(crate) fn record_centre(ctx: &Context, centre: f32) { - pass_state::with::(ctx, |stack| { - if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) { - scope.centre.get_or_insert(centre); + let first_bottom = *scope.first_bottom.get_or_insert(rect.bottom()); + match baseline { + Some(baseline) if rect.top() < first_bottom => { + scope.baseline.get_or_insert(baseline); + } + Some(_) => {} + None => { + scope.centre.get_or_insert(rect.center().y); + } + } } }); } diff --git a/crates/ui-widgets/tests/it/label/baseline.rs b/crates/ui-widgets/tests/it/label/baseline.rs index 17a4e5afd..41573a34f 100644 --- a/crates/ui-widgets/tests/it/label/baseline.rs +++ b/crates/ui-widgets/tests/it/label/baseline.rs @@ -50,3 +50,37 @@ fn a_side_label_lines_up_with_the_text_of_a_piece_a_row_centres() { let (label, field) = (baseline_of(&fixture, "Quality"), baseline_of(&fixture, "80")); assert!((label - field).abs() <= SLACK, "the label lines up with the number of the field beside the check box: label {label}, field {field}"); } + +/// A control whose text sits on a later line than its first piece (a note under a swatch, "Active" after a wrapped +/// row of chips): the label stays level with the first line. +#[trial] +fn a_side_label_stays_on_the_first_line_of_its_control() { + use craft_theme::Srgba; + use craft_ui::choice::Swatch; + use craft_ui::display::Text; + use craft_ui::layout_components::Stack; + const GREY: Srgba = Srgba { r: 0.5, g: 0.5, b: 0.5, a: 1.0 }; + let fixture = settled_in(|ui| { + Field::new("Light theme").show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + Swatch::new("Light", GREY).show(ui); + Text::new("Active").show(ui); + }); + }); + }); + let swatch = fixture.by_label("Light").expect("the swatch is shown").rect; + let label = fixture.texts().iter().find(|run| run.text == "Light theme").map(|run| run.rect).expect("the label is painted"); + assert!( + label.center().y >= swatch.top() && label.center().y <= swatch.bottom(), + "the label sits level with the swatch, not with the note below it: label {label:?}, swatch {swatch:?}" + ); +} + +/// `body` in a field group, settled. +fn settled_in(mut body: impl FnMut(&mut Ui) + 'static) -> Fixture { + let mut fixture = Fixture::new(move |ui| { + FieldGroup::new("Appearance").show(ui, |ui, _| body(ui)); + }); + fixture.settle(); + fixture +}