Merge #2376: Add popup sliders for typed form ranges

This commit is contained in:
David Mann committed 2026-11-20 12:51:35 +01:00
commit 285a5f6fce
5 files changed
+94 -4

No files matched your search

@@ -463,6 +463,7 @@ impl NumberUnit {
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct RowOptions {
range_decimals: Option<u32>,
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]
@@ -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<f64>, 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,37 @@ impl<'p> Pass<'p> {
}
}
fn popup_range(&mut self, ui: &mut Ui, line: &Line<'_>, range: RangeInclusive<f64>, value: f64, places: Option<u32>) {
// 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())
.snap_popup_slider(places.is_some())
.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<RangeInclusive<f64>>, step: f64, decimals: u32) {
let value = self.work.number(line.key).unwrap_or(0.0);
line.show(ui, |ui| {
@@ -69,6 +69,47 @@ fn rows(params: Vec<Param>, values: &Rc<RefCell<DialogFields>>) -> 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");
}
#[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]
+4
View File
@@ -135,6 +135,9 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option<f64>> {
/// value at once, as far as the slider would, and the release closes the slider.
#[prop]
popup_slider: Option<RangeInclusive<f64>>,
/// 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,
+1 -1
View File
@@ -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());