diff --git a/crates/ui-egui/src/canvas/transform_controls_tests.rs b/crates/ui-egui/src/canvas/transform_controls_tests.rs index c977a84d6d..ef5e5668d3 100644 --- a/crates/ui-egui/src/canvas/transform_controls_tests.rs +++ b/crates/ui-egui/src/canvas/transform_controls_tests.rs @@ -323,18 +323,14 @@ fn move_transform_controls_show_resize_and_rotate_cursors_before_first_interacti (r.center_bottom(), egui::CursorIcon::ResizeVertical), (r.right_center(), egui::CursorIcon::ResizeHorizontal), (r.left_center(), egui::CursorIcon::ResizeHorizontal), - // An outward edge band resizes between handles. Beyond it the curved rotate arrow - // replaces the pointer (#4658). - (pos2(r.left() + r.width() * 0.25, r.top() - 11.0), egui::CursorIcon::ResizeVertical), + // Just beyond the outward edge band, the curved rotate arrow replaces the pointer (#4658). (pos2(r.left() + r.width() * 0.25, r.top() - 12.0), egui::CursorIcon::None), ] { assert_eq!(transform_controls_cursor(r, p), Some(expected), "hover at {p:?}"); assert!(transform_controls_hit(r, p), "same region must start a transform"); } assert_eq!(transform_controls_cursor(r, r.center()), Some(egui::CursorIcon::Default), "interior stays Move"); - let far = pos2(r.center().x, r.top() - 30.0); - assert_eq!(transform_controls_cursor(r, far), Some(egui::CursorIcon::None)); - assert!(!transform_controls_hit(r, far), "far hover must not consume a Move press"); + assert_eq!(transform_controls_cursor(r, pos2(r.center().x, r.top() - 30.0)), Some(egui::CursorIcon::None)); // The rotate arrow points into the nearest edge or corner. Handles and the interior have none. let into = |p: Pos2| transform_controls_turn(r, p); assert_eq!(into(pos2(r.left() + r.width() * 0.25, r.top() - 12.0)), Some(vec2(0.0, 16.0)), "top edge"); @@ -353,11 +349,10 @@ fn move_transform_controls_show_resize_and_rotate_cursors_before_first_interacti } #[test] -fn transform_control_hit_resizes_or_rotates_within_the_press_zones() { +fn transform_control_hit_keeps_move_interior_free_and_has_rotation_band() { let r = Rect::from_min_max(pos2(20.0, 30.0), pos2(100.0, 70.0)); assert!(transform_controls_hit(r, r.left_top())); assert!(transform_controls_hit(r, pos2(60.0, 18.0))); assert!(!transform_controls_hit(r, r.center())); - assert!(transform_controls_hit(r, pos2(60.0, 12.0)), "the 18-point rotation band still starts a transform"); - assert!(!transform_controls_hit(r, pos2(60.0, 11.0)), "beyond the band a press stays with Move"); + assert!(!transform_controls_hit(r, pos2(60.0, 4.0))); } diff --git a/crates/ui-egui/src/tools/transform_tool/pointer.rs b/crates/ui-egui/src/tools/transform_tool/pointer.rs index e1457bf208..04ae5ad522 100644 --- a/crates/ui-egui/src/tools/transform_tool/pointer.rs +++ b/crates/ui-egui/src/tools/transform_tool/pointer.rs @@ -19,8 +19,8 @@ pub(crate) enum Hit { /// Screen-sized corner and outward edge bands, shared with Move's unopened controls. impl TransformSession { /// Classify a document point with screen-sized corner and outward edge zones. - /// Corners outrank free edge bands; overlapping painted handles keep nearest-handle priority. - /// Only projection-scale rounding is treated as an edge on the interior side. + /// On and outward from an edge, the nearest corner or midpoint wins, with the corner + /// reach capped along the edge. Inside and beyond the edge ends use the corner radius. pub(crate) fn box_hit(quad: &[[f64; 2]; 4], p: [f64; 2], tol: f64) -> Hit { // `tol` is the existing 12 pt handle tolerance, divided by the view zoom. let corner_radius = tol * (8.0 / HANDLE_PX); @@ -30,32 +30,40 @@ impl TransformSession { let line_epsilon = tol.abs() * (1e-3 / HANDLE_PX); let distance = |q: [f64; 2]| (p[0] - q[0]).hypot(p[1] - q[1]); let interior = inside(quad, p); - let edge_distance = |i| { - let (_, q) = edge_projection(quad, i, p)?; - let d = distance(q); - (d <= edge_width && (!interior || d <= line_epsilon)).then_some(d) - }; - if let Some((i, corner_distance)) = + let edge = (0..4) + .filter_map(|i| { + let (u, q) = edge_projection(quad, i, p)?; + let d = distance(q); + (d <= edge_width && (!interior || d <= line_epsilon)).then_some((i, u, d)) + }) + .min_by(|a, b| a.2.total_cmp(&b.2)); + if let Some((i, u, _)) = edge + && (0.0..=1.0).contains(&u) + { + let next = (i + 1) % 4; + let (a, b) = (quad[i], quad[next]); + let length = (b[0] - a[0]).hypot(b[1] - a[1]); + let quarter = length * 0.25; + let tie_epsilon = line_epsilon.min(quarter * 0.5); + // Projected distances rank the handles along this edge. At the measured + // quarter-point ties, prefer the earlier handle in edge order. + let from_start = u * length; + let from_end = (1.0 - u) * length; + if from_start <= quarter + tie_epsilon && from_start <= edge_width + line_epsilon { + return Hit::Corner(i); + } + if from_end < quarter - tie_epsilon && from_end <= edge_width + line_epsilon { + return Hit::Corner(next); + } + return Hit::Edge(i); + } + if let Some((i, _)) = quad.iter().enumerate().map(|(i, &q)| (i, distance(q))).filter(|(_, d)| *d <= corner_radius).min_by(|a, b| a.1.total_cmp(&b.1)) { - // Preserve nearest-handle priority only when the midpoint itself falls in the - // corner zone. A separate midpoint must not take over a nearby corner press. - let painted_edge = (0..4) - .filter_map(|k| { - edge_distance(k)?; - let (a, b) = (quad[k], quad[(k + 1) % 4]); - let midpoint = [(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0]; - let corner = quad[i]; - let d = distance(midpoint); - let overlaps_corner = (midpoint[0] - corner[0]).hypot(midpoint[1] - corner[1]) <= corner_radius; - (overlaps_corner && d < corner_distance).then_some((k, d)) - }) - .min_by(|a, b| a.1.total_cmp(&b.1)); - return painted_edge.map_or(Hit::Corner(i), |(k, _)| Hit::Edge(k)); + return Hit::Corner(i); } - let edge = (0..4).filter_map(|i| edge_distance(i).map(|d| (i, d))).min_by(|a, b| a.1.total_cmp(&b.1)); match edge { - Some((i, _)) => Hit::Edge(i), + Some((i, _, _)) => Hit::Edge(i), None if interior => Hit::Inside, None => Hit::Outside, } @@ -95,8 +103,8 @@ impl TransformSession { } } -/// The nearest point on an edge segment and its clamped parameter. Degenerate edges have no -/// band or normal; the other edges and corner handles remain usable. +/// The unclamped projection parameter and the nearest point on the segment. Degenerate +/// edges have no band or normal; the other edges and corner handles remain usable. fn edge_projection(quad: &[[f64; 2]; 4], i: usize, p: [f64; 2]) -> Option<(f64, [f64; 2])> { let (a, b) = (quad[i], quad[(i + 1) % 4]); let d = [b[0] - a[0], b[1] - a[1]]; @@ -104,8 +112,9 @@ fn edge_projection(quad: &[[f64; 2]; 4], i: usize, p: [f64; 2]) -> Option<(f64, if !len2.is_finite() || len2 <= 1e-18 { return None; } - let u = (((p[0] - a[0]) * d[0] + (p[1] - a[1]) * d[1]) / len2).clamp(0.0, 1.0); - Some((u, [a[0] + u * d[0], a[1] + u * d[1]])) + let u = ((p[0] - a[0]) * d[0] + (p[1] - a[1]) * d[1]) / len2; + let on_segment = u.clamp(0.0, 1.0); + Some((u, [a[0] + on_segment * d[0], a[1] + on_segment * d[1]])) } /// The shared box zones, retaining the nearest pivot's existing grab priority on small boxes. diff --git a/crates/ui-egui/src/transform_hit_zone_tests.rs b/crates/ui-egui/src/transform_hit_zone_tests.rs index 394e32a278..9cd109b9e5 100644 --- a/crates/ui-egui/src/transform_hit_zone_tests.rs +++ b/crates/ui-egui/src/transform_hit_zone_tests.rs @@ -94,13 +94,39 @@ fn small_boxes_keep_their_painted_mid_edge_handles() { } } +/// Match the measured #11097 boundaries and ties on the top edge and one point outside it. #[test] -fn a_midpoint_outside_the_corner_zone_cannot_steal_its_press() { - for zoom in [1.0, 2.0] { - let quad = [[0.0, 0.0], [20.0, 0.0], [20.0, 20.0], [0.0, 20.0]].map(|p| p.map(|v| v / zoom)); - let tol = 12.0 / zoom; - assert_eq!(TransformSession::box_hit(&quad, [7.0 / zoom, -1.0 / zoom], tol), Hit::Corner(0)); - assert_eq!(TransformSession::box_hit(&quad, [9.0 / zoom, 0.0], tol), Hit::Edge(0)); +fn measured_edge_corner_and_midpoint_zones_at_100_and_200_percent() { + use Hit::{Corner, Edge}; + let matrix: &[(f64, &[(f64, Hit)])] = &[ + (12.0, &[(3.0, Corner(0)), (4.0, Edge(0)), (5.0, Edge(0)), (6.0, Edge(0)), (9.0, Edge(0)), (10.0, Corner(1))]), + (24.0, &[(6.0, Corner(0)), (7.0, Edge(0)), (18.0, Edge(0)), (19.0, Corner(1))]), + (40.0, &[(10.0, Corner(0)), (11.0, Edge(0)), (30.0, Edge(0)), (31.0, Corner(1))]), + (100.0, &[(11.0, Corner(0)), (12.0, Edge(0))]), + ]; + for zoom in [1.0_f32, 2.0] { + let scale = |p: [f64; 2]| p.map(|v| v / f64::from(zoom)); + let xf = view(zoom); + for &(side, samples) in matrix { + let quad = [[0.0, 0.0], [side, 0.0], [side, side], [0.0, side]].map(scale); + let mut app = app(quad, zoom); + app.ui.tool_options.transform_reference_point = false; + let t = app.ui.transform.as_ref().unwrap(); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(side as f32, side as f32)); + for y in [0.0, -1.0] { + for &(x, expected) in samples { + let p = scale([x, y]); + let icon = match expected { + Corner(0) => CursorIcon::ResizeNwSe, + Corner(1) => CursorIcon::ResizeNeSw, + _ => CursorIcon::ResizeVertical, + }; + assert_eq!(crate::transform_tool::box_hit(&app, t, p, 12.0 / f64::from(zoom)), expected, "{side}, {zoom}, {x}, {y}"); + assert_eq!(cursor(&app, &xf, p, Modifiers::NONE), Some(icon)); + assert_eq!(transform_controls_cursor(rect, pos2(x as f32, y as f32)), Some(icon)); + } + } + } } } @@ -169,6 +195,8 @@ fn turned_box_zones_and_resize_cursors_follow_the_quad() { let t = app.ui.transform.as_ref().unwrap(); for (p, expected, icon) in [ ([500.0, 100.0], Hit::Corner(1), CursorIcon::ResizeHorizontal), + ([111.0, 99.0], Hit::Corner(0), CursorIcon::ResizeVertical), + ([112.0, 99.0], Hit::Edge(0), CursorIcon::ResizeNeSw), ([180.0, 95.0], Hit::Edge(0), CursorIcon::ResizeNeSw), ([505.0, 150.0], Hit::Edge(1), CursorIcon::ResizeNwSe), ([180.0, 101.0], Hit::Inside, CursorIcon::Default), @@ -268,8 +296,11 @@ fn unopened_move_controls_share_hover_but_keep_the_move_press_band() { for (p, starts_transform) in [ (pos2(100.0, 100.0), true), (pos2(180.0, 95.0), true), + (pos2(180.0, 89.0), true), (pos2(505.0, 150.0), true), (pos2(518.0, 150.0), true), + (pos2(519.0, 150.0), false), + (pos2(300.0, 70.0), false), (pos2(180.0, 101.0), false), (pos2(620.0, 200.0), false), (pos2(-20.0, -20.0), false),