diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 709acadda..38096d840 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -500,7 +500,7 @@ impl<'p> Pass<'p> { fn number(&mut self, ui: &mut Ui, line: &Line<'_>, range: Option>, 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, 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, decimals: u32, unit: Option) -> 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, max: Option) -> Option> { (min.is_some() || max.is_some()).then(|| min.unwrap_or(f64::NEG_INFINITY)..=max.unwrap_or(f64::INFINITY)) diff --git a/crates/ui-patterns/tests/it/form_dialogs/long_value.rs b/crates/ui-patterns/tests/it/form_dialogs/long_value.rs index be13dfa99..f174d6476 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/long_value.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/long_value.rs @@ -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::>()); +}