mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 03:52:45 +01:00
Merge #2440: Allow NumberField to set a separate scrub step
This commit is contained in:
commit
c1eccbc5f6
3 files changed
+69
-3
No files matched your search
@@ -116,9 +116,12 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option<f64>> {
|
||||
/// The range typed, stepped and scrubbed values stay in.
|
||||
#[prop]
|
||||
range: Option<RangeInclusive<f64>>,
|
||||
/// 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<f64>,
|
||||
/// 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<f64>,
|
||||
/// 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")]
|
||||
@@ -203,6 +206,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,
|
||||
@@ -543,11 +547,12 @@ impl<V: NumberValue + 'static> 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<Scrub<V>> {
|
||||
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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<f64>) -> (Fixture, Rc<Cell<Option<f64>>>) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user