Keep constrained number values visible

This commit is contained in:
Dylan M. Taylor
2026-11-21 16:54:59 -05:00
parent 5d5d1922ca
commit 3222847bd2
2 changed files with 46 additions and 1 deletions
+30 -1
View File
@@ -150,6 +150,8 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option<f64>> {
leading: Vec<Adornment>,
/// The unit and the chevrons after the text, measured in measure and parts for paint.
trailing: Vec<Adornment>,
/// Width of the current value in this pass, including its caret.
value_width: f32,
}
impl<'a> NumberField<'a, Option<f64>> {
@@ -204,6 +206,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
picked: None,
leading: Vec::new(),
trailing: Vec::new(),
value_width: 0.0,
}
.label(label)
}
@@ -363,7 +366,28 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
/// The rect of the inside label's glyph, the text, the unit and the chevrons inside `rect`.
fn laid_out(&self, style: &NumberStyle, rect: Rect, rtl: bool) -> Placed {
place(rect, &style.field, &self.leading, &self.trailing, rtl)
let mut field = style.field.clone();
let mut content = rect;
let mut placed = place(content, &field, &self.leading, &self.trailing, rtl);
if placed.text.width() < self.value_width {
field.metrics.gap = 0.0;
placed = place(content, &field, &self.leading, &self.trailing, rtl);
let missing = (self.value_width - placed.text.width()).max(0.0);
let padding = field.metrics.padding;
let available = padding.start + padding.end;
if available > 0.0 {
let fraction = (missing / available).min(1.0);
let start = padding.start * fraction;
let end = padding.end * fraction;
field.metrics.padding.start -= start;
field.metrics.padding.end -= end;
let (left, right) = if rtl { (end, start) } else { (start, end) };
content.min.x -= left;
content.max.x += right;
placed = place(content, &field, &self.leading, &self.trailing, rtl);
}
}
placed
}
/// The alert for `typed`, a typed number the rule held to its range, in shown units; `kept` is the committed
@@ -449,6 +473,11 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
let chevrons = self.chevrons(&cx.words::<TextWords>());
self.leading = Self::before_text(cx);
self.trailing = self.after_text(cx, &chevrons);
let names = self.names(|row| cx.env.resolve(&row.label().clone()));
self.value_width = cx.value().map_or(0.0, |value| {
let text = self.ruled(&cx.env.units(), false, names).format(value);
cx.field_text(&TextRun::new(&text).role(TypeRole::Mono)).x
});
let placed = self.laid_out(cx.style, cx.content_rect, rtl);
if let Some(slot) = placed.leading.first() {
cx.label_part(*slot);
@@ -358,3 +358,19 @@ fn zoom_field_fits_a_narrow_host_slot() {
fixture.key(Key::Enter, Modifiers::NONE).settle();
assert!((kept.zoom() - 200.0).abs() < 0.01, "the field remains editable");
}
/// A rail-width field shows all of a three-digit zoom and its percent sign.
#[trial]
fn rail_width_zoom_shows_its_whole_value() {
let mut fixture = Fixture::new(|ui| {
let (rect, _) = ui.allocate_exact_size(Vec2::new(40.0, 30.0), egui::Sense::hover());
let canvas = Rect::from_min_size(Pos2::ZERO, Vec2::new(800.0, 600.0));
let view = CanvasView::new(canvas, Point::new(1000.0, 750.0), 2.44);
ZoomField::new(ZOOM, Some(view), DOCUMENT).show_at(ui, rect);
});
fixture.settle();
for text in ["244", "%"] {
let run = fixture.texts().iter().find(|run| run.text == text).expect("the zoom value is painted");
assert!(!run.elided && run.clip.contains_rect(run.rect), "{text} must be visible: {run:?}");
}
}