mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-12 13:24:56 +00:00
Number fields: every " pt" field reads the unit typed
The Control panel's Font Size, Leading and Baseline Shift and Character ••• Baseline Shift still used NumField::number with " pt", which read 13Q as 13 pt. NumField::number now makes a points field for the " pt" suffix, so no point field can miss it; with_suffix is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013phQ3ekq6uGBEWD6FjFYG2
This commit is contained in:
1 parent
98af30bbd8
commit
2c401cb543
3 files changed
+12
-8
No files matched your search
@@ -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)));
|
||||
|
||||
@@ -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)));
|
||||
}
|
||||
|
||||
@@ -95,17 +95,18 @@ impl<'a> NumField<'a> {
|
||||
pub fn measure(id: &'a str, value: Option<f64>, 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<f64>, 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<f64>, 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<f64>, 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<f64>, kind: NumKind<'a>) -> Self {
|
||||
NumField {
|
||||
id,
|
||||
@@ -343,7 +344,7 @@ pub fn spin_measure(ui: &mut Ui, id: &str, value: Option<f64>, unit: Unit, width
|
||||
|
||||
/// A plain number field (percent, degrees, counts).
|
||||
pub fn number(ui: &mut Ui, id: &str, value: Option<f64>, suffix: &str, width: f32, decimals: usize) -> Option<f64> {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user