From 7f4b643d9ee8bb7c575ac2db478f6d154096ddf2 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 4 Nov 2026 19:35:08 +0100 Subject: [PATCH] Fix-up #11165, #11247: the press decides a pasteboard click; a small box's centre moves it #11165: a click pressed on the image that jitters past the canvas edge before the release committed Free Transform; the press point now decides, so it keeps the box open, and a click pressed on the pasteboard still commits. #11247: with Toggle Reference Point off, a press at the centre of a box smaller than two grab areas picked the nearest edge and scaled. The box centre now stands in for the hidden reference point as a move target. Both from CodeRabbit, each with a test that fails without the fix. Co-Authored-By: Claude Opus 5.5 --- .../src/tools/transform_tool/pointer.rs | 17 +++++++++---- .../src/transform_commit_click_tests.rs | 18 ++++++++++++++ .../src/transform_reference_point_tests.rs | 24 ++++++++++++++++++- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/tools/transform_tool/pointer.rs b/crates/ui-egui/src/tools/transform_tool/pointer.rs index 473599fd7b..4f63c9cc56 100644 --- a/crates/ui-egui/src/tools/transform_tool/pointer.rs +++ b/crates/ui-egui/src/tools/transform_tool/pointer.rs @@ -35,7 +35,11 @@ fn hit_with(t: &TransformSession, p: [f64; 2], tol: f64, pivot: bool) -> Hit { let (a, b) = (t.quad[i], t.quad[(i + 1) % 4]); [(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0] }; - let handles = (0..4).map(|i| (t.quad[i], Hit::Corner(i))).chain((0..4).map(|i| (mid(i), Hit::Edge(i)))).chain(pivot.then_some((t.pivot, Hit::Pivot))); + // With the reference point hidden, the box centre is a move target in its place, so a press + // at the centre of a box smaller than two grab areas moves it instead of grabbing an edge. + let centre = [t.quad.iter().map(|q| q[0]).sum::() / 4.0, t.quad.iter().map(|q| q[1]).sum::() / 4.0]; + let middle = if pivot { (t.pivot, Hit::Pivot) } else { (centre, Hit::Inside) }; + let handles = (0..4).map(|i| (t.quad[i], Hit::Corner(i))).chain((0..4).map(|i| (mid(i), Hit::Edge(i)))).chain(std::iter::once(middle)); let nearest = handles.map(|(q, h)| (d(q), h)).filter(|(dist, _)| *dist < tol).min_by(|a, b| a.0.total_cmp(&b.0)); match nearest { Some((_, h)) => h, @@ -114,10 +118,15 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> // Clicking the pasteboard (outside the document) commits; a click outside the box on the // image does nothing and the box stays open (#11100). Dragging the rotation zone and // Option-clicking to move the pivot keep their existing behavior. - if matches!(ev, ToolEvent::Up { .. }) && g.is_some_and(|g| accepts_outside_click(g, [x, y], tol)) { - if on_pasteboard(app, [x, y]) { + // The press decides: a click that starts on the image and jitters past the edge + // before the release is still a click on the image. + if matches!(ev, ToolEvent::Up { .. }) + && let Some(g) = g + && accepts_outside_click(g, [x, y], tol) + { + if on_pasteboard(app, g.start) { commit(app); - } else if let (Some(g), Some(s)) = (g, app.ui.transform.as_mut()) { + } else if let Some(s) = app.ui.transform.as_mut() { // Jitter before the release may have turned the box a little: put it back. s.quad = g.quad0; s.pivot = g.pivot0; diff --git a/crates/ui-egui/src/transform_commit_click_tests.rs b/crates/ui-egui/src/transform_commit_click_tests.rs index 1d43c501f7..3f52b6d21e 100644 --- a/crates/ui-egui/src/transform_commit_click_tests.rs +++ b/crates/ui-egui/src/transform_commit_click_tests.rs @@ -72,6 +72,24 @@ fn a_click_on_the_pasteboard_commits_one_free_transform_step() { } } +/// The press decides where a click lands: one on the image that jitters past the canvas edge +/// before the release keeps the box open, one on the pasteboard that jitters onto the image commits. +#[test] +fn a_click_counts_where_it_was_pressed() { + let mut app = app(); + let (before, quad) = (steps(&app), app.ui.transform.as_ref().unwrap().quad); + tool_event(&mut app, ToolEvent::Down { x: 199.5, y: 150.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 200.5, y: 150.0 }, Modifiers::NONE); + assert!(app.ui.transform.is_some(), "pressed on the image: the box stays open"); + assert_eq!(app.ui.transform.as_ref().unwrap().quad, quad); + assert_eq!(steps(&app), before); + + tool_event(&mut app, ToolEvent::Down { x: 200.5, y: 150.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 199.5, y: 150.0 }, Modifiers::NONE); + assert!(app.ui.transform.is_none(), "pressed on the pasteboard: committed"); + assert_eq!(steps(&app), before + 1); +} + #[test] fn a_drag_outside_the_box_still_rotates() { let mut app = app(); diff --git a/crates/ui-egui/src/transform_reference_point_tests.rs b/crates/ui-egui/src/transform_reference_point_tests.rs index 43cc61fef7..2dc6648a8d 100644 --- a/crates/ui-egui/src/transform_reference_point_tests.rs +++ b/crates/ui-egui/src/transform_reference_point_tests.rs @@ -9,11 +9,16 @@ use crate::canvas::ToolEvent; /// A 64 x 64 document with a layer filled at 4..60 and Free Transform open on it. fn transforming(reference_point: bool) -> PhotocraftApp { + transforming_box(reference_point, 56) +} + +/// [`transforming`] with a filled square of `side` px at 4, 4. +fn transforming_box(reference_point: bool, side: u32) -> PhotocraftApp { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); app.sync_views(); app.run("layer.new.layer", json!({})).unwrap(); - app.run("select.rect", json!({"x": 4, "y": 4, "width": 56, "height": 56})).unwrap(); + app.run("select.rect", json!({"x": 4, "y": 4, "width": side, "height": side})).unwrap(); app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); app.run("select.deselect", json!({})).unwrap(); app.ui.tool_options.transform_reference_point = reference_point; @@ -56,3 +61,20 @@ fn with_the_reference_point_on_the_centre_drags_it() { assert_eq!(t.quad, quad, "the box stays"); assert!((t.pivot[0] - pivot[0] - 5.0).abs() < 1e-6, "the reference point moved: {:?}", t.pivot); } + +/// A box smaller than two grab areas (12 screen px each side of a handle): a press on its centre +/// still moves it with the reference point hidden, rather than grabbing the nearest edge. +#[test] +fn with_the_reference_point_off_the_centre_of_a_small_box_moves_it() { + let mut app = transforming_box(false, 10); + let tol = crate::transform_tool::handle_tolerance(&app); + let side = { + let q = app.ui.transform.as_ref().unwrap().quad; + q[1][0] - q[0][0] + }; + assert!(side < 2.0 * tol, "the box ({side} px) is under two grab areas ({tol} px each)"); + let (quad, _) = drag_from_pivot(&mut app); + let t = app.ui.transform.as_ref().unwrap(); + let moved = quad.map(|[x, y]| [x + 5.0, y]); + assert!(t.quad.iter().flatten().zip(moved.iter().flatten()).all(|(a, b)| (a - b).abs() < 1e-6), "the box moved whole: {:?}", t.quad); +}