From d1dbb74d0cf3e5315d9c8f96ea8403f75a7e11b2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 21:03:19 -0800 Subject: [PATCH] form_dialogs: a length Range row's field steps and snaps by the set step A Range row with Param::length and Param::step kept the step for the slider only. The permanent row's number field stepped by the shown unit's default, and the popup row's field stepped by the set step but did not snap typed values to it. SliderField now converts a set step from the stored unit to the shown unit for its length field and snaps typed values to it unless typed_decimals is set. Without a step the field keeps the shown unit's own step. The length field also takes the slider field's pointer modifiers, which the plain field already does. The popup row's length field snaps typed values to its set step. With points stored, inches shown and a step of 144 pt, Up on either field moves 2 in, and typing 1.4 stores 144 pt. --- .../src/patterns/form_dialogs/rows.rs | 4 +-- .../tests/it/form_dialogs/params.rs | 24 ++++++++++++++ crates/ui-widgets/src/form/slider_field.rs | 31 ++++++++++++++----- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index a8421e2971..c69c47df79 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -932,11 +932,11 @@ impl<'p> Pass<'p> { let bind = line.bind(Some(value), default); let mut field = match lengths { // A length field keeps the shown unit's decimals and, without a set step, its step; a set step is - // in the stored unit, so the field steps by it in the shown unit. + // in the stored unit, so the field steps by it in the shown unit and snaps typed values to it. Some(lengths) => { let field = NumberField::length(line.name.clone(), bind, lengths); match (set_step, self.units.length_scale(lengths)) { - (Some(step), Some(scale)) => field.step(step * scale), + (Some(step), Some(scale)) => field.step(step * scale).snap_typed(true), _ => field, } } diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 05e51842b4..3f1c0eb621 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -1236,6 +1236,30 @@ fn a_range_row_shows_a_length_in_the_shown_unit() { assert!(stepped.is_some_and(|points| (points - 144.0).abs() < 1e-6), "one step of 72 pt: {stepped:?}"); } +/// A range row with lengths and a set step, with a permanent slider and with a popup one: the step is in the stored +/// unit, so Up on the field moves by it, and a typed value goes to its nearest multiple. +#[trial] +fn a_length_range_row_steps_and_snaps_its_field_by_the_set_step() { + let lengths = craft_ui::runtime::env::LengthUnits::new(UnitKind::Points, UnitKind::Inches, 72.0); + let depth = Param::new("depth", "Depth", ParamKind::range(0.0, 720.0, 144.0, SliderScale::Linear)).length(lengths).step(144.0); + for popup in [false, true] { + let row = if popup { depth.clone().popup_slider() } else { depth.clone() }; + let values = Rc::new(RefCell::new(DialogFields::new())); + values.borrow_mut().set("depth", FieldValue::Number(144.0)); + let mut fixture = rows(vec![row], &values); + fixture.room(Vec2::new(1280.0, 800.0)).settle(); + let tree = fixture.tree(); + let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().find(|target| tree.name_of(target.node) == Some("Depth")).expect("the field"); + fixture.click(&field).settle(); + fixture.key(Key::ArrowUp, Modifiers::NONE).settle(); + let stepped = values.borrow().number("depth"); + assert!(stepped.is_some_and(|points| (points - 288.0).abs() < 1e-6), "one step of 144 pt, popup {popup}: {stepped:?}"); + fixture.key(Key::A, Modifiers::COMMAND).type_text("1.4").key(Key::Enter, Modifiers::NONE).settle(); + let typed = values.borrow().number("depth"); + assert!(typed.is_some_and(|points| (points - 144.0).abs() < 1e-6), "1.4 in snaps to 2 in, 144 pt, popup {popup}: {typed:?}"); + } +} + /// A row marked `beside_previous` stands on the line of the row before it, where an unmarked one stands below. #[trial] fn a_row_beside_the_previous_one_shares_its_line() { diff --git a/crates/ui-widgets/src/form/slider_field.rs b/crates/ui-widgets/src/form/slider_field.rs index db0502d1f2..4837470e38 100644 --- a/crates/ui-widgets/src/form/slider_field.rs +++ b/crates/ui-widgets/src/form/slider_field.rs @@ -7,7 +7,7 @@ use crate::contract::{ Bind, BindCheck, BindMut, Common, Component, Cx, Edit as _, EditResponse, FieldLayout, Focus as _, MeasureCx, Outcome, PaintUi, Props as _, Role, SideLabel, Sizing, Validity, WordSetCx as _, }; -use crate::runtime::env::{Label, LengthUnits, UnitKind}; +use crate::runtime::env::{EnvView, Label, LengthUnits, UnitKind}; use crate::runtime::keys::{ScalarKeyPolicy, ScalarRow, register_scalar}; use crate::style::{ComponentStyle, StyleCx}; use crate::text::{NumberField, RangeAlert}; @@ -131,7 +131,9 @@ pub struct SliderField<'a> { unit_kind: Option, /// The lengths the value is kept and shown in ([`NumberField::length`]): the binding, the range, the slider and its /// step stay in `lengths.stored`, and the number field shows and reads the value in `lengths.shown`, at that unit's - /// own step and decimals. `step`, `typed_decimals`, `field_decimals` and `unit_kind` then apply to the slider only. + /// own decimals. A set `step` is in `lengths.stored`: the field steps by it converted to `lengths.shown` and snaps + /// typed values to it unless `typed_decimals` is set; without a `step` the field keeps the shown unit's own step. + /// `typed_decimals`, `field_decimals` and `unit_kind` then apply to the slider only. #[prop] lengths: Option, /// A unit shown after the number, for a quantity with no `UnitKind` (a font axis). @@ -369,8 +371,14 @@ impl Look { } } - /// The number field showing `shown`, with the composite's check as the slider has it. - fn field<'c>(&self, name: &Label, shown: Mixed, check: Check<'c>) -> NumberField<'c> { + /// How many shown units make one stored unit for a length field; `None` without lengths or for a pair with no scale. + fn length_scale(&self, env: &EnvView<'_>) -> Option { + self.lengths.and_then(|lengths| env.units().length_scale(lengths)) + } + + /// The number field showing `shown`, with the composite's check as the slider has it. `length_scale` is + /// [`Look::length_scale`]'s answer. + fn field<'c>(&self, name: &Label, shown: Mixed, check: Check<'c>, length_scale: Option) -> NumberField<'c> { let number = match shown { Mixed::Same(value) => Mixed::Same(Some(value)), Mixed::Mixed => Mixed::Mixed, @@ -383,8 +391,15 @@ impl Look { bind = bind.check_with(check, |check, value| value.map_or(Validity::Ok, |value| lifted(check.validity(&value)))); } if let Some(lengths) = self.lengths { - // The step, the snap and the decimals below are in the stored unit; a length field keeps the shown unit's own. - let field = NumberField::length(name.clone(), bind, lengths).range(self.range.clone()).scrub_by_label(); + // The decimals below are in the stored unit; a length field keeps the shown unit's own. A set step is in the + // stored unit, so the field steps by it in the shown unit and snaps typed values to it; without one the field + // keeps the shown unit's own step. + let field = + NumberField::length(name.clone(), bind, lengths).range(self.range.clone()).scrub_by_label().pointer_modifiers(self.pointer_modifiers); + let field = match (self.set_step(), length_scale) { + (Some(step), Some(scale)) => field.step(step * scale).snap_typed(self.typed_decimals.is_none()), + _ => field, + }; return self.finish_field(field); } let step = self.field_step(); @@ -484,7 +499,7 @@ impl Component for SliderField<'_> { let name = cx.label(); let look = self.look(); let slider = cx.child("slider", &mut look.slider(&name, shown, None, None), Proposal::Ideal); - let field = cx.child("field", &mut look.field(&name, shown, None), Proposal::Ideal); + let field = cx.child("field", &mut look.field(&name, shown, None, look.length_scale(&cx.env)), Proposal::Ideal); if cx.proposal == Proposal::Ideal { self.field_width = field.x; } @@ -537,7 +552,7 @@ impl Component for SliderField<'_> { // The group has no stop, so its tooltip, and with it the reason it is disabled, is carried by the first stop. let face = self.track_face.take(); let bind = self.value.reborrow(); - let field_view = look.field(&name, shown, Some(bind.check_of())); + let field_view = look.field(&name, shown, Some(bind.check_of()), look.length_scale(&cx.env)); // A read-only field's track is read-only too: no press on it edits the value, whatever `track_press` is, and an // inert composite arms no first-Down gesture in its child for a later writable release. let slider = look.slider(&name, shown, Some(bind.check_of()), face).read_only(cx.resolved.read_only);