From 21051f8d27aa9bbb3870d4f7cf5e40d175405fa6 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 01:53:42 +0000 Subject: [PATCH] Kerning field: the doc says what it shows for Metrics and Optical; a trial holds a named number value to its row label and the typed name --- crates/text/src/ui/character.rs | 3 +- crates/ui-widgets/tests/it/text/number.rs | 39 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/text/src/ui/character.rs b/crates/text/src/ui/character.rs index 4fa52000c..d4254c5b2 100644 --- a/crates/text/src/ui/character.rs +++ b/crates/text/src/ui/character.rs @@ -346,7 +346,8 @@ impl Outcome for CharacterControlsOutput { /// the field (presets are shortcuts, not limits: any value in range can be typed). Ranges in points: size /// 0.1–1296, leading 0.1–5000, baseline shift −1296–1296; tracking and kerning −1000–10000; scales 0–1000 %. The /// leading menu starts with an Auto row ([`Leading::Auto`]); while leading is Auto the field shows the font size -/// × 120 %. The kerning menu starts with Metrics and Optical; the field is empty while either is the kerning. +/// × 120 %. The kerning menu starts with Metrics and Optical; while either is the kerning the field shows its name, +/// and the name typed sets it. /// - Family: craft-font-manager's `FontPicker` with `font-picker` and [`font_picker`](Self::font_picker), else a /// `Select` of `families`. /// - Toggles are toggle buttons in one row, pressed while on; Tab reaches each. diff --git a/crates/ui-widgets/tests/it/text/number.rs b/crates/ui-widgets/tests/it/text/number.rs index 05d8f6c65..888f5d198 100644 --- a/crates/ui-widgets/tests/it/text/number.rs +++ b/crates/ui-widgets/tests/it/text/number.rs @@ -240,4 +240,43 @@ fn an_inside_glyph_label_scrubs_the_field_in_edit_too() { fixture.drag_at(glyph.center(), Vec2::new(30.0, 0.0)); fixture.idle(2); assert!(value.get().unwrap_or_default() > START, "a drag on the glyph in edit raised the value"); + +/// The one name of `named_field`'s values, tied to its presets row. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Metrics; + +impl craft_ui::text::NumberName for Metrics { + fn id(self) -> u32 { + METRICS_ROW + } + + fn from_id(id: u32) -> Option { + (id == METRICS_ROW).then_some(Metrics) + } +} + +const METRICS_ROW: u32 = 7; + +/// A field that holds a name shows its presets row's label as its text, and typing the name over a number commits the +/// name (kerning's Metrics). +#[trial] +fn a_named_value_shows_its_row_label_and_the_typed_name_commits_it() { + use craft_ui::text::NamedOr; + let value = std::rc::Rc::new(RefCell::new(NamedOr::Named(Metrics))); + let (held, id) = (value.clone(), std::rc::Rc::new(Cell::new(egui::Id::NULL))); + let shown_id = id.clone(); + let presets = vec![Entry::new(NumberPick::Choice(METRICS_ROW), "Metrics"), Entry::new(NumberPick::Number(25.0), "25")]; + let mut fixture = craft_ui::testing::Fixture::new(move |ui| { + let mut guard = held.borrow_mut(); + let field = NumberField::named("Kerning", Bind::bound(&mut *guard)).id_salt("kerning").presets(&presets); + shown_id.set(field.show(ui).response.id); + }); + fixture.settle(); + assert_eq!(fixture.tree().value(id.get()), Some("Metrics"), "a named value shows its row's label"); + *value.borrow_mut() = NamedOr::Number(5.0); + fixture.settle(); + let field = fixture.by_label("Kerning").expect("the field"); + fixture.click(&field).settle(); + fixture.key(Key::A, Modifiers::COMMAND).type_text("metrics").key(Key::Enter, Modifiers::NONE).settle(); + assert_eq!(*value.borrow(), NamedOr::Named(Metrics), "the typed name commits the name"); }