From 8f60c0fb4bea41ae28c937f92a1f76f8340c8d02 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 24 Nov 2026 14:49:30 +0100 Subject: [PATCH] Slider and SliderField: TrackPress names when a track press sets the value; a read-only SliderField's track is read-only Review: the per-call flag propose_on_primary_down becomes a named option, TrackPress { OnRelease, OnPress } (#[non_exhaustive], OnRelease by default, so nothing changes unless an app asks), set with track_press on Slider and on SliderField, each with a story. A SliderField passed read-only to its slider only when the press option was on, so a read-only field without it kept a writable track. It now passes it always. The new trial checks a click, a held press and a drag on a read-only field's track leave the value and report no edit, with the option unset, OnRelease and OnPress. --- crates/ui-widgets/src/form/slider_field.rs | 22 ++++++++-------- .../src/form/slider_field/stories.rs | 9 ++++++- crates/ui-widgets/src/value_drag/mod.rs | 2 +- crates/ui-widgets/src/value_drag/slider.rs | 25 ++++++++++++++----- .../src/value_drag/slider/stories.rs | 9 ++++++- .../tests/it/value_drag/primary_down.rs | 21 ++++++++++++++++ .../tests/it/value_drag/primary_down_route.rs | 6 ++--- 7 files changed, 70 insertions(+), 24 deletions(-) diff --git a/crates/ui-widgets/src/form/slider_field.rs b/crates/ui-widgets/src/form/slider_field.rs index 55deb4c57..4fab8e5a4 100644 --- a/crates/ui-widgets/src/form/slider_field.rs +++ b/crates/ui-widgets/src/form/slider_field.rs @@ -10,7 +10,7 @@ use crate::contract::{ use crate::runtime::env::{Label, UnitKind}; use crate::style::{ComponentStyle, StyleCx}; use crate::text::{NumberField, RangeAlert}; -use crate::value_drag::{Slider, SliderScale, SliderThumb, TrackFace}; +use crate::value_drag::{Slider, SliderScale, SliderThumb, TrackFace, TrackPress}; use craft_behavior::{Flow, Metrics, Mixed, Proposal, Sense, decimals_for_step}; use egui::{Rect, Response, Vec2, pos2}; use std::ops::{Deref, RangeInclusive}; @@ -77,11 +77,9 @@ pub struct SliderField<'a> { /// The change of one wheel notch over the slider where it is coarser than the step. #[prop(quantity = "step")] wheel_step: Option, - /// Begin a genuine value gesture on the first primary track press and propose its landing in that same pass. - /// False preserves click/drag timing. Thumb presses and keyboard, wheel and editor behavior are unchanged. + /// When a press on the slider's track sets the value (`Slider::track_press`): on its release or drag by default. #[prop] - propose_on_primary_down: bool, - + track_press: TrackPress, /// What the number counts: the field shows its unit and reads its spellings. #[prop] unit_kind: Option, @@ -156,7 +154,7 @@ impl<'a> SliderField<'a> { step: None, typed_decimals: None, wheel_step: None, - propose_on_primary_down: false, + track_press: TrackPress::OnRelease, unit_kind: None, unit: None, fill: true, @@ -188,7 +186,7 @@ impl<'a> SliderField<'a> { /// What the children are built from, apart from the binding and the track face. fn look(&self) -> Look { Look { - propose_on_primary_down: self.propose_on_primary_down, + track_press: self.track_press, range: self.range.clone(), scale: self.scale, step: self.step, @@ -217,7 +215,7 @@ enum FieldMarks { /// What a `SliderField` builds its two children from. struct Look { - propose_on_primary_down: bool, + track_press: TrackPress, range: RangeInclusive, scale: SliderScale, step: Option, @@ -258,7 +256,7 @@ impl Look { bind = bind.check_with(check, BindCheck::validity); } let slider = Slider::new(bind, self.range.clone()) - .propose_on_primary_down(self.propose_on_primary_down) + .track_press(self.track_press) .scale(self.scale) .fill(self.fill) .thumb(self.thumb) @@ -415,9 +413,9 @@ impl Component for SliderField<'_> { SliderFieldLayout::Inline => ui.child("field", place(field), field_view), }; self.out_of_range = typed.out_of_range.clone(); - let slider = look.slider(&name, shown, Some(bind.check_of()), face); - // An inert composite must not arm a hidden first-Down gesture in its child for a later writable release. - let slider = if self.propose_on_primary_down { slider.read_only(cx.resolved.read_only) } else { slider }; + // A read-only field's track is read-only too: no press on it edits the value, whatever `track_press` is, and an + // inert composite arms no first-Down gesture in its child for a later writable release. + let slider = look.slider(&name, shown, Some(bind.check_of()), face).read_only(cx.resolved.read_only); let dragged = ui.child("slider", place(track), slider); let start = *self.range.start(); cx.adopt(&typed, &start, |value, mine| { diff --git a/crates/ui-widgets/src/form/slider_field/stories.rs b/crates/ui-widgets/src/form/slider_field/stories.rs index ec77bd617..a8b0b6cbf 100644 --- a/crates/ui-widgets/src/form/slider_field/stories.rs +++ b/crates/ui-widgets/src/form/slider_field/stories.rs @@ -4,7 +4,7 @@ use super::SliderField; use crate::contract::{Props as _, Show as _, Validity}; use crate::runtime::env::{Message, UnitKind}; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use crate::value_drag::SliderScale; +use crate::value_drag::{SliderScale, TrackPress}; use craft_behavior::Mixed; use egui::Ui; @@ -34,6 +34,13 @@ fn slider_field_typed_precision(ui: &mut Ui, data: &mut StoryData) -> StoryOutpu SliderField::new("Size", data.bind("precise_size", 36.1231), 1.0..=100.0).step(1.0).typed_decimals(4).show(ui).into() } +/// A press on the track sets the value as the button goes down (`TrackPress::OnPress`): a brush setting whose +/// preview should answer the first touch. +#[story(SliderField<'static>, covers = [], drive = Drive::Never)] +fn slider_field_track_on_press(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + SliderField::new("Flow", data.bind("flow", 60.0), PERCENT).unit_kind(UnitKind::Percent).track_press(TrackPress::OnPress).show(ui).into() +} + #[story(SliderField<'static>, covers = [Mixed], drive = Drive::Never)] fn slider_field_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { SliderField::new("Opacity", data.bind_mixed("mixed", Mixed::Mixed), PERCENT).show(ui).into() diff --git a/crates/ui-widgets/src/value_drag/mod.rs b/crates/ui-widgets/src/value_drag/mod.rs index 12d65961e..9c38d9495 100644 --- a/crates/ui-widgets/src/value_drag/mod.rs +++ b/crates/ui-widgets/src/value_drag/mod.rs @@ -11,7 +11,7 @@ mod words; pub use angle_dial::{AngleDial, AngleDialStyle, AngleOrientation}; pub use point_picker::{Backdrop, PointMark, PointPicker, PointPickerStyle, PointPickerWords, SamePoint}; pub use range_slider::{RangeSlider, RangeSliderStyle}; -pub use slider::{Slider, SliderStyle, SliderThumb}; +pub use slider::{Slider, SliderStyle, SliderThumb, TrackPress}; pub use thumb_row::{ Anchor, Follow, Neighbours, OffThumbs, Order, RowOp, RowRules, SpanInk, Thumb, ThumbHold, ThumbOp, ThumbRow, ThumbRowOutput, ThumbRowStyle, ThumbSpan, diff --git a/crates/ui-widgets/src/value_drag/slider.rs b/crates/ui-widgets/src/value_drag/slider.rs index 867d68500..a6a73206a 100644 --- a/crates/ui-widgets/src/value_drag/slider.rs +++ b/crates/ui-widgets/src/value_drag/slider.rs @@ -85,10 +85,10 @@ pub struct Slider<'a> { /// moves along the track and ignores it. #[prop(quantity = "step")] wheel_step: Option, - /// Begin a genuine value gesture on the first primary track press and propose its landing in that same pass. - /// False preserves click/drag timing. Thumb presses and keyboard, wheel and editor behavior are unchanged. + /// When a press on the track sets the value: on its release or drag (the default), or as the primary button goes + /// down. Presses on the thumb and keyboard, wheel and editor input do not change. #[prop] - propose_on_primary_down: bool, + 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. @@ -107,6 +107,19 @@ pub struct Slider<'a> { short: bool, } +/// When a press on a slider's track sets the value. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum TrackPress { + /// The value jumps to the press when it ends as a click, or follows the pointer once the press starts a drag; a + /// press held still changes nothing yet. + #[default] + OnRelease, + /// The value lands where the primary button goes down, in the same pass, and a drag goes on from there: an + /// editor whose preview should answer the first touch. A double click still restores the reset value. + OnPress, +} + /// The shape of a slider's thumb. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] @@ -135,7 +148,7 @@ impl<'a> Slider<'a> { thumb: SliderThumb::Round, marks: Marks::default(), wheel_step: None, - propose_on_primary_down: false, + track_press: TrackPress::OnRelease, value_text: None, track_face: None, trim: None, @@ -235,7 +248,7 @@ impl Component for Slider<'_> { cx.part(track::GROOVE, self.face_rect(cx.style, along)); } let sense = Sense::CLICK | Sense::DRAG | Sense::TAKE_FOCUS; - let sense = if self.propose_on_primary_down { sense | Sense::SETS_ON_PRESS } else { sense }; + let sense = if self.track_press == TrackPress::OnPress { sense | Sense::SETS_ON_PRESS } else { sense }; cx.part(TRACK, along.area).sense(sense).value_part().pointer_only("the arrow keys step the value"); cx.part(Self::THUMB, thumb) .sense(Sense::DRAG | Sense::TAKE_FOCUS) @@ -268,7 +281,7 @@ impl Component for Slider<'_> { let press_at = |cx: &Cx<'_, Self>, x: f32| pick.press(&[along.x(scale.position(current(cx)))], x); // Multi-clicks keep the legacy track path: reset wins on Down, and a later drag // can still begin from the reset value through the existing DragStart handling. - let follow_track_on_down = self.propose_on_primary_down + let follow_track_on_down = self.track_press == TrackPress::OnPress && cx.part(TRACK).press.phase.start().is_none_or(|start| start.button != craft_behavior::PointerButton::Primary || start.clicks < 2); if follow_track_on_down { let press = cx.part(TRACK).press; diff --git a/crates/ui-widgets/src/value_drag/slider/stories.rs b/crates/ui-widgets/src/value_drag/slider/stories.rs index 26859e8c9..5eb6ec958 100644 --- a/crates/ui-widgets/src/value_drag/slider/stories.rs +++ b/crates/ui-widgets/src/value_drag/slider/stories.rs @@ -1,7 +1,7 @@ //! Stories of `Slider`, in the old gallery's groups: Values, Step, Disabled and Right to left as form rows, then the //! groups the old gallery did not have. -use super::{Slider, SliderScale, SliderThumb}; +use super::{Slider, SliderScale, SliderThumb, TrackPress}; use crate::contract::{Props as _, Show as _, ShowContainer as _, Validity}; use crate::display::Progress; use crate::form::Field; @@ -56,6 +56,13 @@ fn slider_triangle_ticks(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { Slider::new(data.bind_ranged("value", 30.0, RANGE), RANGE).thumb(SliderThumb::Triangle).ticks(10).label("Triangle").show(ui).into() } +/// A press on the track sets the value as the button goes down, not on its release (`TrackPress::OnPress`): the +/// thumb answers the first touch, as an editor's live preview wants. +#[story(Slider<'static>, covers = [], drive = Drive::Never, group = "Track press", title = "On press")] +fn slider_track_on_press(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + Slider::new(data.bind_ranged("value", 50.0, RANGE), RANGE).track_press(TrackPress::OnPress).label("On press").show(ui).into() +} + /// A slider of the "Step" group: steps of `step` from `start`. fn step_row(ui: &mut Ui, data: &mut StoryData, step: f64, start: f64) -> StoryOutput { Slider::new(data.bind_ranged("value", start, RANGE), RANGE).step(step).label(format!("Step {step}")).show(ui).into() diff --git a/crates/ui-widgets/tests/it/value_drag/primary_down.rs b/crates/ui-widgets/tests/it/value_drag/primary_down.rs index e3c462ed7..c38fdf316 100644 --- a/crates/ui-widgets/tests/it/value_drag/primary_down.rs +++ b/crates/ui-widgets/tests/it/value_drag/primary_down.rs @@ -554,3 +554,24 @@ fn a_live_wheel_commits_before_opted_track_down_and_the_fresh_press_keeps_draggi assert_eq!(rig.state.borrow().values[0], 0.6); } } + +/// A read-only `SliderField` keeps its value whatever its track press option: a click, a press held still and a drag on +/// its track change nothing and report no edit. Before, only a field with the press option passed read-only to its +/// slider, so a read-only field without it kept a writable track. +#[trial] +fn a_read_only_slider_field_ignores_track_presses_with_either_track_press_option() { + for policy in [None, Some(false), Some(true)] { + let mut rig = Rig::new(true, policy); + let geometry = rig.geometry(0); + rig.state.borrow_mut().read_only = true; + rig.fixture.settle(); + rig.clear(); + rig.fixture.click_at(geometry.at(0.8)); + rig.fixture.settle(); + rig.fixture.drag_hold_at(geometry.at(0.6), Vec2::new(40.0, 0.0)); + rig.fixture.release_at(geometry.at(0.6) + Vec2::new(40.0, 0.0)); + rig.fixture.settle(); + assert_eq!(rig.state.borrow().values[0], START, "a read-only field keeps its value ({policy:?})"); + assert!(rig.state.borrow().events.iter().all(Vec::is_empty), "and reports no edit ({policy:?})"); + } +} diff --git a/crates/ui-widgets/tests/it/value_drag/primary_down_route.rs b/crates/ui-widgets/tests/it/value_drag/primary_down_route.rs index 7a15524fa..461bc2ad9 100644 --- a/crates/ui-widgets/tests/it/value_drag/primary_down_route.rs +++ b/crates/ui-widgets/tests/it/value_drag/primary_down_route.rs @@ -1,10 +1,10 @@ //! Only the real capability route differs across the causal pair; all physical inputs and assertions are identical. use craft_ui::form::SliderField; -use craft_ui::value_drag::Slider; +use craft_ui::value_drag::{Slider, TrackPress}; pub(super) fn slider(value: Slider<'_>, policy: Option, grid: Option, fine_shift: bool) -> Slider<'_> { let _ = (grid, fine_shift); match policy { - Some(enabled) => value.propose_on_primary_down(enabled), + Some(enabled) => value.track_press(if enabled { TrackPress::OnPress } else { TrackPress::OnRelease }), None => value, } } @@ -12,7 +12,7 @@ pub(super) fn slider(value: Slider<'_>, policy: Option, grid: Option, pub(super) fn field(value: SliderField<'_>, policy: Option, grid: Option, fine_shift: bool) -> SliderField<'_> { let _ = (grid, fine_shift); match policy { - Some(enabled) => value.propose_on_primary_down(enabled), + Some(enabled) => value.track_press(if enabled { TrackPress::OnPress } else { TrackPress::OnRelease }), None => value, } }