A transformed box's corner near the origin holds the precision of the box's largest coordinate: a host's f32 write-back of a rotated text box no longer reads as a document change that cancels the handle drag after its first step

This commit is contained in:
aiongg
2026-11-17 08:02:42 +00:00
parent f4eb126da2
commit 5ccb18f828
3 changed files with 14 additions and 4 deletions
+1 -1
View File
@@ -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};
+7 -1
View File
@@ -13,7 +13,13 @@ 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)
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.
@@ -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)
}