diff --git a/crates/behavior/src/lib.rs b/crates/behavior/src/lib.rs index f26cfb4c9..7ad5665c7 100644 --- a/crates/behavior/src/lib.rs +++ b/crates/behavior/src/lib.rs @@ -112,7 +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, + same_in_f32, same_in_f32_at, }; 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 42391e6f6..6c9fa770c 100644 --- a/crates/behavior/src/value.rs +++ b/crates/behavior/src/value.rs @@ -13,7 +13,13 @@ 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) + same_in_f32_at(proposed, host, 0.0) +} + +/// [`same_in_f32`] for a number the host computes in `f32` from terms as large as `magnitude`: a corner of a box it +/// transforms lies near zero while the terms it sums do not, and holds their precision, not its own. +pub fn same_in_f32_at(proposed: f64, host: f64, magnitude: f64) -> bool { + proposed == host || (proposed - host).abs() <= 4.0 * f64::from(f32::EPSILON) * magnitude.max(proposed.abs()).max(host.abs()).max(1.0) } /// A value that may differ across a multi-selection. diff --git a/crates/draw/src/ui/transform/transform_box.rs b/crates/draw/src/ui/transform/transform_box.rs index c78a3a74b..cc9232870 100644 --- a/crates/draw/src/ui/transform/transform_box.rs +++ b/crates/draw/src/ui/transform/transform_box.rs @@ -927,9 +927,13 @@ impl Component for TransformBox<'_> { /// A host keeps the box in f32 (a layer's transform, a text layer's box) and gives a proposal back rounded to /// it: each corner and the pivot within f32 precision of the proposal is the proposal, not a change of the - /// document under the drag. + /// document under the drag. The host computes every corner from the same box and transform, so a corner near + /// the origin holds the precision of the box's largest coordinate, not its own. fn same_value(&self, proposed: &BoxState, host: &BoxState) -> bool { - let near = |a: Point, b: Point| craft_behavior::same_in_f32(a.x, b.x) && craft_behavior::same_in_f32(a.y, b.y); + let points = proposed.quad.iter().chain(&host.quad).chain([&proposed.pivot, &host.pivot]); + let magnitude = points.fold(0.0f64, |most, point| most.max(point.x.abs()).max(point.y.abs())); + let same = |a: f64, b: f64| craft_behavior::same_in_f32_at(a, b, magnitude); + let near = |a: Point, b: Point| same(a.x, b.x) && same(a.y, b.y); proposed.quad.iter().zip(&host.quad).all(|(a, b)| near(*a, *b)) && near(proposed.pivot, host.pivot) }