From 9db1bef17f9505b5ebe2068bfb167d585ce029a9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 02:39:48 +0000 Subject: [PATCH] Value gestures: a host that keeps a number in f32 echoes the proposal rounded to it; Slider, SliderField, AngleDial and TransformBox read that echo as their own value (craft_behavior::same_in_f32), so a live drag runs on as one gesture --- crates/behavior/src/lib.rs | 1 + crates/behavior/src/value.rs | 6 ++++ crates/draw/src/ui/transform/transform_box.rs | 5 +-- crates/ui-widgets/src/form/slider_field.rs | 5 +++ .../ui-widgets/src/value_drag/angle_dial.rs | 5 +++ crates/ui-widgets/src/value_drag/slider.rs | 5 +++ .../ui-widgets/tests/it/values/write_back.rs | 31 +++++++++++++++++++ 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/crates/behavior/src/lib.rs b/crates/behavior/src/lib.rs index 20cc2e8de..f26cfb4c9 100644 --- a/crates/behavior/src/lib.rs +++ b/crates/behavior/src/lib.rs @@ -112,6 +112,7 @@ pub use sizing::{ControlMetrics, Metrics, Proposal}; pub use typing::{EditRun, Ended, EscapeRule, Escaped, Typed, TypingRun}; pub use value::{ Applied, CancelReason, EditEvent, EditEvents, EditPolicy, Gesture, GestureId, GestureNote, GesturePhase, Mixed, Preview, Validity, Value, + same_in_f32, }; pub use value_drag::{DragWarp, ValueDrag, wrap_value}; pub use wheel::{GESTURE_GAP, Wheel, WheelFocus, WheelGesture, WheelNotch, WheelPhase, WheelRoom, WheelSource, WheelTaker, WheelUnit}; diff --git a/crates/behavior/src/value.rs b/crates/behavior/src/value.rs index 35b2149a8..b87d41c25 100644 --- a/crates/behavior/src/value.rs +++ b/crates/behavior/src/value.rs @@ -10,6 +10,12 @@ pub trait Value: Clone + PartialEq + Send + Sync + 'static {} impl Value for T {} +/// Whether `host`, a number an app wrote back, is the proposal `proposed` as a host that keeps it in `f32` stores it: +/// within `f32` precision of it. Such a write-back is the gesture's own value, not a change of the document under it. +pub fn same_in_f32(proposed: f64, host: f64) -> bool { + proposed == host || (proposed - host).abs() <= 4.0 * f64::from(f32::EPSILON) * proposed.abs().max(host.abs()).max(1.0) +} + /// A value that may differ across a multi-selection. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Mixed { diff --git a/crates/draw/src/ui/transform/transform_box.rs b/crates/draw/src/ui/transform/transform_box.rs index a0487da93..37a7c70b2 100644 --- a/crates/draw/src/ui/transform/transform_box.rs +++ b/crates/draw/src/ui/transform/transform_box.rs @@ -906,10 +906,7 @@ impl Component for TransformBox<'_> { /// it: each corner and the pivot within f32 precision of the proposal is the proposal, not a change of the /// document under the drag. fn same_value(&self, proposed: &BoxState, host: &BoxState) -> bool { - let near = |a: Point, b: Point| { - let tolerance = |u: f64, v: f64| 4.0 * f64::from(f32::EPSILON) * u.abs().max(v.abs()).max(1.0); - (a.x - b.x).abs() <= tolerance(a.x, b.x) && (a.y - b.y).abs() <= tolerance(a.y, b.y) - }; + let near = |a: Point, b: Point| craft_behavior::same_in_f32(a.x, b.x) && craft_behavior::same_in_f32(a.y, b.y); proposed.quad.iter().zip(&host.quad).all(|(a, b)| near(*a, *b)) && near(proposed.pivot, host.pivot) } diff --git a/crates/ui-widgets/src/form/slider_field.rs b/crates/ui-widgets/src/form/slider_field.rs index 3c239af21..13b6b2a8f 100644 --- a/crates/ui-widgets/src/form/slider_field.rs +++ b/crates/ui-widgets/src/form/slider_field.rs @@ -313,6 +313,11 @@ impl Component for SliderField<'_> { Some(self.value.reborrow()) } + /// A host that keeps the value in `f32` writes the proposal back rounded to it: that is the gesture's own value. + fn same_value(&self, proposed: &f64, host: &f64) -> bool { + craft_behavior::same_in_f32(*proposed, *host) + } + fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let shown = shown_of(cx.value()); let name = cx.label(); diff --git a/crates/ui-widgets/src/value_drag/angle_dial.rs b/crates/ui-widgets/src/value_drag/angle_dial.rs index 992353158..a0518a233 100644 --- a/crates/ui-widgets/src/value_drag/angle_dial.rs +++ b/crates/ui-widgets/src/value_drag/angle_dial.rs @@ -289,6 +289,11 @@ impl Component for AngleDial<'_> { Some(self.value.reborrow()) } + /// A host that keeps the value in `f32` writes the proposal back rounded to it: that is the gesture's own value. + fn same_value(&self, proposed: &f64, host: &f64) -> bool { + craft_behavior::same_in_f32(*proposed, *host) + } + fn update(&mut self, cx: &mut Cx<'_, Self>) { let pointing = self.pointing(cx.rect, cx.style.dead_zone); let press = cx.part(FACE).press; diff --git a/crates/ui-widgets/src/value_drag/slider.rs b/crates/ui-widgets/src/value_drag/slider.rs index 869686758..e63553c07 100644 --- a/crates/ui-widgets/src/value_drag/slider.rs +++ b/crates/ui-widgets/src/value_drag/slider.rs @@ -235,6 +235,11 @@ impl Component for Slider<'_> { Some(self.value.reborrow()) } + /// A host that keeps the value in `f32` writes the proposal back rounded to it: that is the gesture's own value. + fn same_value(&self, proposed: &f64, host: &f64) -> bool { + craft_behavior::same_in_f32(*proposed, *host) + } + fn update(&mut self, cx: &mut Cx<'_, Self>) { let scale = self.track_scale(); let bounds = (scale.min, scale.max); diff --git a/crates/ui-widgets/tests/it/values/write_back.rs b/crates/ui-widgets/tests/it/values/write_back.rs index 825ffe139..a8b133626 100644 --- a/crates/ui-widgets/tests/it/values/write_back.rs +++ b/crates/ui-widgets/tests/it/values/write_back.rs @@ -69,3 +69,34 @@ fn a_write_back_at_the_codecs_precision_is_the_gestures_own() { let (cancels, _) = scrub(|value| value + OVERWRITE); assert_eq!(cancels, vec![CancelReason::HostChanged], "a different value is a host change"); } + +/// A slider field whose host keeps the value in float32 (a percentage stored as a fraction) gets each proposal back +/// rounded to it: the drag runs on and ends with one commit, with no host-change cancel. +#[trial] +fn a_slider_fields_drag_survives_a_float32_write_back() { + use craft_ui::form::SliderField; + use craft_ui::runtime::access::Role; + use craft_ui::testing::Fixture; + use std::rc::Rc; + let doc = Rc::new(RefCell::new(0.5_f32)); + let events = Rc::new(RefCell::new(Vec::new())); + let (held, seen) = (Rc::clone(&doc), Rc::clone(&events)); + let mut fixture = Fixture::new(move |ui| { + let given = f64::from(*held.borrow()) * 100.0; + let out = SliderField::new("Density", Bind::given(given), 0.0..=100.0).show(ui); + for event in &out.events { + if let Some(value) = event.landed() { + *held.borrow_mut() = (value / 100.0) as f32; + } + seen.borrow_mut().push(event.clone()); + } + }); + fixture.settle(); + let track = fixture.by_role(Role::SLIDER).first().expect("the slider").rect; + fixture.drag_at(track.left_center() + Vec2::new(track.width() * 0.3, 0.0), Vec2::new(track.width() * 0.237, 0.0)); + fixture.settle(); + let events = events.borrow(); + assert!(!events.iter().any(|event| matches!(event, EditEvent::Cancel { .. })), "no cancel: {events:?}"); + assert_eq!(events.iter().filter(|event| matches!(event, EditEvent::Commit { .. })).count(), 1, "one commit: {events:?}"); + assert!((f64::from(*doc.borrow()) - 0.5).abs() > 0.01, "the drag moved the value"); +}