diff --git a/crates/ui-contract/src/runtime/env/units.rs b/crates/ui-contract/src/runtime/env/units.rs index 52647c4b6..fd82d62b4 100644 --- a/crates/ui-contract/src/runtime/env/units.rs +++ b/crates/ui-contract/src/runtime/env/units.rs @@ -36,6 +36,7 @@ craft_ui::words! { hyphens: unit "units.hyphens" = "{n} hyphens"; minutes: unit "units.minutes" = "{n} min"; columns: unit "units.columns" = "{n} col"; + per_inch: unit "units.perInch" = "{n}/in"; pixels_name: "units.pixelsName" = "Pixels"; percent_name: "units.percentName" = "Percent"; degrees_name: "units.degreesName" = "Degrees"; @@ -58,6 +59,7 @@ craft_ui::words! { ha_name: "units.haName" = "Ha"; letters_name: "units.lettersName" = "Letters"; hyphens_name: "units.hyphensName" = "Hyphens"; + per_inch_name: "units.perInchName" = "Per Inch"; } } @@ -148,6 +150,9 @@ pub enum UnitKind { Letters, /// `{n} hyphens`: a hyphenation setting counted in hyphens. Hyphens, + /// `{n}/in`: how many of something fall in an inch of length, such as the points a distortion adds along a path. + /// It is a count, not a length: [`per_inch`](Self::per_inch) answers `None` for it. + PerInch, } impl UnitKind { @@ -161,7 +166,7 @@ impl UnitKind { Self::ALL.get(tag as usize).copied() } - const ALL: [UnitKind; 22] = [ + const ALL: [UnitKind; 23] = [ UnitKind::Pixels, UnitKind::Percent, UnitKind::Degrees, @@ -184,6 +189,7 @@ impl UnitKind { UnitKind::Ha, UnitKind::Letters, UnitKind::Hyphens, + UnitKind::PerInch, ]; /// The digits after the decimal mark a number of this kind shows by default, at most. @@ -200,7 +206,8 @@ impl UnitKind { | UnitKind::PixelsPerInch | UnitKind::PixelsPerCentimetre | UnitKind::LinesPerInch - | UnitKind::LinesPerCentimetre => 0..=2, + | UnitKind::LinesPerCentimetre + | UnitKind::PerInch => 0..=2, UnitKind::Inches | UnitKind::Columns => 0..=3, } } @@ -429,6 +436,7 @@ impl Units { UnitKind::Ha => words.ha.as_str(), UnitKind::Letters => words.letters.as_str(), UnitKind::Hyphens => words.hyphens.as_str(), + UnitKind::PerInch => words.per_inch.as_str(), } } @@ -458,6 +466,7 @@ impl Units { UnitKind::Ha => &words.ha_name, UnitKind::Letters => &words.letters_name, UnitKind::Hyphens => &words.hyphens_name, + UnitKind::PerInch => &words.per_inch_name, }; name.clone() } diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 3ef09b666..8c38ea71c 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -12,6 +12,7 @@ use craft_ui::patterns::presets::{PresetFns, PresetRow, PresetSource}; use craft_ui::runtime::env::{Label, Scale, UnitKind}; use craft_ui::testing::relations::{above, left_of}; use craft_ui::testing::{Fixture, trial}; +use craft_ui::text::NumberLook; use craft_ui::value_drag::SliderScale; use egui::{Key, Modifiers, Vec2}; use serde_json::json; @@ -828,6 +829,45 @@ fn parameter_dialogs_15539_show_declared_units_and_keep_parsing() { } } +/// A count per inch shows `/in` after its number, apart in the field or in the number's own run in the inline look, and +/// a typed number or an edit of that run is read back as the count. +#[trial] +fn a_count_per_inch_row_shows_per_inch_and_reads_the_count() { + let detail = vec![Param::new("detail", "Detail", ParamKind::number(Some(0.0), Some(100.0), 10.0, 1.0, 0)).unit(UnitKind::PerInch)]; + for inline in [false, true] { + let values = Rc::new(RefCell::new(DialogFields::new())); + let shown = values.clone(); + let params = detail.clone(); + let mut fixture = Fixture::new(move |ui| { + let _ = ParamDialog::new("Distort", "Distort", params.clone()).values(Bind::bound(&mut *shown.borrow_mut())).show(ui); + }); + if inline { + fixture.root_value(NumberLook::Inline); + } + fixture.room(Vec2::new(1280.0, 800.0)).settle(); + let field = fixture.by_label("Detail").expect("the number field"); + let runs: Vec = fixture + .texts() + .iter() + .filter(|text| !text.elided && text.clip.contains_rect(text.rect) && field.rect.contains_rect(text.rect)) + .map(|text| text.text.clone()) + .collect(); + let unit = runs.iter().filter(|run| run.ends_with("/in")).count(); + assert_eq!(unit, 1, "inline {inline}: one visible /in on Detail: {runs:?}"); + assert!(runs.iter().any(|run| run.starts_with("10")), "inline {inline}: the count shows: {runs:?}"); + + fixture.click(&field).settle(); + if inline { + // The run holds "10/in": an edit at its start keeps the unit's text, which is read back with the number. + fixture.key(Key::Home, Modifiers::NONE).key(Key::Delete, Modifiers::NONE).type_text("4"); + } else { + fixture.key(Key::A, Modifiers::COMMAND).type_text("40"); + } + fixture.key(Key::Tab, Modifiers::NONE).settle(); + assert_eq!(values.borrow().number("detail"), Some(40.0), "inline {inline}: the edited count commits"); + } +} + /// Scale's merge: a `{w, h}` object keeps what it leaves out from the held value; anything else is refused. fn merge_scale(fields: &mut DialogFields, key: &str, value: serde_json::Value) -> Result<(), String> { let serde_json::Value::Object(incoming) = value else { return Err(format!("{key} takes an object")) };