diff --git a/crates/ui-contract/src/runtime/access.rs b/crates/ui-contract/src/runtime/access.rs index 3875e52017..c33b6d4522 100644 --- a/crates/ui-contract/src/runtime/access.rs +++ b/crates/ui-contract/src/runtime/access.rs @@ -293,6 +293,7 @@ pub struct Access { pub(crate) unit: Option, pub(crate) color: Option, pub(crate) numeric: Option, + pub(crate) variable_steps: Option<(bool, bool)>, pub(crate) expanded: Option, pub(crate) has_popup: Option, pub(crate) level: Option, @@ -363,6 +364,12 @@ impl Access { self } + /// Offers the available Increment and Decrement actions for a numeric value with no fixed step size. + pub fn variable_steps(&mut self, increment: bool, decrement: bool) -> &mut Self { + self.variable_steps = Some((increment, decrement)); + self + } + /// Whether a disclosure is open, when no flag says it. pub fn expanded(&mut self, on: bool) -> &mut Self { self.expanded = Some(on); diff --git a/crates/ui-contract/src/runtime/access/emit.rs b/crates/ui-contract/src/runtime/access/emit.rs index 50c92339ae..c117a209c0 100644 --- a/crates/ui-contract/src/runtime/access/emit.rs +++ b/crates/ui-contract/src/runtime/access/emit.rs @@ -161,7 +161,14 @@ fn write_position(spec: &mut NodeSpec, position: Option, level: Opt } /// The actions the interaction and the numeric facts declare. -fn declare_actions(spec: &mut NodeSpec, focusable: bool, clickable: bool, context_menu: bool, numeric: Option<&Numeric>) { +fn declare_actions( + spec: &mut NodeSpec, + focusable: bool, + clickable: bool, + context_menu: bool, + numeric: Option<&Numeric>, + variable_steps: Option<(bool, bool)>, +) { // A read-only value offers no action that would change it. let numeric = numeric.filter(|_| !spec.read_only); if focusable { @@ -173,17 +180,22 @@ fn declare_actions(spec: &mut NodeSpec, focusable: bool, clickable: bool, contex if context_menu { spec.actions.push(NodeAction::ContextMenu); } - if let Some(numeric) = numeric.filter(|numeric| numeric.step.is_some()) { + if let Some(numeric) = numeric { // A step that cannot move the value is not offered: no Increment at the range's end, unless the value wraps. let stuck = |at_end: bool| at_end && !numeric.wraps; - if !stuck(numeric.value.is_some_and(|value| value >= *numeric.range.end())) { + let (increment, decrement) = variable_steps.unwrap_or_else(|| { + let has_step = numeric.step.is_some(); + ( + has_step && !stuck(numeric.value.is_some_and(|value| value >= *numeric.range.end())), + has_step && !stuck(numeric.value.is_some_and(|value| value <= *numeric.range.start())), + ) + }); + if increment { spec.actions.push(NodeAction::Increment); } - if !stuck(numeric.value.is_some_and(|value| value <= *numeric.range.start())) { + if decrement { spec.actions.push(NodeAction::Decrement); } - } - if numeric.is_some() { spec.actions.push(NodeAction::SetValue); } } @@ -308,7 +320,7 @@ pub(crate) fn report(ctx: &Context, own: &Own<'_>) { if let Some(expanded) = expanded { spec.actions.push(if expanded { NodeAction::Collapse } else { NodeAction::Expand }); } - declare_actions(&mut spec, own.focusable, own.clickable, own.context_menu, numeric); + declare_actions(&mut spec, own.focusable, own.clickable, own.context_menu, numeric, access.variable_steps); declare_scroll_actions(&mut spec, (own.scrolls.0 || access.pans.0, own.scrolls.1 || access.pans.1)); custom_actions(&mut spec, &access.custom_actions); write_node(ctx, own.id, &spec); @@ -404,7 +416,7 @@ pub fn report_sub(ctx: &Context, decl: &SubDecl<'_>) { spec.sort = node.sort; spec.sortable = node.sortable; write_states(&mut spec, flags, None, decl.enabled, false); - declare_actions(&mut spec, decl.focusable, decl.clickable, false, numeric); + declare_actions(&mut spec, decl.focusable, decl.clickable, false, numeric, None); if node.steps && numeric.is_none() { spec.actions.extend([NodeAction::Increment, NodeAction::Decrement]); } diff --git a/crates/ui-widgets/src/text/number/rule.rs b/crates/ui-widgets/src/text/number/rule.rs index 4368203c97..62056b348f 100644 --- a/crates/ui-widgets/src/text/number/rule.rs +++ b/crates/ui-widgets/src/text/number/rule.rs @@ -8,8 +8,7 @@ use std::borrow::Cow; use std::marker::PhantomData; use std::ops::RangeInclusive; -/// The value one step from a value in a direction: the up arrow, the wheel and the Page keys of a field whose values -/// lie on a ladder instead of a grid (`NumberField::stepper`). The direction is `1` up and `-1` down. +/// A custom step for a number field or slider whose values lie on a ladder instead of a grid. pub struct NumberStepper<'a>(Box f64 + 'a>); impl<'a> NumberStepper<'a> { @@ -19,7 +18,7 @@ impl<'a> NumberStepper<'a> { NumberStepper(Box::new(next)) } - pub(super) fn next(&self, from: f64, direction: i32, scale: f64) -> f64 { + pub(crate) fn next(&self, from: f64, direction: i32, scale: f64) -> f64 { (self.0)(from, direction, scale) } } diff --git a/crates/ui-widgets/src/value_drag/slider.rs b/crates/ui-widgets/src/value_drag/slider.rs index e2dc3e183d..d7089cba0e 100644 --- a/crates/ui-widgets/src/value_drag/slider.rs +++ b/crates/ui-widgets/src/value_drag/slider.rs @@ -12,6 +12,9 @@ use crate::contract::{ use crate::runtime::env::Label; use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse}; use crate::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, SurfaceStyle, TextLook, TrackLook}; +use crate::text::NumberStepper; +use craft_behavior::intent::{MoveMods, NavMove}; +use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps, step_value}; use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Proposal, Sense, WheelPhase}; use craft_theme::Stateful; use egui::{Pos2, Rect, Vec2}; @@ -70,6 +73,13 @@ pub struct Slider<'a> { /// no stops. #[prop(quantity = "step")] step: Option, + /// Where keys, accessibility actions and the wheel step from the current value. The direction is 1 or -1; + /// the magnitude counts steps. A ladder steps in that direction without wrapping. The callback is also + /// queried for the available accessibility actions. + /// It takes precedence over the fixed key and wheel steps. Pointer placement, including snapping, + /// still uses the track's scale. + #[prop(func = "Fn(f64, i32, f64) -> f64")] + stepper: Option>, /// How positions along the track map to values. #[prop] scale: SliderScale, @@ -82,7 +92,7 @@ pub struct Slider<'a> { marks: Marks, /// The change of one wheel notch where it is coarser than the step (an offset of 0.0001 steps that wants 0.01 a /// notch); the step by default. Shift and Alt scale it as they scale the step. A logarithmic track with no step - /// moves along the track and ignores it. + /// moves along the track and ignores it. A custom stepper also overrides it. #[prop(quantity = "step")] wheel_step: Option, /// When a press on the track sets the value: on its release or drag (the default), or as the primary button goes @@ -90,8 +100,8 @@ pub struct Slider<'a> { #[prop] track_press: TrackPress, /// The value as the caller words it ("50 %"), in place of the slider's own value text: the number in the step's - /// decimals, or in one decimal without a step. Assistive technology reads it, and so does a viewer that - /// shows the value beside the slider. + /// decimals, in one decimal without a step, or at the local step's precision with a custom stepper. Assistive + /// technology reads it, and so does a viewer that shows the value beside the slider. #[prop(into)] value_text: Option