From 8da5ec92a5ab7680f6b706d54db430ee74a6aa54 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 12:33:34 +0700 Subject: [PATCH] =?UTF-8?q?ui-widgets:=20NumberLook::Inline=20sets=20a=20n?= =?UTF-8?q?umber=20field's=20value=20and=20unit=20as=20one=20run=20at=20it?= =?UTF-8?q?s=20start=20(720=20pt,=200=C2=B0,=20100%)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-contract/src/runtime/env/units.rs | 6 + crates/ui-widgets/src/text/mod.rs | 3 +- crates/ui-widgets/src/text/number.rs | 104 ++++++++++++++---- crates/ui-widgets/src/text/number/rule.rs | 35 +++--- crates/ui-widgets/src/text/number/stories.rs | 53 ++++++++- crates/ui-widgets/tests/it/text/mod.rs | 1 + .../ui-widgets/tests/it/text/number_look.rs | 72 ++++++++++++ 7 files changed, 234 insertions(+), 40 deletions(-) create mode 100644 crates/ui-widgets/tests/it/text/number_look.rs diff --git a/crates/ui-contract/src/runtime/env/units.rs b/crates/ui-contract/src/runtime/env/units.rs index 30cefec0a..52647c4b6 100644 --- a/crates/ui-contract/src/runtime/env/units.rs +++ b/crates/ui-contract/src/runtime/env/units.rs @@ -467,6 +467,12 @@ impl Units { self.template(kind).replace("{n}", "").trim().to_owned() } + /// The text before and after the number in the user's language, spacing included: `("", " mm")`, `("", "%")`. + pub fn affixes(&self, kind: UnitKind) -> (String, String) { + let template = self.template(kind); + template.split_once("{n}").map_or_else(|| (String::new(), template.to_owned()), |(before, after)| (before.to_owned(), after.to_owned())) + } + /// The digits after the decimal mark `n` needs within `decimals`: the fewest that show it as it rounds at the most. fn digits(n: f64, decimals: &RangeInclusive) -> usize { let (least, most) = (*decimals.start(), *decimals.end()); diff --git a/crates/ui-widgets/src/text/mod.rs b/crates/ui-widgets/src/text/mod.rs index c924d21fe..615cb624e 100644 --- a/crates/ui-widgets/src/text/mod.rs +++ b/crates/ui-widgets/src/text/mod.rs @@ -19,7 +19,8 @@ pub use chips::{ChipCodec, ChipField, ChipFieldOutput, ChipFieldStyle, ChipWords pub use hex::{HexCodec, HexField}; pub use linked::{LinkMode, LinkedFields, LinkedFieldsStyle, LinkedLayout, LinkedOutput, LinkedWords, follow_equal, follow_ratio, link_toggle}; pub use number::{ - NamedOr, NumberField, NumberName, NumberOutput, NumberPick, NumberStepper, NumberStyle, NumberValue, RangeAlert, StepArrows, UnitPlacement, + NamedOr, NumberField, NumberLook, NumberName, NumberOutput, NumberPick, NumberStepper, NumberStyle, NumberValue, RangeAlert, StepArrows, + UnitPlacement, }; pub use selectable_text::{SelectableStyle, SelectableText}; pub use text_field::TextField; diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index f8671b977..61cf25d9a 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -16,7 +16,7 @@ use crate::contract::{ PartsCx, Pointer as _, Props as _, Role, Scrub, Sizing, Styled as _, WordSetCx as _, }; use crate::primitives::{TextOptions, TextRun}; -use crate::runtime::env::{Label, LengthUnits, UnitKind, Units}; +use crate::runtime::env::{EnvView, Label, LengthUnits, UnitKind, Units}; use crate::runtime::frame; use crate::style::{ComponentStyle, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole}; use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps}; @@ -70,6 +70,21 @@ pub enum StepArrows { Leading, } +/// How a number field sets its value and unit. An app sets it once at its root (`RootEnv::values`); the default is +/// the centred number. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)] +#[env(style)] +#[non_exhaustive] +pub enum NumberLook { + /// The number centred in the mono face, the unit apart at the end of the value area in caption text, so a column + /// of fields lines its units up; a percentage stands against its sign at the end. + #[default] + Centred, + /// The number and its unit as one run ("720 pt", "0°", "100%") at the field's start, in the interface face and + /// the value's colour: editing shows the same text, and the caret and the selection go through the whole run. + Inline, +} + /// The placeholder of a mixed field. const BLANK: &str = "\u{2014}"; @@ -190,6 +205,8 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option> { lengths: Option, /// How many shown units make one stored unit, read from the env in measure and parts; 1 for a plain field. scale: f64, + /// The scope's look, read from the env in measure and parts. + look: NumberLook, options: TextOptions, positive_only: bool, scrub: bool, @@ -268,6 +285,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { max_decimals: None, lengths: None, scale: 1.0, + look: NumberLook::Centred, options: TextOptions::default(), positive_only: false, scrub: false, @@ -326,22 +344,59 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { Rule::new(range.as_ref(), self.step) } - /// Reads a length field's scale from stored to shown from the env. - fn settle_lengths(&mut self, units: &Units) { + /// Reads the scope's look and a length field's scale from stored to shown from the env. The status bar's zoom + /// well keeps its own look. + fn settle(&mut self, env: &EnvView<'_>) { + self.look = if self.status_zoom { NumberLook::Centred } else { env.get() }; if let Some(lengths) = self.lengths { - self.scale = units.length_scale(lengths).unwrap_or(1.0); + self.scale = env.units().length_scale(lengths).unwrap_or(1.0); } } - /// The unit drawn inside the field after the number: the field's own, else the symbol of its unit kind; none when - /// the host draws it after the field. + /// Whether the field draws its unit inside the box, not the host after the field. + fn unit_inside(&self) -> bool { + !self.status_zoom && self.unit_placement == UnitPlacement::Inside + } + + /// The unit drawn apart inside the field after the number: the field's own, else the symbol of its unit kind; + /// none when the host draws it after the field or the number's run holds it. fn shown_unit(&self, units: &Units) -> Option