From a3100efcf07c9a235758664b97eec5dcb56f9af9 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 3 Nov 2026 21:04:22 +0100 Subject: [PATCH] Free Transform: Shift-rotate snaps the turn made by the drag, not the absolute angle With Shift held, the box now turns in 15 degree steps from the angle it started the drag at. A box at 25.59 degrees dragged by about 20 degrees ends at 40.59, not at an absolute multiple of 15. From 0 degrees the results are the same as before. Fixes #11099 Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/src/lib.rs | 3 + .../src/tools/transform_tool/drag_modes.rs | 6 +- .../src/tools/type_tool/canvas_tests.rs | 9 ++- .../src/transform_rotate_snap_tests.rs | 73 +++++++++++++++++++ 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/transform_rotate_snap_tests.rs diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index b9a1380e9a..8c159a3899 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -794,6 +794,9 @@ mod transform_switch_tests; #[cfg(test)] mod transform_undo_tests; +#[cfg(test)] +mod transform_rotate_snap_tests; + #[cfg(test)] mod transform_type_tests; diff --git a/crates/ui-egui/src/tools/transform_tool/drag_modes.rs b/crates/ui-egui/src/tools/transform_tool/drag_modes.rs index 5b416845de..f7bbd01b08 100644 --- a/crates/ui-egui/src/tools/transform_tool/drag_modes.rs +++ b/crates/ui-egui/src/tools/transform_tool/drag_modes.rs @@ -231,10 +231,10 @@ pub(crate) fn apply_drag(s: &mut TransformSession, g: Gesture, p: [f64; 2], mods let a1 = (p[1] - g.pivot0[1]).atan2(p[0] - g.pivot0[0]); let mut da = a1 - a0; if mods.shift { - // Snap the resulting angle to 15° steps. - let base = (g.quad0[1][1] - g.quad0[0][1]).atan2(g.quad0[1][0] - g.quad0[0][0]); + // Snap the turn made by this drag to 15° steps; the angle the box started at is + // kept (#11099). let step = 15f64.to_radians(); - da = ((base + da) / step).round() * step - base; + da = (da / step).round() * step; } let (sn, cs) = da.sin_cos(); let c = g.pivot0; diff --git a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs index 38baecc450..65a06ae936 100644 --- a/crates/ui-egui/src/tools/type_tool/canvas_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/canvas_tests.rs @@ -1382,7 +1382,9 @@ fn temporary_type_transform_free_scale_center_scale_move_skew_and_rotation() { } "rotate" | "snap" => { let a = [frame.quad[1][0] + 20.0, frame.quad[1][1] - 10.0]; - let da: f64 = 0.42; + // A snapped drag turns in 15° steps from where it started, so the first preview steps need a + // turn of at least half a step to change anything (#11099). + let da: f64 = if gesture == "snap" { 0.9 } else { 0.42 }; let (x, y) = (a[0] - frame.pivot[0], a[1] - frame.pivot[1]); let b = [frame.pivot[0] + x * da.cos() - y * da.sin(), frame.pivot[1] + x * da.sin() + y * da.cos()]; (a, b, if gesture == "snap" { type_command() | Modifiers::SHIFT } else { type_command() }) @@ -1420,8 +1422,9 @@ fn temporary_type_transform_free_scale_center_scale_move_skew_and_rotation() { .mul(&initial), ); } else if gesture == "snap" { - let angle = after.transform.m[1].atan2(after.transform.m[0]).to_degrees(); - assert!((angle / 15.0 - (angle / 15.0).round()).abs() < 1e-8); + // Shift snaps the turn made by the drag, not the absolute angle (#11099). + let turn = (after.transform.m[1].atan2(after.transform.m[0]) - initial.m[1].atan2(initial.m[0])).to_degrees(); + assert!((turn / 15.0 - (turn / 15.0).round()).abs() < 1e-8); } else if gesture == "center" { let p = after.transform.apply(Point::new((frame.rect[0] + frame.rect[2]) / 2.0, (frame.rect[1] + frame.rect[3]) / 2.0)); assert!((p.x - frame.pivot[0]).abs() < 1e-8 && (p.y - frame.pivot[1]).abs() < 1e-8); diff --git a/crates/ui-egui/src/transform_rotate_snap_tests.rs b/crates/ui-egui/src/transform_rotate_snap_tests.rs new file mode 100644 index 0000000000..8048b1b6fb --- /dev/null +++ b/crates/ui-egui/src/transform_rotate_snap_tests.rs @@ -0,0 +1,73 @@ +//! Free Transform: Shift while rotating snaps the turn made by the drag to 15° steps and keeps +//! the angle the box started at (#11099). + +use crate::state::TransformSession; +use crate::transform_tool::{Gesture, Hit, apply_drag, hit}; + +/// A 200 × 100 box turned by `deg` degrees about its centre (100, 50). +fn session(deg: f64) -> TransformSession { + let (cx, cy) = (100.0, 50.0); + let (sn, cs) = deg.to_radians().sin_cos(); + let turn = |[x, y]: [f64; 2]| [cx + (x - cx) * cs - (y - cy) * sn, cy + (x - cx) * sn + (y - cy) * cs]; + TransformSession { + session: 1, + layer: 1, + rect: [0.0, 0.0, 200.0, 100.0], + quad: [[0.0, 0.0], [200.0, 0.0], [200.0, 100.0], [0.0, 100.0]].map(turn), + pivot: [cx, cy], + interpolation: "bicubic".into(), + warp: None, + selection: false, + target: None, + path: None, + made: None, + mode: Default::default(), + content_aware: None, + skew_v: 0.0, + layers: Vec::new(), + } +} + +fn angle(s: &TransformSession) -> f64 { + (s.quad[1][1] - s.quad[0][1]).atan2(s.quad[1][0] - s.quad[0][0]).to_degrees() +} + +/// A drag outside the box that turns the pointer about the reference point by `by` degrees. +fn rotate(start_deg: f64, by: f64, shift: bool) -> f64 { + let mut s = session(start_deg); + let at = |deg: f64| { + let (sn, cs) = deg.to_radians().sin_cos(); + [s.pivot[0] + 400.0 * cs, s.pivot[1] + 400.0 * sn] + }; + let (from, to) = (at(10.0), at(10.0 + by)); + let g = Gesture { hit: hit(&s, from, 8.0), start: from, quad0: s.quad, pivot0: s.pivot }; + assert_eq!(g.hit, Hit::Outside); + let mods = if shift { egui::Modifiers::SHIFT } else { egui::Modifiers::NONE }; + apply_drag(&mut s, g, to, mods); + angle(&s) +} + +fn close(a: f64, b: f64) { + assert!((a - b).abs() < 1e-6, "{a} != {b}"); +} + +#[test] +fn shift_snaps_the_change_not_the_absolute_angle() { + // Measured: a box at 25.59°, dragged ~20° with Shift, ends at 40.59° (not 30° or 45°). + close(rotate(25.59, 20.0, true), 40.59); + close(rotate(25.59, -20.0, true), 10.59); + close(rotate(25.59, 7.0, true), 25.59); + close(rotate(25.59, 8.0, true), 40.59); +} + +#[test] +fn from_zero_it_snaps_as_before() { + close(rotate(0.0, 20.0, true), 15.0); + close(rotate(0.0, 23.0, true), 30.0); + close(rotate(0.0, -20.0, true), -15.0); +} + +#[test] +fn without_shift_the_turn_is_free() { + close(rotate(25.59, 20.0, false), 45.59); +}