diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index f850cacde..bab06814f 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -5373,7 +5373,7 @@ impl CharFields<'_> { /// A number attribute shown multiplied by `scale` (100 for a fraction shown as a percentage). fn char_number(ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields, key: &str, suffix: &str, scale: f64, range: (f64, f64)) { let v = cf.get(d, key).as_f64().map(|x| x * scale); - let field = crate::widgets::NumField::with_suffix(key, v, suffix, 2).width(80.0).range(range.0, range.1); + let field = crate::widgets::NumField::number(key, v, suffix, 2).width(80.0).range(range.0, range.1); let edit = if cf.sparse { field.show_or_clear(ui) } else { field.show(ui).map(Some) }; if let Some(n) = edit { cf.set(d, key, n.map_or(Value::Null, |n| json!(n / scale))); diff --git a/crates/ui-egui/src/dialogs/style_cjk.rs b/crates/ui-egui/src/dialogs/style_cjk.rs index f337324f4..674cf44a7 100644 --- a/crates/ui-egui/src/dialogs/style_cjk.rs +++ b/crates/ui-egui/src/dialogs/style_cjk.rs @@ -120,7 +120,7 @@ fn grid(id: &str) -> egui::Grid { /// emptying the field makes it automatic again. fn char_auto_number(ui: &mut egui::Ui, d: &mut Dialog, cf: &CharFields, key: &str, suffix: &str, scale: f64, range: (f64, f64)) { let v = cf.get(d, key).as_f64().map(|x| x * scale); - let field = crate::widgets::NumField::with_suffix(key, v, suffix, 2).width(80.0).range(range.0, range.1); + let field = crate::widgets::NumField::number(key, v, suffix, 2).width(80.0).range(range.0, range.1); if let Some(n) = field.show_or_clear(ui) { cf.set(d, key, n.map_or(Value::Null, |n| json!(n / scale))); } diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index ef78856db..0625fd3dc 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -95,17 +95,18 @@ impl<'a> NumField<'a> { pub fn measure(id: &'a str, value: Option, unit: Unit) -> Self { Self::new(id, value, NumKind::Measure(unit)) } + /// A number shown with `suffix` (`%`, `°`). A ` pt` suffix makes it a length in points that + /// reads any unit ([`Self::points`]), so no point field reads `13Q` as 13 pt. pub fn number(id: &'a str, value: Option, suffix: &'a str, decimals: usize) -> Self { + if suffix == " pt" { + return Self::points(id, value, decimals); + } Self::new(id, value, NumKind::Number { suffix, decimals }) } /// A length in points (type size, leading, stroke weight…): shows `12 pt`, reads any unit. pub fn points(id: &'a str, value: Option, decimals: usize) -> Self { Self::new(id, value, NumKind::Points { decimals }) } - /// [`Self::points`] for the ` pt` suffix, [`Self::number`] for any other. - pub fn with_suffix(id: &'a str, value: Option, suffix: &'a str, decimals: usize) -> Self { - if suffix == " pt" { Self::points(id, value, decimals) } else { Self::number(id, value, suffix, decimals) } - } fn new(id: &'a str, value: Option, kind: NumKind<'a>) -> Self { NumField { id, @@ -343,7 +344,7 @@ pub fn spin_measure(ui: &mut Ui, id: &str, value: Option, unit: Unit, width /// A plain number field (percent, degrees, counts). pub fn number(ui: &mut Ui, id: &str, value: Option, suffix: &str, width: f32, decimals: usize) -> Option { - NumField::with_suffix(id, value, suffix, decimals).width(width).show(ui) + NumField::number(id, value, suffix, decimals).width(width).show(ui) } /// A number field with the spinner segment. @@ -838,7 +839,10 @@ mod tests { assert!(close("5mm", 5.0 * designcraft_geom::units::PT_PER_MM)); assert!(close("9pt+1H", 9.0 + designcraft_geom::units::PT_PER_Q)); assert_eq!(k.parse("13x"), None, "an unknown unit is an invalid value, not 13 pt"); - assert_eq!(NumField::with_suffix("a", None, "%", 0).kind, NumKind::Number { suffix: "%", decimals: 0 }); + // Every ` pt` field reads units, whichever constructor made it (the Control panel's Font + // Size, Leading and Baseline Shift, Character ••• Baseline Shift…). + assert_eq!(NumField::number("a", None, " pt", 2).kind, NumKind::Points { decimals: 2 }); + assert_eq!(NumField::number("a", None, "%", 0).kind, NumKind::Number { suffix: "%", decimals: 0 }); } }