Merge pull request #2957 from dotCooCoo/feat/range-param-step

Form dialogs: a Range row can take a step (Param::step) for its slider and field
This commit is contained in:
BFlat
2026-11-26 10:25:07 +07:00
committed by GitHub
4 changed files with 63 additions and 9 deletions
@@ -557,6 +557,8 @@ impl NumberUnit {
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct RowOptions {
range_decimals: Option<u32>,
/// The step of a range row's slider and field ([`Param::step`]).
range_step: Option<f64>,
pub(crate) popup_slider: bool,
randomize: bool,
eyedropper: bool,
@@ -1230,6 +1232,18 @@ impl Param {
self
}
/// The step of a [`ParamKind::Range`] row, as `SliderField::step`: the slider and the field move by `step` on arrow
/// keys, the wheel and a scrub, and drags, clicks on the track and typed values snap to it. It takes the place of
/// the step [`decimals`](Param::decimals) implies; the stored value is still rounded to those decimals. A step that
/// is not finite and positive leaves the row's step as it was; other kinds are unchanged.
#[must_use]
pub fn step(mut self, step: f64) -> Self {
if matches!(self.kind, ParamKind::Range { .. }) && step.is_finite() && step > 0.0 {
self.options.get_or_insert_default().range_step = Some(step);
}
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 {
@@ -1488,6 +1502,12 @@ impl RowOptions {
self.range_decimals
}
/// The step a range row's slider and field use: the one [`Param::step`] set, else the one its decimals imply, or
/// `None` for the controls' own.
pub(crate) fn range_step(&self) -> Option<f64> {
self.range_step.or_else(|| self.range_decimals.map(|places| 10f64.powi(-(places as i32))))
}
/// Whether an int row has a Random button.
pub(crate) fn randomize(&self) -> bool {
self.randomize
@@ -850,8 +850,9 @@ impl<'p> Pass<'p> {
let value = self.work.number(line.key).unwrap_or(*range.start());
let option = line.param.option();
let places = option.range_places();
let step = option.range_step();
if option.popup_slider {
self.popup_range(ui, line, range, value, places);
self.popup_range(ui, line, range, value, places, step);
return;
}
let layout = self.sliders;
@@ -862,8 +863,8 @@ impl<'p> Pass<'p> {
if short {
slider = slider.short(true).field_sizing(Sizing::Content);
}
if let Some(places) = places {
slider = slider.step(10f64.powi(-(places as i32)));
if let Some(step) = step {
slider = slider.step(step);
}
if !option.marks().is_empty() {
slider = slider.marks_labelled(option.marks().iter().cloned());
@@ -900,10 +901,10 @@ impl<'p> Pass<'p> {
}
}
fn popup_range(&mut self, ui: &mut Ui, line: &Line<'_>, range: RangeInclusive<f64>, value: f64, places: Option<u32>) {
fn popup_range(&mut self, ui: &mut Ui, line: &Line<'_>, range: RangeInclusive<f64>, value: f64, places: Option<u32>, set_step: Option<f64>) {
// 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 step = set_step.unwrap_or(default_step);
let unit = line.param.unit.map(|kind| (kind, self.units.symbol(kind)));
let default = line.param.default_value().and_then(number_of).map(Some);
line.show(ui, |ui| {
@@ -911,11 +912,11 @@ impl<'p> Pass<'p> {
let mut field = NumberField::new(line.name.clone(), line.bind(Some(value), default))
.range(range.clone())
.popup_slider(range.clone())
.snap_popup_slider(places.is_some())
.snap_popup_slider(set_step.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 set_step.is_some() || default_step != 1.0 {
field = field.step(step).snap_typed(set_step.is_some());
}
if let Some((kind, _)) = &unit {
field = field.unit_kind(*kind).unit_placement(UnitPlacement::After);
@@ -28,7 +28,8 @@ fn patterns() -> PresetSource<'static, String> {
PresetSource::new((), PresetFns::new(pattern_rows))
}
/// One row of every kind the rows show, a section, a row that waits for a check box, a row with a unit menu, a
/// One row of every kind the rows show, a section, a row that waits for a check box, a range row with a step of a half,
/// a row with a unit menu, a
/// multi-line text row, a text row forty characters wide, a searchable choice, a radio row with a disabled option, a radio row with its options in a row,
/// a blend row, a pattern row, a files row in folder mode and a section of read-only rows.
fn params() -> Vec<Param> {
@@ -65,6 +66,7 @@ fn params() -> Vec<Param> {
params.push(Param::new("kernel", "Kernel", ParamKind::grid(9, 0)));
params.push(Param::new("sharpen", "Sharpen", ParamKind::bool(false)));
params.push(Param::new("amount", "Amount", ParamKind::range(0.0, 100.0, 50.0, Default::default())).enabled_when("sharpen", true));
params.push(Param::new("gain", "Gain", ParamKind::range(-30.0, 30.0, 0.0, Default::default())).step(0.5));
params.push(Param::new("notes", "Notes", ParamKind::text("")).multiline(3));
params.push(Param::new("caption", "Caption", ParamKind::text("CONFIDENTIAL")).field_chars(40));
params.push(Param::section("Camera Data"));
@@ -118,6 +118,37 @@ fn a_short_popup_slider_can_select_its_midpoint_without_explicit_precision() {
assert!((value - 0.0005).abs() <= 0.00002, "linear midpoint: {value}");
}
/// A range row with a step moves by it on the slider's arrow keys, snaps a typed value to it, and keeps it when its
/// slider is a popup; a step that is not positive leaves the row as it was.
#[trial]
fn a_range_row_moves_and_snaps_by_its_step() {
let gain = || Param::new("gain", "Gain", ParamKind::range(-30.0, 30.0, 0.0, SliderScale::Linear));
let values = Rc::new(RefCell::new(DialogFields::new()));
let mut fixture = rows(vec![gain().step(0.5)], &values);
let slider = fixture.by_role(Role::SLIDER).into_iter().next().expect("the slider");
fixture.click_at(slider.center()).settle();
assert_eq!(values.borrow().number("gain"), Some(0.0), "the middle of the track is on a step");
fixture.key(Key::ArrowRight, Modifiers::NONE).settle();
assert_eq!(values.borrow().number("gain"), Some(0.5), "Right moves one step");
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().expect("the number field");
fixture.click(&field).settle();
fixture.key(Key::A, Modifiers::COMMAND).type_text("3.3");
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(values.borrow().number("gain"), Some(3.5), "a typed value snaps to the step");
let popup = Rc::new(RefCell::new(DialogFields::new()));
let mut fixture = rows(vec![gain().popup_slider().step(0.5)], &popup);
let field = fixture.by_role(Role::SPIN_BUTTON).into_iter().next().expect("the popup row's field");
fixture.click(&field).settle();
fixture.key(Key::ArrowUp, Modifiers::NONE).settle();
assert_eq!(popup.borrow().number("gain"), Some(0.5), "Up on the popup row's field moves one step");
assert_eq!(gain().step(0.0), gain(), "a zero step is ignored");
assert_eq!(gain().step(f64::NAN), gain(), "a step that is no number is ignored");
assert_eq!(Param::new("on", "On", ParamKind::bool(false)).step(0.5), Param::new("on", "On", ParamKind::bool(false)), "other kinds are unchanged");
}
// 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]