ui-widgets: a derived number field shows its reading whole inside the field

A number field gives a value wider than its text area some of its padding
back. It measured that value as the plain number, so a derived value,
which reads in parentheses with its unit, kept the full padding and its
closing parenthesis was cut. In the number_field_derived story at compact
size, "(12.5 pt)" is 62.3 pt wide in a 60 pt clip.

The field now measures a derived value as it shows at rest. Its reading
takes the padding it needs, as a longer number does, and the field keeps
its width. A field whose value fits is laid out as before.
This commit is contained in:
dotCooCoo committed 2026-11-25 02:47:57 -08:00
1 parent 58acd23cdc
commit 7e3945ec4e
2 files changed
+29 -1

No files matched your search

+6 -1
View File
@@ -763,8 +763,13 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
self.leading = Self::before_text(cx, self.step_arrows);
self.trailing = self.after_text(cx, &chevrons);
let names = self.names(|row| cx.env.resolve(&row.label().clone()));
// A derived value is measured as it shows at rest, in parentheses with its unit, so its reading takes the
// padding it needs as a longer number does.
self.value_width = cx.value().map_or(0.0, |value| {
let text = self.ruled(&cx.env, false, names).format(value);
let mut text = self.ruled(&cx.env, false, names).format(value);
if self.derives(Some(value)) {
text = self.derived_text(&text, &cx.env.units());
}
cx.field_text(&TextRun::new(&text).role(self.value_role())).x
});
let placed = self.laid_out(cx.style, cx.content_rect, rtl);
@@ -37,6 +37,29 @@ fn a_derived_number_shows_in_parentheses_and_edits_from_the_plain_number() {
assert_eq!(fixture.tree().numeric(id.get()).and_then(|numeric| numeric.value), Some(12.0), "the field reports the committed number");
}
/// A derived reading that is wider than the field's text area with its padding, "(12.5 pt)" in a one-cell field, takes
/// room from the padding and shows whole inside the field, and the field is as wide as the same field showing the plain
/// number with its unit. Guards against sizing the text area by the plain number, which cut the closing parenthesis.
#[trial]
fn a_derived_number_shows_whole_inside_its_field() {
let boxes = Rc::new(Cell::new([egui::Rect::NOTHING; 2]));
let seen = boxes.clone();
let mut fixture = Fixture::new(move |ui| {
let mut derived = Some(12.5);
let mut plain = Some(12.5);
let at = NumberField::new("Leading", Bind::bound(&mut derived)).unit_kind(UnitKind::Points).derived(true).show(ui).response.rect;
let beside = NumberField::new("Size", Bind::bound(&mut plain)).unit_kind(UnitKind::Points).show(ui).response.rect;
seen.set([at, beside]);
});
fixture.settle();
fixture.idle(3);
let [field, plain] = boxes.get();
let text = fixture.texts().iter().find(|text| text.text == "(12.5 pt)").expect("the derived reading is painted").clone();
assert!(field.contains_rect(text.rect), "the reading {:?} lies inside the field {field:?}", text.rect);
assert!(text.clip.contains_rect(text.rect), "the reading {:?} is not cut by its clip {:?}", text.rect, text.clip);
assert_eq!(field.width(), plain.width(), "the derived field is as wide as the same field with the plain number");
}
/// A blank value is no number to derive: the field shows it as it would without `derived`, never as empty
/// parentheses, and an edit from the blank still starts from nothing.
#[trial]