Number rows of a parameter dialog show their unit symbol (L-304)

This commit is contained in:
Dylan M. Taylor
2026-11-22 00:46:47 -05:00
parent b86e890545
commit 53054939b8
2 changed files with 25 additions and 2 deletions
@@ -500,7 +500,7 @@ impl<'p> Pass<'p> {
fn number(&mut self, ui: &mut Ui, line: &Line<'_>, range: Option<RangeInclusive<f64>>, step: f64, decimals: u32) {
let value = self.work.number(line.key).unwrap_or(0.0);
line.show(ui, |ui| {
let mut field = number_field(self.units, line.name.clone(), Some(value), decimals, line.param.unit).step(step);
let mut field = unit_field(self.units, line.name.clone(), Some(value), decimals, line.param.unit).step(step);
if let Some(range) = range.clone() {
field = field.range(range);
}
@@ -929,7 +929,7 @@ impl<'p> Pass<'p> {
}
None => {}
}
let field = number_field(self.units, line.name.clone(), Some(last), decimals, line.param.unit).range(range.clone());
let field = unit_field(self.units, line.name.clone(), Some(last), decimals, line.param.unit).range(range.clone());
let field = field.id_salt("field").enabled(self.work.number(line.key).is_some());
let shown = field.show(ui);
self.alert(shown.out_of_range);
@@ -1008,6 +1008,15 @@ fn number_field(units: &Units, name: Label, value: Option<f64>, decimals: u32, u
NumberField::new(name, Bind::given(value)).codec(NumberCodec::new(units.reader(unit), decimals as usize))
}
/// A [`number_field`] that shows the symbol of its unit after the number (the parameter's `unit`).
fn unit_field(units: &Units, name: Label, value: Option<f64>, decimals: u32, unit: Option<UnitKind>) -> NumberField<'static> {
let field = number_field(units, name, value, decimals, unit);
match unit {
Some(kind) => field.unit_kind(kind),
None => field,
}
}
/// The range that has the bounds that are set, open on the other side; `None` when neither is.
fn bounds(min: Option<f64>, max: Option<f64>) -> Option<RangeInclusive<f64>> {
(min.is_some() || max.is_some()).then(|| min.unwrap_or(f64::NEG_INFINITY)..=max.unwrap_or(f64::INFINITY))
@@ -22,3 +22,17 @@ fn a_long_unbroken_read_only_value_wraps_inside_the_dialog() {
let dialog = layers.first().expect("the dialog layer");
assert!(dialog.left() >= 0.0 && dialog.right() <= room.x, "the dialog stays inside the window: {dialog:?}");
}
/// A number row with a unit shows the unit's symbol after the number, as the parameter says.
#[trial]
fn a_number_row_shows_its_unit_after_the_number() {
use craft_ui::runtime::env::UnitKind;
let mut fixture = Fixture::new(|ui| {
let mut fields = DialogFields::new();
let rows = vec![Param::number("width", "Width", 0.01, 0.001, 3).unit(UnitKind::Inches)];
ParamDialog::new("units", "Hairlines", rows).values(Bind::bound(&mut fields)).show(ui);
});
fixture.room(vec2(900.0, 700.0));
fixture.settle();
assert!(fixture.texts().iter().any(|run| run.text == "in"), "the unit symbol is painted: {:?}", fixture.texts().iter().map(|run| run.text.clone()).collect::<Vec<_>>());
}