Label scope: measure the control baseline from the top of the labelled control

A control inside a label scope reports the screen y of its first baseline
(or centre line), and the scope stores it measured from the top of the rect
the label step placed the controls in. A piece a row centres beside a taller
one (a number field beside an AngleDial) reports a baseline further down, so
its side label lines up with its text instead of sitting above it.

PaintedText carries the run's first baseline for trials.
This commit is contained in:
aiongg
2026-11-12 14:32:45 +00:00
parent ee9aaf11eb
commit c8f2a28def
6 changed files with 88 additions and 11 deletions
@@ -146,7 +146,9 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option<
let label_id = sub(input.scope, "label");
if input.side.shows_no_leaf() {
scope::begin(&ctx, input.scope, input.owner, scope::Kind::Label(field_label(env, input, label_id, Rect::NOTHING)));
return Placed { label: Rect::NOTHING, control: hidden_control(&ctx, input, flow, gap), trailing: Rect::NOTHING };
let control = hidden_control(&ctx, input, flow, gap);
scope::place_controls(&ctx, input.scope, control.top());
return Placed { label: Rect::NOTHING, control, trailing: Rect::NOTHING };
}
let slot = if reset.is_some() && input.side.reset_dot { slot_side } else { 0.0 };
let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot);
@@ -199,6 +201,7 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option<
let label =
field_label(env, input, label_id_for_scope, placed.label).with_press(shown.press).with_hover(shown.hovered).activated(shown.activated);
scope::begin(&ctx, input.scope, input.owner, scope::Kind::Label(label));
scope::place_controls(&ctx, input.scope, placed.control.top());
if input.claims {
scope::offer_claim(&ctx, unslotted.rect());
}
+2 -2
View File
@@ -1330,9 +1330,9 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<T
let core = e.core_ref();
let baseline = core.paint.borrow().baseline;
if let Some(baseline) = baseline {
scope::record_baseline(&ctx, baseline - core.rect.top());
scope::record_baseline(&ctx, baseline);
} else if shape == ShapeKind::Leaf {
scope::record_centre(&ctx, core.rect.height() / 2.0);
scope::record_centre(&ctx, core.rect.center().y);
}
}
real_pass = real;
+22 -8
View File
@@ -132,9 +132,12 @@ struct Scope {
/// Node components inside this scope whose step 16 has not come yet, outermost first, with the number of focus stops
/// registered before they began.
open: Vec<(Id, usize)>,
/// The first baseline of the first control, from the control's top.
/// The screen y of the top of the rect the label step placed the controls in; the recorded baseline and centre
/// are stored measured from it.
control_top: f32,
/// The screen y of the first baseline of the first control.
baseline: Option<f32>,
/// The centre line of the first control, from its top, for a control that paints no text.
/// The screen y of the centre line of the first control, for a control that paints no text.
centre: Option<f32>,
/// The control that registered a reset value and whether its value differs from it.
reset: Option<ResetFact>,
@@ -191,6 +194,7 @@ pub(crate) fn begin(ctx: &Context, container: Id, owner: Id, kind: Kind) {
kind,
nodes: Vec::new(),
open: Vec::new(),
control_top: 0.0,
baseline: None,
centre: None,
reset: None,
@@ -251,8 +255,18 @@ fn report_unnamed(ctx: &Context, unnamed: Option<Finding>) {
}
}
/// Records the first baseline of a control inside the innermost label scope, measured from the control's top.
/// The first control to report wins.
/// Sets the screen y of the top of the rect the label step of `container` placed its controls in. The label lines up
/// with a baseline measured from there: a control that sits lower in that rect (a short piece a row centres beside a
/// taller one) reports a baseline further down.
pub(crate) fn place_controls(ctx: &Context, container: Id, top: f32) {
pass_state::with::<Stack, _>(ctx, |stack| {
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| scope.container == container && matches!(scope.kind, Kind::Label(_))) {
scope.control_top = top;
}
});
}
/// 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) {
pass_state::with::<Stack, _>(ctx, |stack| {
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) {
@@ -261,8 +275,8 @@ pub(crate) fn record_baseline(ctx: &Context, baseline: f32) {
});
}
/// Records the centre line of a control that paints no text (a box, a track), measured from its top, in the innermost
/// label scope; the label centres on it. The first control to report wins.
/// 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::<Stack, _>(ctx, |stack| {
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) {
@@ -548,10 +562,10 @@ pub(crate) fn finish(ctx: &Context, container: Id, merged: Option<Merged>) {
crate::runtime::focus::request(ctx, only.inner_stop.unwrap_or(crate::runtime::focus::FocusTarget::Id(only.id)));
}
if let Some(baseline) = scope.baseline {
layout_memory::record(ctx, sub(scope.container, "baseline"), Vec2::new(0.0, baseline));
layout_memory::record(ctx, sub(scope.container, "baseline"), Vec2::new(0.0, baseline - scope.control_top));
}
if let (None, Some(centre)) = (scope.baseline, scope.centre) {
layout_memory::record(ctx, sub(scope.container, "centre"), Vec2::new(0.0, centre));
layout_memory::record(ctx, sub(scope.container, "centre"), Vec2::new(0.0, centre - scope.control_top));
}
let slot = reset_slot(scope.container);
match scope.reset {
@@ -73,6 +73,8 @@ pub struct PaintedText {
pub elided: bool,
/// The run's full text.
pub text: String,
/// The screen y of the run's first baseline.
pub baseline: f32,
}
/// The IME output of a frame: the owner widget, whether it asks the OS to interrupt a composition, the caret rect.
@@ -167,8 +169,13 @@ pub(crate) fn run_frame(
let nth = painted_before.entry(Arc::as_ptr(&text.galley) as usize).or_insert(0);
let full = super::painted::full_text_of_painted(ctx, &text.galley, *nth);
*nth += 1;
let baseline = text.galley.rows.first().map_or(0.0, |placed| match placed.row.glyphs.first() {
Some(glyph) => placed.pos.y + glyph.pos.y,
None => placed.pos.y + placed.row.size.y,
});
Some(PaintedText {
rect: text.galley.rect.translate(text.pos.to_vec2()),
baseline: text.pos.y + baseline,
elided: full.is_some(),
text: full.unwrap_or_else(|| text.galley.job.text.clone()),
})
@@ -0,0 +1,52 @@
//! A side label beside a row of controls of different heights lines up with the text of the control it names.
use craft_ui::choice::Checkbox;
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _};
use craft_ui::form::{Field, FieldGroup};
use craft_ui::layout_components::Row;
use craft_ui::testing::{Fixture, trial};
use craft_ui::text::NumberField;
use craft_ui::value_drag::AngleDial;
use egui::Ui;
/// How far two baselines may differ and still line up, in points.
const SLACK: f32 = 1.0;
/// The painted baseline of the run whose text is `text`.
fn baseline_of(fixture: &Fixture, text: &str) -> f32 {
let texts = fixture.texts();
let run = texts.iter().find(|run| run.text.trim() == text).unwrap_or_else(|| panic!("{text:?} is painted: {texts:?}"));
run.baseline
}
/// The rows of a parameter form: a field whose control is a row of `body`, in a field group.
fn settled(name: &'static str, mut body: impl FnMut(&mut Ui) + 'static) -> Fixture {
let mut fixture = Fixture::new(move |ui| {
FieldGroup::new("Parameters").show(ui, |ui, _| {
Field::new(name).show(ui, |ui, _| {
Row::new().show(ui, |ui, _| body(ui));
});
});
});
fixture.settle();
fixture
}
/// A row centres its shorter piece beside a taller one: the label lines up with the text of the piece that reports
/// the first baseline, not with the top of that piece.
#[trial]
fn a_side_label_lines_up_with_the_text_of_a_piece_a_row_centres() {
let fixture = settled("Angle", |ui| {
AngleDial::new(Bind::given(45.0)).label("Angle").id_salt("dial").show(ui);
NumberField::new("Angle in degrees", Bind::given(Some(45.0))).id_salt("field").show(ui);
});
let (label, field) = (baseline_of(&fixture, "Angle"), baseline_of(&fixture, "45"));
assert!((label - field).abs() <= SLACK, "the label lines up with the number beside the tall dial: label {label}, field {field}");
let fixture = settled("Quality", |ui| {
Checkbox::new(Bind::given(true)).label("Custom").id_salt("custom").show(ui);
NumberField::new("Quality", Bind::given(Some(80.0))).id_salt("field").show(ui);
});
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}");
}
+1
View File
@@ -1,6 +1,7 @@
//! The label step: scrub across a pointer warp and the label beside or above a fill-width control.
mod align;
mod baseline;
pub mod group;
mod hit;
mod inside;