From fd0d543d0da1612b0d88be71792cd627a9c9c39d Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 16:59:14 -0600 Subject: [PATCH 1/2] Perspective Crop: the quad follows the pointer while corners are clicked (#11804) After two clicks the preview is the two points with the pointer as the third and fourth corner, after three the pointer is the fourth; it is drawn with its grid in the finished quad's style, and the clicked corners are square handles. The canvas sends button-up moves to the tool while corners are pending, as it does for the Magnetic Lasso. --- crates/ui-egui/src/canvas.rs | 10 + .../perspective_crop_click_preview_tests.rs | 190 ++++++++++++++++++ .../ui-egui/src/tools/perspective_crop_ui.rs | 70 ++++++- 3 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 5cc293021d..f21205ee58 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1287,6 +1287,16 @@ fn handle_canvas_tool_clicks( app.defer_live_stroke = false; crate::magnetic_lasso_ui::flush(app); } + // Perspective Crop: between corner clicks the preview quad follows the pointer (#11804). + if tool == Tool::PerspectiveCrop + && !app.pcrop.clicks.is_empty() + && !(buttons.started || buttons.dragged || buttons.stopped) + && response.hovered() + && let Some(p) = ui.input(|i| i.pointer.latest_pos()).filter(|p| rect.contains(*p)) + { + let d = xf.to_doc(p); + tool_event(app, ToolEvent::Move { x: d[0], y: d[1], pressure: 1.0 }, mods); + } // Move tool: a double-click on type edits it, as in Photoshop (#1132). Only the selected // Move tool, not ⌘ held over another tool; an open transform box keeps its double-click // (commit, below). diff --git a/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs new file mode 100644 index 0000000000..a55e2fc4d9 --- /dev/null +++ b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs @@ -0,0 +1,190 @@ +//! #11804: while corners are clicked, the quad follows the pointer with its grid. After two clicks +//! the third and fourth corner are the pointer (a triangle), after three the fourth is. + +use super::*; + +/// A 200 × 150 document with the Perspective Crop tool. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 150})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::PerspectiveCrop; + app +} + +fn click(app: &mut PhotocraftApp, p: [f64; 2]) { + assert!(pointer(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 })); + assert!(pointer(app, ToolEvent::Up { x: p[0], y: p[1] })); +} + +/// The pointer moved with the button up. +fn hover(app: &mut PhotocraftApp, p: [f64; 2]) { + assert!(pointer(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 })); +} + +const A: [f64; 2] = [30.0, 20.0]; +const B: [f64; 2] = [170.0, 25.0]; +const C: [f64; 2] = [160.0, 130.0]; +const D: [f64; 2] = [40.0, 120.0]; + +#[test] +fn after_two_clicks_the_pointer_is_the_third_and_fourth_corner() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [120.0, 100.0]); + assert_eq!(click_preview(&app), Some([A, B, [120.0, 100.0], [120.0, 100.0]])); + // Every frame's move updates it. + hover(&mut app, [90.0, 140.0]); + assert_eq!(click_preview(&app), Some([A, B, [90.0, 140.0], [90.0, 140.0]])); +} + +#[test] +fn after_three_clicks_the_pointer_is_the_fourth_corner() { + let mut app = app(); + for p in [A, B, C] { + click(&mut app, p); + } + hover(&mut app, [45.0, 110.0]); + assert_eq!(click_preview(&app), Some([A, B, C, [45.0, 110.0]])); + hover(&mut app, D); + assert_eq!(click_preview(&app), Some([A, B, C, D])); +} + +#[test] +fn with_one_click_or_none_nothing_follows_the_pointer() { + let mut app = app(); + hover(&mut app, [100.0, 70.0]); + assert_eq!(click_preview(&app), None); + assert_eq!(app.pcrop.hover, None, "no clicks: the pointer isn't kept"); + click(&mut app, A); + hover(&mut app, [100.0, 70.0]); + assert_eq!(click_preview(&app), None); +} + +#[test] +fn esc_and_the_fourth_click_work_as_before() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [100.0, 70.0]); + cancel(&mut app); + assert!(app.pcrop.clicks.is_empty()); + assert_eq!(app.pcrop.hover, None); + assert_eq!(click_preview(&app), None); + // The fourth click makes the same ordered quad as without the preview, clicked in any order. + for p in [C, A, D] { + click(&mut app, p); + hover(&mut app, [100.0, 70.0]); + } + click(&mut app, B); + assert_eq!(app.ui.perspective_crop, Some(ordered([C, A, D, B]))); + assert_eq!(app.ui.perspective_crop, Some([A, B, C, D])); + assert_eq!(click_preview(&app), None); +} + +#[test] +fn a_non_finite_pointer_or_a_document_switch_leaves_no_preview() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + hover(&mut app, [100.0, 70.0]); + hover(&mut app, [f64::NAN, 70.0]); + assert_eq!(click_preview(&app), None, "a non-finite pointer"); + hover(&mut app, [100.0, f64::INFINITY]); + assert_eq!(click_preview(&app), None); + hover(&mut app, [100.0, 70.0]); + assert!(click_preview(&app).is_some()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.sync_views(); + assert_eq!(click_preview(&app), None, "another document: the clicks and preview are dropped"); + assert!(app.pcrop.clicks.is_empty()); +} + +#[test] +fn the_two_click_grid_meets_at_the_pointer() { + let h = [120.0, 100.0]; + let lines = grid_lines([A, B, h, h], 4); + assert_eq!(lines.len(), 6); + let meets = lines.iter().filter(|[_, b]| (b[0] - h[0]).hypot(b[1] - h[1]) < 1e-9).count(); + assert_eq!(meets, 3, "one family of lines runs from side A–B to the pointer"); + // With three clicks and the pointer making a convex quad, the finished quad's perspective grid. + let q = [A, B, C, D]; + let hom = Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], q).unwrap(); + let (x, y) = hom.apply(0.5, 0.0); + assert!(grid_lines(q, 2).iter().any(|[a, _]| (a[0] - x).abs() < 1e-9 && (a[1] - y).abs() < 1e-9)); +} + +/// Shapes `draw_overlay` paints for the app as it is. +fn painted(app: &PhotocraftApp) -> usize { + let ctx = egui::Context::default(); + let rect = egui::Rect::from_min_size(Pos2::ZERO, egui::vec2(200.0, 150.0)); + let xf = ViewXform { rect, zoom: 1.0, center: [100.0, 75.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let mut o = ctx.run_ui(Default::default(), |ui| draw_overlay(app, ui.painter(), &xf)); + o.textures_delta.clear(); + o.shapes.len() +} + +#[test] +fn the_preview_is_drawn_with_its_grid() { + let mut app = app(); + click(&mut app, A); + click(&mut app, B); + let without = painted(&app); + hover(&mut app, [120.0, 100.0]); + app.ui.tool_options.perspective_crop_grid = true; + let with_grid = painted(&app); + app.ui.tool_options.perspective_crop_grid = false; + let without_grid = painted(&app); + assert_eq!(without_grid, without, "with Show Grid off the outline replaces the line between the clicks"); + assert!(with_grid > without_grid, "Show Grid adds the grid: {with_grid} vs {without_grid}"); +} + +/// Real egui input: the pointer moving over the canvas with the button up reaches the tool, so +/// the preview follows it. +#[test] +fn hover_over_the_canvas_moves_the_preview() { + use egui::{Event, Modifiers, PointerButton}; + let mut a = app(); + a.ui.extras.snap = false; + a.ui.extras.rulers = false; + a.ui.views = vec![crate::state::View { + zoom: 2.0, + center: [100.0, 75.0], + fit_pending: false, + fill_pending: false, + doc_size: [200, 150], + rotation: 0.0, + zoom_animating: false, + zoom_anchor: None, + display_camera: None, + zoom_transition: None, + }]; + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(600.0, 400.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let v = app.ui.views[0].clone(); + crate::canvas::canvas_view(app, ui, 0, ui.max_rect(), v, true); + }, + a, + ); + h.run_steps(2); + let rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 400.0)); + let xf = ViewXform { rect, zoom: 2.0, center: [100.0, 75.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + for p in [[60.0, 40.0], [140.0, 40.0]] { + let at = xf.to_screen(p[0], p[1]); + h.event(Event::PointerMoved(at)); + h.step(); + h.event(button(at, true)); + h.step(); + h.event(button(at, false)); + h.step(); + } + assert_eq!(h.state().pcrop.clicks.len(), 2, "{:?}", h.state().pcrop.clicks); + let to = xf.to_screen(110.0, 100.0); + h.event(Event::PointerMoved(to)); + h.step(); + let q = click_preview(h.state()).expect("a preview after two clicks and a move"); + assert!((q[2][0] - 110.0).abs() < 0.5 && (q[2][1] - 100.0).abs() < 0.5, "{q:?}"); + assert_eq!(q[2], q[3]); +} diff --git a/crates/ui-egui/src/tools/perspective_crop_ui.rs b/crates/ui-egui/src/tools/perspective_crop_ui.rs index ed60c90d41..6e3085cec7 100644 --- a/crates/ui-egui/src/tools/perspective_crop_ui.rs +++ b/crates/ui-egui/src/tools/perspective_crop_ui.rs @@ -40,6 +40,8 @@ pub struct PerspectiveCrop { pub drag: Option, /// Corners clicked so far, before the fourth makes the quad. pub clicks: Vec<[f64; 2]>, + /// The pointer between clicks (button up), the preview quad's free corner (#11804). + pub hover: Option<[f64; 2]>, /// The document the pending quad belongs to. doc: Option, } @@ -75,6 +77,7 @@ pub fn cancel(app: &mut PhotocraftApp) { app.ui.perspective_crop = None; app.pcrop.drag = None; app.pcrop.clicks.clear(); + app.pcrop.hover = None; } /// Another document became active: its pending quad is dropped (also from `sync_views`, so it is @@ -115,6 +118,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } | ToolEvent::Up { x, y } => [x, y], }; if app.session.active().is_none() || !p[0].is_finite() || !p[1].is_finite() { + app.pcrop.hover = None; return true; } cancel_stale(app); @@ -135,7 +139,11 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { None => Some(Drag::New { start: p, cur: p }), }; } - ToolEvent::Move { .. } => drag_to(app, p), + ToolEvent::Move { .. } => { + // Between corner clicks the preview's free corner follows the pointer (#11804). + app.pcrop.hover = (app.pcrop.drag.is_none() && !app.pcrop.clicks.is_empty()).then_some(p); + drag_to(app, p); + } ToolEvent::Up { .. } => { drag_to(app, p); if let Some(Drag::New { start, cur }) = app.pcrop.drag { @@ -233,6 +241,39 @@ fn grid_steps(side: f32) -> usize { ((side / 48.0).round() as usize).clamp(2, 12) } +/// The quad that follows the pointer while corners are clicked (#11804): after two clicks the two +/// points and the pointer as the third and fourth corner (a triangle), after three the three points +/// and the pointer as the fourth. `None` with fewer clicks or no pointer position. +pub(crate) fn click_preview(app: &PhotocraftApp) -> Option<[[f64; 2]; 4]> { + let h = app.pcrop.hover.filter(|h| h.iter().all(|v| v.is_finite()))?; + match app.pcrop.clicks[..] { + [a, b] => Some([a, b, h, h]), + [a, b, c] => Some([a, b, c, h]), + _ => None, + } +} + +/// The grid lines of the quad `q` (document px, `n` steps): its perspective grid, or for a quad +/// with two corners on one point (the two-click preview) the lines between matching points of +/// opposite sides, which meet at that point. +fn grid_lines(q: [[f64; 2]; 4], n: usize) -> Vec<[[f64; 2]; 2]> { + let lerp = |a: [f64; 2], b: [f64; 2], t: f64| [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; + let homography = if convex_quad(&q) { Homography::rect_to_quad([0.0, 0.0, 1.0, 1.0], q) } else { None }; + let at = |u: f64, v: f64| match &homography { + Some(h) => { + let (x, y) = h.apply(u, v); + [x, y] + } + None => lerp(lerp(q[0], q[1], u), lerp(q[3], q[2], u), v), + }; + (1..n) + .flat_map(|k| { + let t = k as f64 / n as f64; + [[at(t, 0.0), at(t, 1.0)], [at(0.0, t), at(1.0, t)]] + }) + .collect() +} + /// The quad, its perspective grid and corner handles; corners clicked so far; the box being drawn. pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) { if app.ui.tool != Tool::PerspectiveCrop { @@ -278,12 +319,29 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform } } let clicks: Vec = app.pcrop.clicks.iter().map(|c| scr(*c)).collect(); - if clicks.len() > 1 { + if let Some(q) = click_preview(app) { + // The quad to the pointer with its grid, in the finished quad's style (#11804). A third + // click that makes it cross turns it red as the finished quad (not measured); the + // two-click triangle is drawn as a quad. + let ok = clicks.len() == 2 || convex_quad(&q); + let color = if ok { accent } else { Color32::from_rgb(0xe5, 0x3e, 0x3e) }; + let pts: Vec = q.iter().map(|c| scr(*c)).collect(); + if ok && app.ui.tool_options.perspective_crop_grid { + let side = pts.iter().zip(pts.iter().skip(1)).map(|(a, b)| a.distance(*b)).fold(0.0, f32::max); + let thin = Stroke::new(0.75, Color32::from_rgba_unmultiplied(255, 255, 255, 150)); + for [a, b] in grid_lines(q, grid_steps(side)) { + painter.line_segment([scr(a), scr(b)], thin); + } + } + painter.add(egui::Shape::closed_line(pts, Stroke::new(1.5, color))); + } else if clicks.len() > 1 { painter.add(egui::Shape::line(clicks.clone(), Stroke::new(1.5, accent))); } + // The clicked corners: small square handles, as the reference application marks them. for c in clicks { - painter.circle_filled(c, 4.0, Color32::WHITE); - painter.circle_stroke(c, 4.0, Stroke::new(1.25, accent)); + let r = egui::Rect::from_center_size(c, egui::vec2(8.0, 8.0)); + painter.rect_filled(r, 0.0, Color32::WHITE); + painter.rect_stroke(r, 0.0, Stroke::new(1.25, accent), egui::StrokeKind::Inside); } } @@ -316,6 +374,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { }); } +#[cfg(test)] +#[path = "perspective_crop_click_preview_tests.rs"] +mod perspective_crop_click_preview_tests; + #[cfg(test)] mod tests { use super::*; From 4cb7519df6d2695b69c311491455d1ec8eb61045 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Tue, 10 Nov 2026 00:12:40 -0600 Subject: [PATCH 2/2] Perspective Crop click-preview tests: pass the modifiers main's pointer() now takes --- .../src/tools/perspective_crop_click_preview_tests.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs index a55e2fc4d9..aef086e406 100644 --- a/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs +++ b/crates/ui-egui/src/tools/perspective_crop_click_preview_tests.rs @@ -13,13 +13,13 @@ fn app() -> PhotocraftApp { } fn click(app: &mut PhotocraftApp, p: [f64; 2]) { - assert!(pointer(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 })); - assert!(pointer(app, ToolEvent::Up { x: p[0], y: p[1] })); + assert!(pointer(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, egui::Modifiers::NONE)); + assert!(pointer(app, ToolEvent::Up { x: p[0], y: p[1] }, egui::Modifiers::NONE)); } /// The pointer moved with the button up. fn hover(app: &mut PhotocraftApp, p: [f64; 2]) { - assert!(pointer(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 })); + assert!(pointer(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 }, egui::Modifiers::NONE)); } const A: [f64; 2] = [30.0, 20.0];