From e8ec7b44ae3cf3f2c69f3ffd2b89d4911f882a28 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 16:40:28 +0000 Subject: [PATCH] A unit select names its unit once: the number field reads the unit's spellings without a suffix, and the unit lists show the units' names (Units::name) --- crates/draw/src/ui/shape_drag/dialog.rs | 2 +- crates/ui-contract/src/runtime/env/units.rs | 43 ++++++++++++++++++- .../form_dialogs/new_document/details.rs | 4 +- .../form_dialogs/save_for_web/options.rs | 4 +- .../src/patterns/form_dialogs/sizing.rs | 2 +- crates/ui-widgets/src/pickers/unit_select.rs | 18 ++++---- .../src/pickers/unit_select/stories.rs | 2 +- 7 files changed, 58 insertions(+), 17 deletions(-) diff --git a/crates/draw/src/ui/shape_drag/dialog.rs b/crates/draw/src/ui/shape_drag/dialog.rs index c10d7fa24..ad6b6e35a 100644 --- a/crates/draw/src/ui/shape_drag/dialog.rs +++ b/crates/draw/src/ui/shape_drag/dialog.rs @@ -410,7 +410,7 @@ fn body( typed: &mut Option, ) -> bool { let document = values.units.unwrap_or_else(pixels_only); - let entries: Vec> = length_units(values).map(|kind| Entry::new(kind, Label::from(numbers.symbol(kind)))).collect(); + let entries: Vec> = length_units(values).map(|kind| Entry::new(kind, numbers.name(kind))).collect(); let kind = values.kind; FieldGroup::new(title_of(kind, words)).show(ui, |ui, _| { let (width, height) = (Label::from(words.width.as_str().to_owned()), Label::from(words.height.as_str().to_owned())); diff --git a/crates/ui-contract/src/runtime/env/units.rs b/crates/ui-contract/src/runtime/env/units.rs index 65a4cdeb1..5e9de334e 100644 --- a/crates/ui-contract/src/runtime/env/units.rs +++ b/crates/ui-contract/src/runtime/env/units.rs @@ -1,7 +1,7 @@ //! `Units`: what typed number input and unit readouts need from the env (core 2.1): the locale's separators and the //! unit spellings of a number's kind in the user's language, and the file sizes and durations written with them. -use super::EnvValue; +use super::{EnvValue, Label}; use craft_behavior::NumberCodec; use craft_behavior::quantity::{Reader, is_unit_spelling}; use craft_i18n::{FileSize, Format, WordSet as _, Words}; @@ -10,7 +10,8 @@ use std::ops::RangeInclusive; use std::sync::{Arc, OnceLock}; craft_ui::words! { - /// The unit words: `{n}` is where the number goes. + /// The unit words: `{n}` is where the number goes. The `_name` words name a unit in a list of units (a unit + /// select), without a number. pub struct UnitWords { pixels: unit "units.pixels" = "{n} px"; percent: unit "units.percent" = "{n}%"; @@ -28,6 +29,21 @@ craft_ui::words! { lines_per_centimetre: unit "units.linesPerCentimetre" = "{n} lpcm"; minutes: unit "units.minutes" = "{n} min"; columns: unit "units.columns" = "{n} col"; + pixels_name: "units.pixelsName" = "Pixels"; + percent_name: "units.percentName" = "Percent"; + degrees_name: "units.degreesName" = "Degrees"; + points_name: "units.pointsName" = "Points"; + times_name: "units.timesName" = "Times"; + inches_name: "units.inchesName" = "Inches"; + centimetres_name: "units.centimetresName" = "Centimetres"; + millimetres_name: "units.millimetresName" = "Millimetres"; + picas_name: "units.picasName" = "Picas"; + pixels_per_inch_name: "units.pixelsPerInchName" = "Pixels/Inch"; + pixels_per_centimetre_name: "units.pixelsPerCentimetreName" = "Pixels/Centimetre"; + minutes_name: "units.minutesName" = "Minutes"; + columns_name: "units.columnsName" = "Columns"; + lines_per_inch_name: "units.linesPerInchName" = "Lines/Inch"; + lines_per_centimetre_name: "units.linesPerCentimetreName" = "Lines/Centimetre"; } } @@ -342,6 +358,29 @@ impl Units { } } + /// The unit's name in the user's language, as a list of units shows it: Pixels, Pixels/Inch. + pub fn name(&self, kind: UnitKind) -> Label { + let words = &*self.words; + let name = match kind { + UnitKind::Pixels => &words.pixels_name, + UnitKind::Percent => &words.percent_name, + UnitKind::Degrees => &words.degrees_name, + UnitKind::Points => &words.points_name, + UnitKind::Times => &words.times_name, + UnitKind::Inches => &words.inches_name, + UnitKind::Centimetres => &words.centimetres_name, + UnitKind::Millimetres => &words.millimetres_name, + UnitKind::Picas => &words.picas_name, + UnitKind::PixelsPerInch => &words.pixels_per_inch_name, + UnitKind::PixelsPerCentimetre => &words.pixels_per_centimetre_name, + UnitKind::Minutes => &words.minutes_name, + UnitKind::Columns => &words.columns_name, + UnitKind::LinesPerInch => &words.lines_per_inch_name, + UnitKind::LinesPerCentimetre => &words.lines_per_centimetre_name, + }; + name.clone() + } + /// The unit's spelling in the user's language without the number: `mm`, `px`. pub fn symbol(&self, kind: UnitKind) -> String { self.template(kind).replace("{n}", "").trim().to_owned() diff --git a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs index 538136904..a287ae5e9 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs @@ -80,9 +80,9 @@ fn apply_number(session: &mut Session, events: &EditEvents>, set: im } } -/// The entries of a unit list, named by the units' symbols. +/// The entries of a unit list, named by the units' names. fn unit_entries(read: &Read<'_>, kinds: &[UnitKind]) -> Vec> { - kinds.iter().map(|kind| Entry::new(*kind, read.units.symbol(*kind))).collect() + kinds.iter().map(|kind| Entry::new(*kind, read.units.name(*kind))).collect() } /// Shows the column. diff --git a/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs b/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs index 4e9ef2b34..bc524cb97 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs @@ -333,8 +333,8 @@ fn metadata_row(ui: &mut Ui, words: &SaveForWebWords, options: &mut SaveForWebOp /// Image Size: Width and Height in pixels or percent, the Percent row and the Resample method. fn size_section(ui: &mut Ui, words: &SaveForWebWords, sizing: &SizingWords, units: &Units, link: &mut LinkedSize, resample: &mut Resample) { let kinds = [ - Entry::new(UnitKind::Pixels, Label::from(units.symbol(UnitKind::Pixels))), - Entry::new(UnitKind::Percent, Label::from(units.symbol(UnitKind::Percent))), + Entry::new(UnitKind::Pixels, units.name(UnitKind::Pixels)), + Entry::new(UnitKind::Percent, units.name(UnitKind::Percent)), ]; Section::fixed(words.image_size.clone()).id_salt("size").show(ui, |ui, _| { row(ui, &words.width, "width", |ui| { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs index cae7cfbc0..7e54cfa18 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs @@ -334,7 +334,7 @@ impl Kit { let env = Env::of(ui); let units = env.units(); let entries = - |kinds: &[UnitKind]| -> Vec> { kinds.iter().map(|kind| Entry::new(*kind, Label::from(units.symbol(*kind)))).collect() }; + |kinds: &[UnitKind]| -> Vec> { kinds.iter().map(|kind| Entry::new(*kind, units.name(*kind))).collect() }; let lengths = entries(&LENGTH_UNITS); let mut widths = lengths.clone(); widths.extend(entries(&[UnitKind::Columns])); diff --git a/crates/ui-widgets/src/pickers/unit_select.rs b/crates/ui-widgets/src/pickers/unit_select.rs index f36fc7a0b..e303f3374 100644 --- a/crates/ui-widgets/src/pickers/unit_select.rs +++ b/crates/ui-widgets/src/pickers/unit_select.rs @@ -4,10 +4,10 @@ use super::select::Select; use crate::contract::{ Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Entry, MeasureCx, Outcome, PaintUi, Props as _, Role, WordSetCx as _, }; -use crate::runtime::env::{Label, UnitKind}; +use crate::runtime::env::{Label, UnitKind, Units}; use crate::style::{ComponentStyle, StyleCx}; use crate::text::{NumberField, RangeAlert}; -use craft_behavior::{EditEvents, Flow, Metrics, Mixed, Proposal, Sense}; +use craft_behavior::{EditEvents, Flow, Metrics, Mixed, NumberCodec, Proposal, Sense}; use egui::{Rect, Response, Vec2, pos2}; use std::ops::{Deref, RangeInclusive}; @@ -59,9 +59,10 @@ impl Outcome for UnitSelectOutput { } } -/// A number field with a select of units after it. The select picks the unit kind the field reads typed text in and -/// shows its number with, and the kind sets the digits and the step of the number; the number itself is the field's value, and the unit goes to its own binding, so the app -/// converts the number when the unit changes. +/// A number field with a select of units after it. The select picks the unit kind the field reads typed text in +/// (its spellings: "12 mm") and shows its number with, and the kind sets the digits and the step of the number; the +/// select names the unit, so the field shows the number alone. The number itself is the field's value, and the unit +/// goes to its own binding, so the app converts the number when the unit changes. #[derive(crate::contract::Component, crate::contract::Props)] #[component] pub struct UnitSelect<'a> { @@ -102,7 +103,7 @@ impl<'a> UnitSelect<'a> { .label(label) } - fn number(&self, name: Label, value: Mixed>, kind: Option) -> NumberField<'static> { + fn number(&self, name: Label, value: Mixed>, kind: Option<(UnitKind, &Units)>) -> NumberField<'static> { let field = NumberField::new(name, Bind::given(value)); let field = match self.number_enabled { Some(on) => field.enabled(on), @@ -113,7 +114,7 @@ impl<'a> UnitSelect<'a> { None => field, }; match kind { - Some(kind) => field.unit_kind(kind).step(kind.step()), + Some((kind, units)) => field.codec(NumberCodec::new(units.reader(Some(kind)), *kind.decimals().end())).step(kind.step()), None => field, } } @@ -172,7 +173,8 @@ impl Component for UnitSelect<'_> { }; let shown = shown_of(cx.value()); let start = cx.value().and_then(|value| *value); - let number = ui.child("number", place(number_rect), self.number(group.clone(), shown, kind)); + let units = cx.env.units(); + let number = ui.child("number", place(number_rect), self.number(group.clone(), shown, kind.map(|kind| (kind, &units)))); cx.adopt(&number, &start, |value, mine| *mine = *value); self.out_of_range = number.out_of_range; let select = Select::new(self.select_name(&group), self.unit.reborrowed(), self.units); diff --git a/crates/ui-widgets/src/pickers/unit_select/stories.rs b/crates/ui-widgets/src/pickers/unit_select/stories.rs index ed6b65d14..9127d93b3 100644 --- a/crates/ui-widgets/src/pickers/unit_select/stories.rs +++ b/crates/ui-widgets/src/pickers/unit_select/stories.rs @@ -8,7 +8,7 @@ use craft_behavior::Mixed; use egui::Ui; fn units() -> [Entry; 3] { - [Entry::new(UnitKind::Millimetres, "mm"), Entry::new(UnitKind::Inches, "in"), Entry::new(UnitKind::Points, "pt")] + [Entry::new(UnitKind::Millimetres, "Millimetres"), Entry::new(UnitKind::Inches, "Inches"), Entry::new(UnitKind::Points, "Points")] } #[story(UnitSelect<'static>, covers = [Default], drive = Drive::Type("42"))]