From 8dc0a214ef6b0a9023ff5afdb4032c700f579fc3 Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:03:02 +0900 Subject: [PATCH] Allow NumberField to set a separate scrub step --- crates/ui-widgets/src/text/number.rs | 11 +++- crates/ui-widgets/tests/it/text/mod.rs | 1 + .../tests/it/text/number_scrub_step.rs | 60 +++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 crates/ui-widgets/tests/it/text/number_scrub_step.rs diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index b7a0d27f7..ec7e171b6 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -115,9 +115,12 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option> { /// The range typed, stepped and scrubbed values stay in. #[prop] range: Option>, - /// What one arrow press, one wheel notch and one point of scrub change; the default is 1. + /// What one arrow press, one wheel notch and one point of scrub change unless overridden. The default is 1. #[prop(quantity = "step")] step: Option, + /// The change per point of scrub, in shown units. An omitted, non-positive or non-finite rate uses the field step. + #[prop(quantity = "step")] + scrub_step: Option, /// Where an arrow press, a Page key and a wheel notch go from the current value, for values on a ladder (the zoom /// ladder); the step is ignored for them. The range still holds the result. #[prop(func = "Fn(f64, i32) -> f64")] @@ -197,6 +200,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { unit_kind: None, range: None, step: None, + scrub_step: None, stepper: None, unit: None, unit_placement: UnitPlacement::Inside, @@ -504,11 +508,12 @@ impl Component for NumberField<'_, V> { proposed == host || (proposed.named() == host.named() && at_precision(proposed) == at_precision(host)) } - /// A drag on the body of a field not in edit, or on its side label with `scrub_by_label`, moves a step a point; + /// A drag on the body of a field not in edit, or on its side label with `scrub_by_label`, moves by the scrub step per point; /// a drag on the popup slider's chevron moves the value as far as the same drag on the slider. fn scrub(&self) -> Option> { let rule = self.rule(); - let drag = ValueDrag::relative(rule.step, rule.bounds()).warp(true).hide_cursor(true).coarse(LARGE_STEPS).fine(1.0 / LARGE_STEPS); + let step = self.scrub_step.filter(|step| step.is_finite() && *step > 0.0).unwrap_or(rule.step); + let drag = ValueDrag::relative(step, rule.bounds()).warp(true).hide_cursor(true).coarse(LARGE_STEPS).fine(1.0 / LARGE_STEPS); // Rounded to the field's decimals, so a scrubbed value carries no float noise. The step is not a grid // here: an Alt drag moves by a share of it. let decimals = self.decimals(); diff --git a/crates/ui-widgets/tests/it/text/mod.rs b/crates/ui-widgets/tests/it/text/mod.rs index d6e55d6f6..7e0db9182 100644 --- a/crates/ui-widgets/tests/it/text/mod.rs +++ b/crates/ui-widgets/tests/it/text/mod.rs @@ -8,6 +8,7 @@ mod linked; mod mixed_blank; mod number; mod number_length; +mod number_scrub_step; mod selectable; mod text_field; mod widths; diff --git a/crates/ui-widgets/tests/it/text/number_scrub_step.rs b/crates/ui-widgets/tests/it/text/number_scrub_step.rs new file mode 100644 index 000000000..1e2a630f7 --- /dev/null +++ b/crates/ui-widgets/tests/it/text/number_scrub_step.rs @@ -0,0 +1,60 @@ +//! Numeric fields can keep fine discrete steps and a faster scrub. + +use craft_ui::contract::{Bind, Show as _, ShowContainer as _, SideLabel}; +use craft_ui::form::Field; +use craft_ui::testing::pointer::{moved, wheel_lines}; +use craft_ui::testing::{Fixture, trial}; +use craft_ui::text::NumberField; +use egui::{Key, Modifiers, vec2}; +use std::cell::Cell; +use std::rc::Rc; + +const START: f64 = 30.0; + +fn angle_field(scrub_step: Option) -> (Fixture, Rc>>) { + let value = Rc::new(Cell::new(Some(START))); + let shown = Rc::clone(&value); + let mut fixture = Fixture::new(move |ui| { + Field::new("Angle").side(SideLabel::new().scrub(true)).show(ui, |ui, _| { + let mut current = shown.get(); + let field = NumberField::new("Angle", Bind::bound(&mut current)).step(0.01).fixed_decimals(2).range(-359.99..=359.99).scrub_by_label(); + let field = match scrub_step { + Some(step) => field.scrub_step(step), + None => field, + }; + field.show(ui); + shown.set(current); + }); + }); + fixture.settle(); + (fixture, value) +} + +fn scrub_caption(fixture: &mut Fixture) { + let field = fixture.by_label("Angle").expect("the Angle number field"); + let caption = fixture.texts().iter().find(|text| text.text == "Angle").expect("the visible scrub caption").rect; + assert!(!field.rect.intersects(caption), "the caption is a separate handle"); + fixture.drag_at(caption.center(), vec2(50.0, 0.0)).settle(); +} + +#[trial] +fn a_separate_scrub_rate_keeps_fine_key_and_wheel_steps() { + let (mut fixture, value) = angle_field(Some(1.0)); + scrub_caption(&mut fixture); + assert_eq!(value.get(), Some(80.0), "50 points of caption drag add 50 degrees"); + let field = fixture.by_label("Angle").expect("the Angle number field"); + fixture.click(&field).key(Key::ArrowUp, Modifiers::NONE).settle(); + assert_eq!(value.get(), Some(80.01), "an arrow still adds one hundredth"); + fixture.frame(vec![moved(field.center())]); + fixture.frame(vec![wheel_lines(1.0)]).settle(); + assert_eq!(value.get(), Some(80.02), "one wheel notch still adds one hundredth"); +} + +#[trial] +fn an_omitted_or_invalid_scrub_rate_uses_the_field_step() { + for rate in [None, Some(0.0), Some(-1.0), Some(f64::NAN), Some(f64::INFINITY)] { + let (mut fixture, value) = angle_field(rate); + scrub_caption(&mut fixture); + assert_eq!(value.get(), Some(30.50), "the fallback rate for {rate:?} is 0.01 per point"); + } +}