From 23d397b6ad0510f97d29692f1ac4c021e9b786b0 Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:19:50 +0900 Subject: [PATCH 1/2] feat(ui): allow popup sliders for typed form ranges --- .../src/patterns/form_dialogs/param.rs | 8 ++++ .../src/patterns/form_dialogs/rows.rs | 42 +++++++++++++++++-- .../tests/it/form_dialogs/params.rs | 26 ++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/param.rs b/crates/ui-patterns/src/patterns/form_dialogs/param.rs index 2c154c7c2..7ea26b037 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/param.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/param.rs @@ -463,6 +463,7 @@ impl NumberUnit { #[derive(Clone, Debug, Default, PartialEq)] pub(crate) struct RowOptions { range_decimals: Option, + pub(crate) popup_slider: bool, randomize: bool, eyedropper: bool, eyedropper_armed: bool, @@ -782,6 +783,13 @@ impl Param { self } + /// Shows a Range row's slider in a linear popup opened from the number field. + #[must_use] + pub fn popup_slider(mut self) -> Self { + self.options.get_or_insert_default().popup_slider = true; + self + } + /// A Random button after the field of a [`ParamKind::Int`] row (a seed): it sets a random value within the row's /// bounds (an open side is `0` or `i32::MAX`); other kinds are unchanged. #[must_use] diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index 2ee03c78a..e6a7630ab 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -15,6 +15,7 @@ use crate::choice::{Checkbox, Radio, RadioGroup, Swatch}; use crate::contract::{ Bind, BodyCx, CanvasGesture, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked, }; +use crate::display::Text; use crate::form::{Field, FieldGroup, SliderField, SliderFieldLayout}; use crate::layout_components::{Row, Stack}; @@ -24,11 +25,11 @@ use crate::pickers::{Select, UnitSelect}; use crate::runtime::env::{Env, Label, UnitKind, Units, with_env}; use crate::sections::{HeaderStrength, Section}; -use crate::text::{NumberField, RangeAlert, SelectableText, TextField}; +use crate::text::{NumberField, RangeAlert, SelectableText, TextField, UnitPlacement}; use crate::value_drag::{AngleDial, PointPicker, SliderScale}; -use craft_behavior::{EditEvent, IconId, Mixed, NumberCodec}; +use craft_behavior::{EditEvent, IconId, Mixed, NumberCodec, decimals_for_step}; use craft_host::PickRequest; use craft_theme::Srgba; @@ -433,7 +434,12 @@ impl<'p> Pass<'p> { /// A range row: inline beside its label in the row's field, or stacked under its own caption across the form. fn range(&mut self, ui: &mut Ui, line: &Line<'_>, range: RangeInclusive, scale: SliderScale) { let value = self.work.number(line.key).unwrap_or(*range.start()); - let places = line.param.option().range_places(); + let options = line.param.option(); + let places = options.range_places(); + if options.popup_slider { + self.popup_range(ui, line, range, value, places); + return; + } let layout = self.sliders; let mut show = |ui: &mut Ui| { let mut slider = SliderField::new(line.name.clone(), Bind::given(value), range.clone()).scale(scale).layout(layout); @@ -462,6 +468,36 @@ impl<'p> Pass<'p> { } } + fn popup_range(&mut self, ui: &mut Ui, line: &Line<'_>, range: RangeInclusive, value: f64, places: Option) { + // Match the ordinary range row's number field precision when the caller sets no step. + let default_step = if range.end() - range.start() <= 10.0 { 0.01 } else { 1.0 }; + let step = places.map_or(default_step, |places| 10f64.powi(-(places as i32))); + let unit = line.param.unit.map(|kind| (kind, self.units.symbol(kind))); + line.show(ui, |ui| { + Row::new().show(ui, |ui, _| { + let mut field = NumberField::new(line.name.clone(), Bind::given(Some(value))) + .range(range.clone()) + .popup_slider(range.clone()) + .commit_decimals(decimals_for_step(step)) + .scrub_by_label(); + if places.is_some() || default_step != 1.0 { + field = field.step(step).snap_typed(places.is_some()); + } + if let Some((kind, _)) = &unit { + field = field.unit_kind(*kind).unit_placement(UnitPlacement::After); + } + let shown = field.show(ui); + if let Some((_, symbol)) = &unit { + Text::new(symbol.clone()).show(ui); + } + self.alert(shown.out_of_range); + if let Some(value) = shown.edit.value().copied().flatten() { + self.work.set(line.key, rounded(value, places)); + } + }); + }); + } + fn number(&mut self, ui: &mut Ui, line: &Line<'_>, range: Option>, step: f64, decimals: u32) { let value = self.work.number(line.key).unwrap_or(0.0); line.show(ui, |ui| { diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index bbeb493da..93c6ea42e 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -69,6 +69,32 @@ fn rows(params: Vec, values: &Rc>) -> Fixture { fixture } +/// The opt-in range keeps its numeric value and only opens its slider from the field's arrow. +#[trial] +fn a_popup_slider_opens_from_a_typed_range_row() { + let params = Param::parse_list(r#"{"scale":1..1000=100}"#); + let popup = vec![params[0].clone().popup_slider().unit(UnitKind::Percent)]; + let values = Rc::new(RefCell::new(DialogFields::new())); + let mut fixture = rows(popup.clone(), &values); + assert!(fixture.by_role(Role::SLIDER).is_empty(), "no permanent slider"); + let chevron = fixture.by_label("Slider").expect("the slider arrow"); + let unit = fixture.texts().iter().find(|run| run.text == "%").expect("the percent unit"); + assert!(unit.rect.left() >= chevron.rect.right(), "the unit follows the arrow"); + fixture.click(&chevron).settle(); + let slider = fixture.by_role(Role::SLIDER).into_iter().next().expect("the popup slider"); + fixture.click_at(slider.center()).settle(); + let value = values.borrow().number("scale").unwrap(); + assert!((value - 500.5).abs() <= 4.0, "linear midpoint: {value}"); + + assert!(set_param_field(&popup, &mut values.borrow_mut(), "scale", json!("125")).is_err()); + assert!(set_param_field(&popup, &mut values.borrow_mut(), "scale", json!(1001)).is_err()); + set_param_field(&popup, &mut values.borrow_mut(), "scale", json!(125.5)).unwrap(); + assert_eq!(ParamDialog::remembered(&popup, &values.borrow()).number("scale"), Some(125.5)); + + let standard = rows(params, &Rc::new(RefCell::new(DialogFields::new()))); + assert_eq!(standard.by_role(Role::SLIDER).len(), 1, "standard range uses its visible slider"); +} + // A row with `visible_when` is there only while its condition holds, and one with `enabled_when` takes input only // while its condition holds; a tooltip is the description of its row. #[trial] From f0942b4479b68f763e10b80c0ba1e464170a5375 Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:08:02 +0900 Subject: [PATCH 2/2] Keep popup sliders continuous without explicit precision --- .../ui-patterns/src/patterns/form_dialogs/rows.rs | 1 + .../ui-patterns/tests/it/form_dialogs/params.rs | 15 +++++++++++++++ crates/ui-widgets/src/text/number.rs | 4 ++++ crates/ui-widgets/src/text/number/popups.rs | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs index e6a7630ab..8534fa6c9 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/rows.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/rows.rs @@ -478,6 +478,7 @@ impl<'p> Pass<'p> { let mut field = NumberField::new(line.name.clone(), Bind::given(Some(value))) .range(range.clone()) .popup_slider(range.clone()) + .snap_popup_slider(places.is_some()) .commit_decimals(decimals_for_step(step)) .scrub_by_label(); if places.is_some() || default_step != 1.0 { diff --git a/crates/ui-patterns/tests/it/form_dialogs/params.rs b/crates/ui-patterns/tests/it/form_dialogs/params.rs index 93c6ea42e..8119b0b12 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/params.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/params.rs @@ -95,6 +95,21 @@ fn a_popup_slider_opens_from_a_typed_range_row() { assert_eq!(standard.by_role(Role::SLIDER).len(), 1, "standard range uses its visible slider"); } +#[trial] +fn a_short_popup_slider_can_select_its_midpoint_without_explicit_precision() { + let param = Param::new("scale", "Scale", ParamKind::range(0.0, 0.001, 0.0, SliderScale::Linear)).popup_slider(); + let values = Rc::new(RefCell::new(DialogFields::new())); + let mut fixture = rows(vec![param], &values); + let chevron = fixture.by_label("Slider").expect("the slider arrow"); + fixture.click(&chevron).settle(); + let slider = fixture.by_role(Role::SLIDER).into_iter().next().expect("the popup slider"); + + fixture.click_at(slider.center()).settle(); + + let value = values.borrow().number("scale").expect("the selected value"); + assert!((value - 0.0005).abs() <= 0.00002, "linear midpoint: {value}"); +} + // A row with `visible_when` is there only while its condition holds, and one with `enabled_when` takes input only // while its condition holds; a tooltip is the description of its row. #[trial] diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index b7a0d27f7..76f2dc992 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -135,6 +135,9 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option> { /// value at once, as far as the slider would, and the release closes the slider. #[prop] popup_slider: Option>, + /// Whether the popup slider snaps to the configured step; defaults to true. + #[prop] + snap_popup_slider: bool, /// A typed value goes to the nearest multiple of the step when it commits. #[prop] snap_typed: bool, @@ -202,6 +205,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { unit_placement: UnitPlacement::Inside, presets: None, popup_slider: None, + snap_popup_slider: true, snap_typed: false, automatic: false, commit_decimals: None, diff --git a/crates/ui-widgets/src/text/number/popups.rs b/crates/ui-widgets/src/text/number/popups.rs index b74135080..2fc97af7e 100644 --- a/crates/ui-widgets/src/text/number/popups.rs +++ b/crates/ui-widgets/src/text/number/popups.rs @@ -64,7 +64,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { let start = cx.value().and_then(NumberValue::number).unwrap_or_else(|| self.settle_stored((range.start() + range.end()) / 2.0)); let mut shown = start; let name = cx.label(); - let step = self.step.map(|step| self.to_stored(step)); + let step = self.step.filter(|_| self.snap_popup_slider).map(|step| self.to_stored(step)); let (rule, scale) = (self.rule(), self.scale); let edit = ui.show(popup, Overlay::popover(), |ui, _| { let slider = Slider::new(Bind::bound(&mut shown), range.clone()).label(name.clone());