diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 0f8a04f770..80146394ec 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -145,7 +145,7 @@ fn filter_event_before_tool(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui:: if crate::distort_ui::pointer(app, ev, mods) { return None; } - if crate::perspective_crop_ui::pointer(app, ev) { + if crate::perspective_crop_ui::pointer(app, ev, mods) { return None; } // Window › Modifier Keys: sticky Shift/⌘/⌥ act as held keys. diff --git a/crates/ui-egui/src/tools/perspective_crop_modifiers_tests.rs b/crates/ui-egui/src/tools/perspective_crop_modifiers_tests.rs new file mode 100644 index 0000000000..1a95a885ad --- /dev/null +++ b/crates/ui-egui/src/tools/perspective_crop_modifiers_tests.rs @@ -0,0 +1,153 @@ +//! #11802: ⇧ and ⌥ constrain the Perspective Crop's new drag (a square, from the centre) and its +//! corner drags (one axis, stays a rectangle). Ctrl, and no modifier, keep today's behaviour. + +use egui::Modifiers; + +use super::*; + +const SHIFT: Modifiers = Modifiers::SHIFT; +const ALT: Modifiers = Modifiers::ALT; + +/// An 800 × 500 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": 800, "height": 500})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::PerspectiveCrop; + app +} + +/// A press at `from`, a move and the release at `to`, all with `mods`. +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) { + assert!(pointer(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, mods)); + let mid = [(from[0] + to[0]) / 2.0, (from[1] + to[1]) / 2.0]; + assert!(pointer(app, ToolEvent::Move { x: mid[0], y: mid[1], pressure: 1.0 }, mods)); + assert!(pointer(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, mods)); + assert!(pointer(app, ToolEvent::Up { x: to[0], y: to[1] }, mods)); +} + +fn rect(x0: f64, y0: f64, x1: f64, y1: f64) -> Option<[[f64; 2]; 4]> { + Some([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) +} + +#[test] +fn shift_drag_draws_a_square_of_the_shorter_side() { + let mut app = app(); + drag(&mut app, [100.0, 100.0], [522.0, 320.0], SHIFT); + assert_eq!(app.ui.perspective_crop, rect(100.0, 100.0, 320.0, 320.0), "422 x 220 drag: 220 x 220"); + // Up and to the left: anchored at the press, extending the drag's way. + app.ui.perspective_crop = None; + drag(&mut app, [500.0, 400.0], [450.0, 100.0], SHIFT); + assert_eq!(app.ui.perspective_crop, rect(450.0, 350.0, 500.0, 400.0)); +} + +#[test] +fn alt_drag_draws_from_the_centre() { + let mut app = app(); + drag(&mut app, [400.0, 250.0], [600.0, 350.0], ALT); + assert_eq!(app.ui.perspective_crop, rect(200.0, 150.0, 600.0, 350.0)); +} + +#[test] +fn shift_alt_drag_draws_a_centred_square() { + let mut app = app(); + drag(&mut app, [400.0, 250.0], [600.0, 350.0], SHIFT | ALT); + assert_eq!(app.ui.perspective_crop, rect(300.0, 150.0, 500.0, 350.0), "side twice the shorter drag side"); +} + +#[test] +fn the_live_box_follows_the_modifiers() { + let mut app = app(); + assert!(pointer(&mut app, ToolEvent::Down { x: 400.0, y: 250.0, pressure: 1.0 }, ALT)); + assert!(pointer(&mut app, ToolEvent::Move { x: 600.0, y: 350.0, pressure: 1.0 }, ALT)); + let Some(Drag::New { start, cur, square, centred }) = app.pcrop.drag else { panic!("drawing a box") }; + assert_eq!(new_box(start, cur, square, centred), rect(200.0, 150.0, 600.0, 350.0), "the overlay draws the centred box"); +} + +#[test] +fn shift_corner_drag_keeps_one_axis() { + let mut app = app(); + let q = [[100.0, 100.0], [500.0, 100.0], [500.0, 400.0], [100.0, 400.0]]; + // Mostly horizontal: only x moves. + app.ui.perspective_crop = Some(q); + drag(&mut app, [500.0, 400.0], [600.0, 430.0], SHIFT); + assert_eq!(app.ui.perspective_crop.unwrap()[2], [600.0, 400.0]); + // Mostly vertical: only y moves. + app.ui.perspective_crop = Some(q); + drag(&mut app, [500.0, 400.0], [520.0, 470.0], SHIFT); + assert_eq!(app.ui.perspective_crop.unwrap()[2], [500.0, 470.0]); + assert_eq!(app.ui.perspective_crop.unwrap()[..2], q[..2], "the other corners stay"); +} + +#[test] +fn alt_corner_drag_keeps_a_rectangle() { + let mut app = app(); + app.ui.perspective_crop = Some([[100.0, 100.0], [500.0, 100.0], [500.0, 400.0], [100.0, 400.0]]); + drag(&mut app, [500.0, 400.0], [740.0, 430.0], ALT); + assert_eq!(app.ui.perspective_crop, rect(100.0, 100.0, 740.0, 430.0), "opposite corner fixed, neighbours follow"); + // From the top-left corner too. + drag(&mut app, [100.0, 100.0], [50.0, 160.0], ALT); + assert_eq!(app.ui.perspective_crop, rect(50.0, 160.0, 740.0, 430.0)); +} + +#[test] +fn alt_corner_drag_on_a_skewed_quad_moves_the_corner_alone() { + // Not measured: today's single-corner drag. + let mut app = app(); + let q = [[100.0, 100.0], [500.0, 130.0], [480.0, 400.0], [120.0, 380.0]]; + app.ui.perspective_crop = Some(q); + drag(&mut app, q[2], [700.0, 450.0], ALT); + assert_eq!(app.ui.perspective_crop, Some([q[0], q[1], [700.0, 450.0], q[3]])); +} + +#[test] +fn no_modifier_and_ctrl_keep_todays_behaviour() { + for mods in [Modifiers::NONE, Modifiers::CTRL, Modifiers::COMMAND] { + let mut app = app(); + drag(&mut app, [100.0, 100.0], [522.0, 320.0], mods); + assert_eq!(app.ui.perspective_crop, rect(100.0, 100.0, 522.0, 320.0), "{mods:?}"); + drag(&mut app, [522.0, 320.0], [740.0, 430.0], mods); + assert_eq!(app.ui.perspective_crop, Some([[100.0, 100.0], [522.0, 100.0], [740.0, 430.0], [100.0, 320.0]]), "a single-corner drag"); + drag(&mut app, [300.0, 200.0], [310.0, 220.0], mods); + assert_eq!(app.ui.perspective_crop.unwrap()[0], [110.0, 120.0], "a press inside moves the quad"); + // Four clicks. + let mut app = self::app(); + for p in [[10.0, 10.0], [90.0, 12.0], [88.0, 70.0], [12.0, 72.0]] { + assert!(pointer(&mut app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, mods)); + assert!(pointer(&mut app, ToolEvent::Up { x: p[0], y: p[1] }, mods)); + } + assert_eq!(app.ui.perspective_crop, Some([[10.0, 10.0], [90.0, 12.0], [88.0, 70.0], [12.0, 72.0]])); + } +} + +#[test] +fn zero_size_drags_and_degenerate_quads_never_panic() { + for mods in [SHIFT, ALT, SHIFT | ALT, Modifiers::CTRL] { + let mut app = app(); + // A straight drag: no box. + drag(&mut app, [100.0, 100.0], [300.0, 100.0], mods); + assert_eq!(app.ui.perspective_crop, None, "{mods:?}"); + // All four corners on one point. + app.ui.perspective_crop = Some([[200.0, 200.0]; 4]); + drag(&mut app, [200.0, 200.0], [260.0, 230.0], mods); + // A quad with a line for a side. + app.ui.perspective_crop = Some([[100.0, 100.0], [100.0, 100.0], [400.0, 300.0], [100.0, 300.0]]); + drag(&mut app, [400.0, 300.0], [420.0, 360.0], mods); + assert!(new_box([1.0, 1.0], [1.0, 1.0], true, true).is_none()); + } +} + +#[test] +fn releasing_alt_mid_corner_drag_puts_the_neighbours_back() { + // ⌥ released during the drag: the corner then moves alone, from the quad at the press. + let mut app = app(); + let q = [[100.0, 100.0], [500.0, 100.0], [500.0, 400.0], [100.0, 400.0]]; + app.ui.perspective_crop = Some(q); + assert!(pointer(&mut app, ToolEvent::Down { x: 500.0, y: 400.0, pressure: 1.0 }, ALT)); + assert!(pointer(&mut app, ToolEvent::Move { x: 740.0, y: 430.0, pressure: 1.0 }, ALT)); + assert_eq!(app.ui.perspective_crop, rect(100.0, 100.0, 740.0, 430.0)); + assert!(pointer(&mut app, ToolEvent::Move { x: 700.0, y: 450.0, pressure: 1.0 }, Modifiers::NONE)); + assert_eq!(app.ui.perspective_crop, Some([q[0], q[1], [700.0, 450.0], q[3]]), "the preview during the drag"); + assert!(pointer(&mut app, ToolEvent::Up { x: 700.0, y: 450.0 }, Modifiers::NONE)); + assert_eq!(app.ui.perspective_crop, Some([q[0], q[1], [700.0, 450.0], q[3]])); +} diff --git a/crates/ui-egui/src/tools/perspective_crop_shield_tests.rs b/crates/ui-egui/src/tools/perspective_crop_shield_tests.rs index c203fe5a6c..c210a46095 100644 --- a/crates/ui-egui/src/tools/perspective_crop_shield_tests.rs +++ b/crates/ui-egui/src/tools/perspective_crop_shield_tests.rs @@ -87,7 +87,7 @@ fn the_shield_shows_with_a_quad_follows_a_drag_and_goes_away() { let mut app = app(); assert_eq!(painted(&app), 0, "no quad: no shield"); // While the first box is dragged. - app.pcrop.drag = Some(Drag::New { start: [20.0, 20.0], cur: [180.0, 130.0] }); + app.pcrop.drag = Some(Drag::New { start: [20.0, 20.0], cur: [180.0, 130.0], square: false, centred: false }); assert!(painted(&app) > 0, "the shield follows the box being dragged"); app.pcrop.drag = None; app.ui.perspective_crop = Some([[20.0, 20.0], [180.0, 20.0], [180.0, 130.0], [20.0, 130.0]]); diff --git a/crates/ui-egui/src/tools/perspective_crop_ui.rs b/crates/ui-egui/src/tools/perspective_crop_ui.rs index a64683558e..4ce9f03df8 100644 --- a/crates/ui-egui/src/tools/perspective_crop_ui.rs +++ b/crates/ui-egui/src/tools/perspective_crop_ui.rs @@ -27,10 +27,11 @@ const CLICK_PX: f64 = 3.0; #[derive(Clone, Copy, Debug)] pub enum Drag { - /// Drawing a box from `start`. - New { start: [f64; 2], cur: [f64; 2] }, - /// Moving one corner; `offset` is the press's distance from it. - Corner { index: usize, offset: [f64; 2] }, + /// Drawing a box from `start`; ⇧ makes it a square, ⌥ centres it on `start` (#11802). + New { start: [f64; 2], cur: [f64; 2], square: bool, centred: bool }, + /// Moving one corner; `offset` is the press's distance from it, `press` where the press was + /// and `quad` the quad then (⇧ keeps one axis, ⌥ keeps a rectangle one, #11802). + Corner { index: usize, offset: [f64; 2], press: [f64; 2], quad: [[f64; 2]; 4] }, /// Moving the whole quad. Move { start: [f64; 2], quad: [[f64; 2]; 4] }, } @@ -59,7 +60,7 @@ pub fn pending(app: &PhotocraftApp) -> bool { pub fn dragging(app: &PhotocraftApp) -> bool { app.ui.tool == Tool::PerspectiveCrop && match app.pcrop.drag { - Some(Drag::New { start, cur }) => !is_click(app, start, cur), + Some(Drag::New { start, cur, .. }) => !is_click(app, start, cur), Some(_) => true, None => false, } @@ -105,8 +106,33 @@ fn inside(q: &[[f64; 2]; 4], p: [f64; 2]) -> bool { }) } -/// Pointer events (document px) with the tool. True when consumed. -pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { +/// The box a new drag from `start` to `cur` draws (document px, x0, y0, x1, y1): ⇧ makes it a +/// square of the shorter drag side in the drag's direction, ⌥ centres it on `start` at twice the +/// drag, both together a centred square (#11802). `None` without area. +pub(crate) fn new_box(start: [f64; 2], cur: [f64; 2], square: bool, centred: bool) -> Option<[[f64; 2]; 4]> { + let (mut dx, mut dy) = (cur[0] - start[0], cur[1] - start[1]); + if square { + let side = dx.abs().min(dy.abs()); + (dx, dy) = (side.copysign(dx), side.copysign(dy)); + } + let (a, b) = if centred { ([start[0] - dx, start[1] - dy], [start[0] + dx, start[1] + dy]) } else { (start, [start[0] + dx, start[1] + dy]) }; + let (x0, y0, x1, y1) = (a[0].min(b[0]), a[1].min(b[1]), a[0].max(b[0]), a[1].max(b[1])); + // A straight drag has no area: no box (it could neither be committed nor replaced by a new + // drag). + (x1 > x0 && y1 > y0).then_some([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) +} + +/// Whether every side of `q` is either horizontal or vertical: a rectangle on the image's axes, +/// which ⌥ keeps one while a corner is dragged. +fn axis_rect(q: &[[f64; 2]; 4]) -> bool { + (0..4).all(|i| { + let (a, b) = (q[i], q[(i + 1) % 4]); + (a[0] == b[0]) != (a[1] == b[1]) + }) +} + +/// Pointer events (document px) with the tool and the held modifiers. True when consumed. +pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool { if app.ui.tool != Tool::PerspectiveCrop { app.pcrop.drag = None; return false; @@ -127,18 +153,18 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { .filter(|&i| (q[i][0] - p[0]).hypot(q[i][1] - p[1]) <= tol) .min_by(|&i, &j| (q[i][0] - p[0]).hypot(q[i][1] - p[1]).total_cmp(&(q[j][0] - p[0]).hypot(q[j][1] - p[1]))); match near { - Some(index) => Some(Drag::Corner { index, offset: [p[0] - q[index][0], p[1] - q[index][1]] }), + Some(index) => Some(Drag::Corner { index, offset: [p[0] - q[index][0], p[1] - q[index][1]], press: p, quad: q }), None if inside(&q, p) => Some(Drag::Move { start: p, quad: q }), None => None, } } - None => Some(Drag::New { start: p, cur: p }), + None => Some(Drag::New { start: p, cur: p, square: mods.shift, centred: mods.alt }), }; } - ToolEvent::Move { .. } => drag_to(app, p), + ToolEvent::Move { .. } => drag_to(app, p, mods), ToolEvent::Up { .. } => { - drag_to(app, p); - if let Some(Drag::New { start, cur }) = app.pcrop.drag { + drag_to(app, p, mods); + if let Some(Drag::New { start, cur, square, centred }) = app.pcrop.drag { if is_click(app, start, cur) { app.pcrop.clicks.push(start); if let [a, b, c, d] = app.pcrop.clicks[..] { @@ -147,10 +173,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { } } else { app.pcrop.clicks.clear(); - let (x0, y0, x1, y1) = (start[0].min(cur[0]), start[1].min(cur[1]), start[0].max(cur[0]), start[1].max(cur[1])); - // A straight drag has no area: no box (it could neither be committed nor - // replaced by a new drag). - app.ui.perspective_crop = (x1 > x0 && y1 > y0).then_some([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]); + app.ui.perspective_crop = new_box(start, cur, square, centred); } } app.pcrop.drag = None; @@ -159,12 +182,38 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { true } -fn drag_to(app: &mut PhotocraftApp, p: [f64; 2]) { +fn drag_to(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) { match app.pcrop.drag { - Some(Drag::New { start, .. }) => app.pcrop.drag = Some(Drag::New { start, cur: p }), - Some(Drag::Corner { index, offset }) => { - if let Some(c) = app.ui.perspective_crop.as_mut().and_then(|q| q.get_mut(index)) { - *c = [p[0] - offset[0], p[1] - offset[1]]; + // The modifiers held now shape the box, as the Frame tool's (not measured mid-drag). + Some(Drag::New { start, .. }) => app.pcrop.drag = Some(Drag::New { start, cur: p, square: mods.shift, centred: mods.alt }), + Some(Drag::Corner { index, offset, press, quad }) => { + let Some(from) = quad.get(index).copied() else { return }; + let mut c = [p[0] - offset[0], p[1] - offset[1]]; + // ⇧: along the axis the drag mostly follows since the press; the other stays. + if mods.shift { + if (p[0] - press[0]).abs() >= (p[1] - press[1]).abs() { + c[1] = from[1]; + } else { + c[0] = from[0]; + } + } + let Some(q) = app.ui.perspective_crop.as_mut() else { return }; + // Each move starts from the quad at the press, so ⌥ released mid-drag puts the + // neighbours back. + *q = quad; + // ⌥ on a rectangle: the neighbours follow, one coordinate each, the opposite corner + // stays. On any other quad (not measured) the corner moves alone. + if mods.alt && axis_rect(&quad) { + for j in [(index + 1) % 4, (index + 3) % 4] { + if quad[j][0] == from[0] { + q[j][0] = c[0]; + } else { + q[j][1] = c[1]; + } + } + } + if let Some(corner) = q.get_mut(index) { + *corner = c; } } Some(Drag::Move { start, quad }) => { @@ -286,10 +335,7 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32); let accent = crate::theme::Tokens::get(painter.ctx()).accent; let quad = match app.pcrop.drag { - Some(Drag::New { start, cur }) => { - let (x0, y0, x1, y1) = (start[0].min(cur[0]), start[1].min(cur[1]), start[0].max(cur[0]), start[1].max(cur[1])); - (x1 > x0 && y1 > y0).then_some([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) - } + Some(Drag::New { start, cur, square, centred }) => new_box(start, cur, square, centred), _ => app.ui.perspective_crop, }; if let Some(q) = quad.filter(|q| q.iter().flatten().all(|v| v.is_finite())) { @@ -376,6 +422,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { #[path = "perspective_crop_bar_buttons_tests.rs"] mod perspective_crop_bar_buttons_tests; +#[cfg(test)] +#[path = "perspective_crop_modifiers_tests.rs"] +mod perspective_crop_modifiers_tests; + #[cfg(test)] #[path = "perspective_crop_shield_tests.rs"] mod perspective_crop_shield_tests; @@ -394,7 +444,7 @@ mod tests { s.add_document(doc, None); let mut app = PhotocraftApp::new(s, crate::Services::default()); app.ui.tool = Tool::PerspectiveCrop; - let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e)); + let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e, egui::Modifiers::NONE)); for (x1, y1) in [(10.0, 60.0), (90.0, 10.0)] { ev(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }); ev(&mut app, ToolEvent::Move { x: x1, y: y1, pressure: 1.0 }); @@ -613,7 +663,7 @@ mod tests { s.add_document(doc, None); let mut app = PhotocraftApp::new(s, crate::Services::default()); app.ui.tool = Tool::PerspectiveCrop; - let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e)); + let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e, egui::Modifiers::NONE)); ev(&mut app, ToolEvent::Down { x: 20.0, y: 10.0, pressure: 1.0 }); ev(&mut app, ToolEvent::Move { x: 120.0, y: 90.0, pressure: 1.0 }); ev(&mut app, ToolEvent::Up { x: 120.0, y: 90.0 }); @@ -670,7 +720,7 @@ mod tests { s.set_active(0); let mut app = PhotocraftApp::new(s, crate::Services::default()); app.ui.tool = Tool::PerspectiveCrop; - let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e)); + let ev = |app: &mut PhotocraftApp, e| assert!(pointer(app, e, egui::Modifiers::NONE)); ev(&mut app, ToolEvent::Down { x: 20.0, y: 10.0, pressure: 1.0 }); ev(&mut app, ToolEvent::Move { x: 120.0, y: 90.0, pressure: 1.0 }); ev(&mut app, ToolEvent::Up { x: 120.0, y: 90.0 });