mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 02:32:33 +00:00
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
This commit is contained in:
@@ -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};
|
||||
|
||||
@@ -10,6 +10,12 @@ pub trait Value: Clone + PartialEq + Send + Sync + 'static {}
|
||||
|
||||
impl<T: Clone + PartialEq + Send + Sync + 'static> 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<T> {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user