From 8f7d916ba5ce6d94f2334d47bb23b84ff6cbaff2 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:01:37 +0100 Subject: [PATCH] Single Row / Column Marquee: a drag from inside the selection moves it (#11392) (#11534) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Single Row / Column Marquee: a drag from inside the selection moves it (#11392) `selection_drag_kind` lists the Single Row and Single Column Marquee with the other marquees, so a plain drag that starts inside the selection moves the outline (one step, as the Rectangular Marquee's move) instead of replacing it with one new line. A click inside without moving still selects a line (`finish_gesture`), never deselects, and snaps it as a click outside the selection does (`snap_ui::snap_click`: guides, grid, layers and document bounds, not the selection's own edges, which would pick the line beside a 1 px one). A click on a floating piece drops it where it is first, then selects the line. ⇧ / ⌥ and drags from outside are unchanged. A selection drag is a click only when the pointer, before snapping, never left the press by half a pixel or more (`Drag::left_press`, `selection_release`); a click ends where it was pressed, unsnapped, as with snapping off. With View › Snap on, a click inside a selection whose edge sat within reach of a guide or the canvas edge moved it onto that edge instead of selecting the line (or, with the Rectangular and Elliptical Marquee, deselecting). `tool_input` passes the classification on (`finish_release`), so a drag that snapping pulls back to where it started, or that comes back there, leaves the selection as it was instead of taking the click's path. Fixes #11392 Co-Authored-By: Claude Opus 5.5 * Selection drags: judge a press inside the selection before snapping (#11392) The press handler asked `selection_drag_kind` about the snapped press point. With View › Snap on, a press just outside a selected row snapped onto the row's edge and counted as inside, so a Single Row / Column Marquee drag from there moved the selection instead of selecting a new line. It now asks about the press as it was, as `snap_ui::begin` does when it picks the drag's snapping. The Move tool's click shortcut also needs the pointer never to have left the press (`Drag::left_press`): a drag that snapping held at the press point and that came back there dropped a floating piece. A drag that ends unmoved without being a click now clears the floating piece's move preview, which nothing else releases on that path. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/ui-egui/src/canvas/drag.rs | 4 + crates/ui-egui/src/canvas/gesture.rs | 54 ++- crates/ui-egui/src/canvas/snap_ui.rs | 13 + crates/ui-egui/src/canvas/tool_input.rs | 60 ++- crates/ui-egui/src/lib.rs | 3 + .../src/single_line_marquee_move_tests.rs | 367 ++++++++++++++++++ 6 files changed, 490 insertions(+), 11 deletions(-) create mode 100644 crates/ui-egui/src/single_line_marquee_move_tests.rs diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index ac3da2eacb..8325b5add7 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -27,6 +27,9 @@ pub struct Drag { /// `Some(false)` moves the outline, `Some(true)` moves the floating piece (`select.float`), as /// every Move-tool drag with a selection does (`move_ui::moves_selected_pixels_with`). pub sel_move: Option, + /// The pointer, before snapping, has been half a pixel or more from the press: the gesture is + /// not a click even if it comes back there (`tool_input::selection_release`, #11392). + pub left_press: bool, /// The last ⇧-locked offset of a selection-tool drag of the selection (`move_mods`, #6231); /// keeps the locked direction stable between events. The Move tool's own drags use `move_mods`. pub sel_axis: Option<[f64; 2]>, @@ -50,6 +53,7 @@ impl Drag { released: egui::Modifiers::NONE, reposition: false, sel_move: None, + left_press: false, sel_axis: None, lasso: None, vector_owner: None, diff --git a/crates/ui-egui/src/canvas/gesture.rs b/crates/ui-egui/src/canvas/gesture.rs index ae89b9063f..2a2889848e 100644 --- a/crates/ui-egui/src/canvas/gesture.rs +++ b/crates/ui-egui/src/canvas/gesture.rs @@ -7,6 +7,19 @@ use super::textures::shown_as_document; use super::{PhotocraftApp, Tool, json}; pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) { + finish(app, d, None); +} + +/// [`finish_gesture`] for a pointer release. `click`: the release, before snapping, came back +/// within half a pixel of the press (`tool_input`). A selection drag that snapping pulled back onto +/// its press point is not one (#11392). +pub(crate) fn finish_release(app: &mut PhotocraftApp, d: Drag, click: bool) { + finish(app, d, Some(click)); +} + +/// `click`: whether the gesture was a click, when the caller saw the release before snapping; +/// otherwise the end point decides. +fn finish(app: &mut PhotocraftApp, mut d: Drag, click: Option) { // Control can select a different tool and release before the next frame cancels the drag. // Cancel before even recording a Shift-stroke anchor, including the preview fallback paths. if d.tool == Tool::MixerBrush && app.ui.tool != Tool::MixerBrush { @@ -20,9 +33,9 @@ pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) { let (sx, sy) = snap_aliased_tip(app, d.tool, end[0], end[1]); end[0] = sx; end[1] = sy; - if let Some(floating) = d.sel_move { - finish_selection_drag(app, floating, d.start, [end[0], end[1]]); - return; + if d.sel_move.is_some() { + let Some(line) = finish_selection_release(app, &mut d, end, click) else { return }; + end = line; } // Where the next ⇧-click line starts. if crate::stroke_constraint::connects(d.tool) @@ -153,6 +166,41 @@ pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) { } } +/// The release of selection drag `d` at `end` (`click`: see [`finish`]). A Single Row / Single +/// Column click inside the selection or on a floating piece selects a line, as it does elsewhere, +/// instead of deselecting or dropping as other marquees do (#11392): the point it selects at, for +/// the tool's own release to go on with. `None`: the release is done. A click ends here unsnapped +/// (`tool_input`), so it is never a move; the line snaps as a click outside the selection does (its +/// press began as a move, which keeps the press point). +fn finish_selection_release(app: &mut PhotocraftApp, d: &mut Drag, end: [f64; 3], click: Option) -> Option<[f64; 3]> { + let floating = d.sel_move?; + let unmoved = (end[0] - d.start[0]).round() == 0.0 && (end[1] - d.start[1]).round() == 0.0; + let click = click.unwrap_or(unmoved); + if unmoved && !click { + // A drag that snapping pulled back where it started leaves the selection as it was. Its + // preview of a floating piece goes too: nothing changed for it to go stale with, and a + // Move-tool drag's release (`move_ui::finish`) is not reached. It is rebuilt as needed. + app.move_preview = None; + return None; + } + if !click || !matches!(d.tool, Tool::SingleRowMarquee | Tool::SingleColumnMarquee) { + finish_selection_drag(app, floating, d.start, [end[0], end[1]]); + return None; + } + // The piece drops where it is first, as a press off it drops it (`tool_input`). + if floating + && app.session.active().and_then(photocraft_engine::float_cmds::floating).is_some() + && let Err(e) = app.run("select.drop", json!({})) + { + app.ui.status = e; + app.ui.status_error = true; + return None; + } + let [x, y] = crate::snap_ui::snap_click(app, d.start, d.live); + d.points.push([x, y, end[2]]); + Some([x, y, end[2]]) +} + /// Selection mode from the options bar, overridden by modifier keys (⇧ add, ⌥ subtract, ⇧⌥ intersect). /// The cursor badge announces the same mode (`tool_feedback`). pub(crate) fn selection_mode(app: &PhotocraftApp, m: egui::Modifiers) -> &'static str { diff --git a/crates/ui-egui/src/canvas/snap_ui.rs b/crates/ui-egui/src/canvas/snap_ui.rs index 84b4f785dc..989af47e1c 100644 --- a/crates/ui-egui/src/canvas/snap_ui.rs +++ b/crates/ui-egui/src/canvas/snap_ui.rs @@ -317,6 +317,19 @@ pub fn begin_guide(app: &mut PhotocraftApp, p: [f64; 2]) { app.prefs_rt.snap = Some(ActiveSnap { gesture: Gesture::Guide, start: p, targets, smart: SnapTargets::default(), disabled: false }); } +/// Snap `p` as a point press does (`Gesture::Point`), for a press that began as a selection move +/// and ended as a click: a Single Row / Column Marquee click inside the selection selects the line +/// at the snapped point, as one outside does (#11392). Not to the selection's own bounds, which the +/// click replaces (on a 1 px line they would pick the line beside it). Ctrl held (`mods`) turns +/// snapping off, as during a drag. +pub(crate) fn snap_click(app: &PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) -> [f64; 2] { + if !snap_on(app) || override_held(mods) { + return p; + } + let targets = build(app, &[], false).filtered(|k| k != SnapKind::Selection); + targets.snap_point(p, tolerance(app)).0 +} + /// Round to whole pixels when Preferences › Tools asks vector tools and transforms to snap to /// the pixel grid. fn pixel_round(app: &PhotocraftApp, p: [f64; 2]) -> [f64; 2] { diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 0b47c8447f..19758b6691 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -1,7 +1,7 @@ use super::cancel_stale_vector_drag; use super::cursors::{alt_eyedropper, sample_eyedropper}; use super::drag::Drag; -use super::gesture::{finish_gesture, polygon_click, selection_mode}; +use super::gesture::{finish_release, polygon_click, selection_mode}; use super::selection::finish_selection_drag; use super::strokes::{HOLD_REPEAT_MS, begin_live_stroke, feed_live_stroke, paint_target, snap_aliased_tip, stroke_params, strokes_live}; use super::{PhotocraftApp, Tool, json, multiple_color_target}; @@ -398,8 +398,10 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) return; } // Marquee / lasso inside the selection: drag the outline, or ⌘-drag to cut the selected - // pixels into a floating piece (`select.float`) and drag that. - if let Some(cut) = selection_drag_kind(app, tool, [x, y], mods) { + // pixels into a floating piece (`select.float`) and drag that. Inside is judged where the + // press was before snapping, as `snap_ui::begin` judges it: snapped, a press just outside + // can land on the selection's edge (#11392). + if let Some(cut) = selection_drag_kind(app, tool, unsnapped(raw), mods) { // ⌘⌥ on a floating piece leaves it where it is and drags a copy of it. if cut && mods.alt @@ -553,6 +555,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) } return; } + note_left_press(app, raw); tool_move(app, x, y, pressure, mods); feed_live_stroke(app); } @@ -578,9 +581,12 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); let [x, y] = crate::stroke_constraint::constrain(d.tool, &mut d.constrain, d.start, last, [x, y], mods.shift, zoom); let [x, y] = crate::move_mods::selection_drag_to(&mut d, [x, y], mods.shift && app.active_tool() != Tool::Move); + let (click, [x, y]) = selection_release(&d, raw, [x, y]); // A Move-tool click (released where it was pressed) selects, it never moves: snapping - // the release point would otherwise nudge the layer onto a nearby edge. - if d.tool == Tool::Move && d.points.len() < 2 && matches!(raw, ToolEvent::Up { x, y } if [x, y] == d.start) { + // the release point would otherwise nudge the layer onto a nearby edge. A drag that + // snapping held at the press point (no points recorded) and that came back there is + // not a click: it keeps a floating piece floating (#11392). + if d.tool == Tool::Move && !d.left_press && d.points.len() < 2 && matches!(raw, ToolEvent::Up { x, y } if [x, y] == d.start) { app.move_preview = None; crate::move_mods::finish(app); if d.sel_move.is_some() { @@ -592,12 +598,47 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) d.points.push([x, y, d.points.last().map_or(1.0, |p| p[2])]); app.stylus.record_point(); } - finish_gesture(app, d); + finish_release(app, d, click); crate::move_mods::finish(app); } } } +/// Where pointer event `raw` (before snapping) was. +fn unsnapped(raw: ToolEvent) -> [f64; 2] { + match raw { + ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } | ToolEvent::Up { x, y } => [x, y], + } +} + +/// Is `p` within half a pixel of the press of `d` (a click's reach, a zoomed-in hand's jitter +/// included)? +fn at_press(d: &Drag, p: [f64; 2]) -> bool { + (p[0] - d.start[0]).round() == 0.0 && (p[1] - d.start[1]).round() == 0.0 +} + +/// Records on the drag that pointer move `raw` (before snapping) left the press (`Drag::left_press`). +fn note_left_press(app: &mut PhotocraftApp, raw: ToolEvent) { + if let ToolEvent::Move { x, y, .. } = raw + && let Some(d) = app.drag.as_mut() + { + d.left_press |= !at_press(d, [x, y]); + } +} + +/// Whether the release `raw` of selection drag `d` is a click, and where it ends (`p`: snapped). A +/// click never leaves the press (`at_press`) before snapping, and ends where it was pressed, as +/// with snapping off: snapping the release would nudge the selection's edges onto a nearby guide or +/// the canvas edge, moving it instead of deselecting (a Single Row / Column Marquee: selecting the +/// line). A drag that snapping pulls back onto the press point, or that comes back to it, is not a +/// click (#11392). +fn selection_release(d: &Drag, raw: ToolEvent, p: [f64; 2]) -> (bool, [f64; 2]) { + match raw { + ToolEvent::Up { x, y } if d.sel_move.is_some() && !d.left_press && at_press(d, [x, y]) => (true, d.start), + _ => (false, p), + } +} + /// Why a press with `tool` must not start: it would paint on or move a hidden target layer (the /// engine refuses the command; refusing the press keeps the live stroke from drawing first). fn hidden_target(app: &PhotocraftApp, tool: Tool) -> Option<&'static str> { @@ -629,12 +670,15 @@ fn covers(sel: &photocraft_raster::Surface, p: [f64; 2]) -> bool { /// moves the floating piece (⌘ cuts one first, ⌘⌥ copies; a floating piece moves with a plain /// drag; the Polygonal Lasso and Magic Wand need ⌘, as a plain press there places or samples), /// `Some(false)` just the outline (no ⇧ / ⌥ and the options bar on New Selection, so a combining -/// drag still draws). +/// drag still draws). The Single Row / Single Column Marquee drag the outline too (#11392); a click +/// without moving, inside the selection or on a floating piece (dropped first), still selects the +/// line under the pointer (`finish_gesture`). pub fn selection_drag_kind(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option { // Click-driven selection tools (a press places a point or samples): only ⌘ drags the // selection, and not while a polygon is being drawn. let clicky = matches!(tool, Tool::PolygonLasso | Tool::MagicWand); - if !(clicky || matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::Lasso)) || (clicky && !app.ui.polygon.is_empty()) { + let marquee = matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::SingleRowMarquee | Tool::SingleColumnMarquee); + if !(clicky || marquee || tool == Tool::Lasso) || (clicky && !app.ui.polygon.is_empty()) { return None; } // ⌘ cuts, ⌘⌥ copies (`select.float`'s `copy`). diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d042afba59..3819430d7f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -896,6 +896,9 @@ mod dropdown_press_tests; #[cfg(test)] mod marquee_tests; +#[cfg(test)] +mod single_line_marquee_move_tests; + #[cfg(test)] mod guide_cursor_tests; diff --git a/crates/ui-egui/src/single_line_marquee_move_tests.rs b/crates/ui-egui/src/single_line_marquee_move_tests.rs new file mode 100644 index 0000000000..01c2286137 --- /dev/null +++ b/crates/ui-egui/src/single_line_marquee_move_tests.rs @@ -0,0 +1,367 @@ +//! Single Row / Single Column Marquee drags through the real canvas (#11392): a drag that starts +//! inside the selection moves it, as the other marquees' does; one that starts outside, a click +//! inside, and a ⇧ / ⌥ press still select the line under the release. + +use egui::Modifiers; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// An 800 × 600 document with snapping off, so the drags land where they are sent. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600})).unwrap(); + app.ui.extras.snap = false; + app +} + +/// A drag with `tool` from `a` to `b`, as the canvas sends it. +fn drag(app: &mut PhotocraftApp, tool: Tool, a: [f64; 2], b: [f64; 2], m: Modifiers) { + app.ui.tool = tool; + tool_event(app, ToolEvent::Down { x: a[0], y: a[1], pressure: 1.0 }, m); + let mid = [(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0]; + tool_event(app, ToolEvent::Move { x: mid[0], y: mid[1], pressure: 1.0 }, m); + tool_event(app, ToolEvent::Move { x: b[0], y: b[1], pressure: 1.0 }, m); + tool_event(app, ToolEvent::Up { x: b[0], y: b[1] }, m); +} + +fn click(app: &mut PhotocraftApp, tool: Tool, p: [f64; 2], m: Modifiers) { + app.ui.tool = tool; + tool_event(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, m); + tool_event(app, ToolEvent::Up { x: p[0], y: p[1] }, m); +} + +fn bounds(app: &PhotocraftApp) -> Rect { + app.session.active().unwrap().doc.selection.as_ref().map_or(Rect::EMPTY, |s| s.content_bounds()) +} + +fn selected(app: &PhotocraftApp, x: i32, y: i32) -> bool { + app.session.active().unwrap().doc.selection.as_ref().is_some_and(|s| s.sample_channel(x, y, 0) > 0.5) +} + +fn step(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().history.undo_label().map(str::to_string) +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.entries().len() +} + +/// Rows 150 and 250 (New, then ⇧ adds). +fn two_rows(app: &mut PhotocraftApp) { + click(app, Tool::SingleRowMarquee, [300.0, 250.5], Modifiers::NONE); + click(app, Tool::SingleRowMarquee, [200.0, 150.5], Modifiers::SHIFT); + assert_eq!(bounds(app), Rect::new(0, 150, 800, 251)); +} + +/// Columns 200 and 300. +fn two_columns(app: &mut PhotocraftApp) { + click(app, Tool::SingleColumnMarquee, [300.5, 250.0], Modifiers::NONE); + click(app, Tool::SingleColumnMarquee, [200.5, 150.0], Modifiers::SHIFT); + assert_eq!(bounds(app), Rect::new(200, 0, 301, 600)); +} + +/// What the Rectangular Marquee's move of the same selection gives: where the part moved past the +/// canvas goes, and the History step's name. +fn rect_marquee_move(select: impl Fn(&mut PhotocraftApp), a: [f64; 2], b: [f64; 2]) -> (Rect, Option) { + let mut app = app(); + select(&mut app); + drag(&mut app, Tool::RectMarquee, a, b, Modifiers::NONE); + (bounds(&app), step(&app)) +} + +#[test] +fn single_row_drag_from_inside_moves_the_selection() { + let mut app = app(); + two_rows(&mut app); + let before = steps(&app); + drag(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], [400.0, 320.5], Modifiers::NONE); + assert!(selected(&app, 450, 220) && selected(&app, 450, 320), "both rows moved by +100,+70"); + assert!(!selected(&app, 450, 150) && !selected(&app, 450, 250), "the old rows are gone"); + assert!(!selected(&app, 50, 220), "moved right by 100"); + assert_eq!(steps(&app), before + 1, "one step"); + assert_eq!((bounds(&app), step(&app)), rect_marquee_move(two_rows, [300.0, 250.5], [400.0, 320.5]), "as the Rectangular Marquee moves it"); +} + +#[test] +fn single_column_drag_from_inside_moves_the_selection() { + let mut app = app(); + two_columns(&mut app); + let before = steps(&app); + drag(&mut app, Tool::SingleColumnMarquee, [300.5, 250.0], [400.5, 320.0], Modifiers::NONE); + assert!(selected(&app, 300, 400) && selected(&app, 400, 400), "both columns moved by +100,+70"); + assert!(!selected(&app, 200, 400), "the old column 200 is gone"); + assert!(!selected(&app, 300, 30), "moved down by 70"); + assert_eq!(steps(&app), before + 1, "one step"); + assert_eq!((bounds(&app), step(&app)), rect_marquee_move(two_columns, [300.5, 250.0], [400.5, 320.0]), "as the Rectangular Marquee moves it"); +} + +#[test] +fn drag_from_outside_selects_the_line_at_the_release() { + let mut app = app(); + two_rows(&mut app); + drag(&mut app, Tool::SingleRowMarquee, [100.0, 100.5], [100.0, 200.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 200, 800, 201)); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + two_columns(&mut app); + drag(&mut app, Tool::SingleColumnMarquee, [100.5, 100.0], [150.5, 100.0], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(150, 0, 151, 600)); + assert_eq!(step(&app).as_deref(), Some("Single Column Marquee")); +} + +#[test] +fn click_inside_selects_the_line_under_the_pointer() { + let mut app = app(); + two_rows(&mut app); + click(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 250, 800, 251), "row 250 alone, not deselected"); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + two_columns(&mut app); + click(&mut app, Tool::SingleColumnMarquee, [200.5, 150.0], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(200, 0, 201, 600), "column 200 alone, not deselected"); + assert_eq!(step(&app).as_deref(), Some("Single Column Marquee")); +} + +/// With snapping on, a click inside is still a click when the selection's edge is within reach of +/// the canvas edge: the snapped release moved both rows up 3 px ("Transform Selection") instead, +/// and the Rectangular Marquee's selection with them instead of deselecting. +#[test] +fn click_inside_with_snapping_on_selects_the_line() { + let mut app = app(); + click(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], Modifiers::NONE); + click(&mut app, Tool::SingleRowMarquee, [200.0, 3.5], Modifiers::SHIFT); + assert_eq!(bounds(&app), Rect::new(0, 3, 800, 251)); + app.ui.extras.snap = true; + click(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 250, 800, 251)); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + app.run("select.rect", json!({"x": 3, "y": 3, "width": 100, "height": 50})).unwrap(); + click(&mut app, Tool::RectMarquee, [50.5, 30.5], Modifiers::NONE); + assert!(app.session.active().unwrap().doc.selection.is_none(), "deselected, not moved onto the edge"); +} + +/// A drag from inside that snapping pulls back to where it started (the selection's edge on a +/// guide, 2 px away) is not a click: the selection stays, with no step. It took the click's path: +/// one line replaced the rows, a floating piece was dropped, and the Rectangular Marquee deselected. +#[test] +fn a_drag_snapped_back_to_its_start_leaves_the_selection() { + let floats = |app: &PhotocraftApp| app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 150})).unwrap(); + app.ui.extras.snap = false; + two_rows(&mut app); + app.ui.extras.snap = true; + let before = steps(&app); + drag(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], [300.0, 252.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 150, 800, 251), "both rows where they were"); + assert_eq!(steps(&app), before, "no step"); + + drag(&mut app, Tool::RectMarquee, [300.0, 250.5], [300.0, 252.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 150, 800, 251), "the Rectangular Marquee keeps them too"); + + app.run("select.rect", json!({"x": 100, "y": 150, "width": 200, "height": 100})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.float", json!({"dx": 0, "dy": 0})).unwrap(); + drag(&mut app, Tool::SingleRowMarquee, [200.0, 200.5], [200.0, 202.5], Modifiers::NONE); + assert!(floats(&app), "the piece still floats"); + assert_eq!(bounds(&app), Rect::new(100, 150, 300, 250)); +} + +/// A drag from inside that goes away and comes back to the press is not a click either: the +/// selection stays where it was, with no step. Only the release was checked, so it took the +/// click's path (one line replaced the rows, the piece was dropped, the Rectangular Marquee +/// deselected). +#[test] +fn a_drag_that_returns_to_its_start_leaves_the_selection() { + let floats = |app: &PhotocraftApp| app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + let out_and_back = |app: &mut PhotocraftApp, tool: Tool, p: [f64; 2]| { + app.ui.tool = tool; + tool_event(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: p[0] + 60.0, y: p[1] + 40.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: p[0] + 0.2, y: p[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: p[0] + 0.2, y: p[1] }, Modifiers::NONE); + }; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap(); + app.ui.extras.snap = false; + two_rows(&mut app); + let before = steps(&app); + out_and_back(&mut app, Tool::SingleRowMarquee, [300.0, 250.5]); + assert_eq!(bounds(&app), Rect::new(0, 150, 800, 251), "both rows where they were"); + assert_eq!(steps(&app), before, "no step"); + + two_columns(&mut app); + out_and_back(&mut app, Tool::SingleColumnMarquee, [300.5, 250.0]); + assert_eq!(bounds(&app), Rect::new(200, 0, 301, 600)); + + out_and_back(&mut app, Tool::RectMarquee, [300.5, 250.0]); + assert_eq!(bounds(&app), Rect::new(200, 0, 301, 600), "the Rectangular Marquee keeps them too"); + + app.run("select.rect", json!({"x": 100, "y": 150, "width": 200, "height": 100})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.float", json!({"dx": 0, "dy": 0})).unwrap(); + out_and_back(&mut app, Tool::SingleRowMarquee, [200.0, 200.5]); + assert!(floats(&app), "the piece still floats"); + assert_eq!(bounds(&app), Rect::new(100, 150, 300, 250)); +} + +/// A click whose release drifts under a pixel from the press (a zoomed-in hand jitter) is still a +/// click with snapping on: only an exactly still release ended unsnapped, so the jittered one +/// snapped the rows onto the canvas edge ("Transform Selection") and the Rectangular Marquee's +/// selection with them instead of deselecting. +#[test] +fn a_jittered_click_inside_with_snapping_on_is_still_a_click() { + let jittered_click = |app: &mut PhotocraftApp, tool: Tool, p: [f64; 2]| { + app.ui.tool = tool; + let q = [p[0] + 0.2, p[1] - 0.3]; + tool_event(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: q[0], y: q[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: q[0], y: q[1] }, Modifiers::NONE); + }; + let mut app = app(); + click(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], Modifiers::NONE); + click(&mut app, Tool::SingleRowMarquee, [200.0, 3.5], Modifiers::SHIFT); + app.ui.extras.snap = true; + jittered_click(&mut app, Tool::SingleRowMarquee, [300.0, 250.5]); + assert_eq!(bounds(&app), Rect::new(0, 250, 800, 251), "row 250 alone, not both rows moved up 3 px"); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + app.run("select.rect", json!({"x": 3, "y": 3, "width": 100, "height": 50})).unwrap(); + jittered_click(&mut app, Tool::RectMarquee, [50.5, 30.5]); + assert!(app.session.active().unwrap().doc.selection.is_none(), "deselected, not moved onto the edge"); +} + +/// With snapping on, a click inside the selection snaps the line onto a guide 2 px away, as a +/// click outside it does; the press began as a move, which kept it on the pointer. +#[test] +fn click_inside_snaps_the_line_to_a_guide() { + let mut app = app(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 252})).unwrap(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 302})).unwrap(); + // The selections are made unsnapped; only the click under test snaps. + let snapped_click = |app: &mut PhotocraftApp, tool: Tool, p: [f64; 2]| { + app.ui.extras.snap = true; + click(app, tool, p, Modifiers::NONE); + app.ui.extras.snap = false; + }; + + snapped_click(&mut app, Tool::SingleRowMarquee, [600.0, 250.5]); + assert_eq!(bounds(&app), Rect::new(0, 252, 800, 253), "outside the selection: snapped to the guide"); + + two_rows(&mut app); + snapped_click(&mut app, Tool::SingleRowMarquee, [600.0, 250.5]); + assert_eq!(bounds(&app), Rect::new(0, 252, 800, 253), "inside: snapped to the guide too"); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + two_columns(&mut app); + snapped_click(&mut app, Tool::SingleColumnMarquee, [300.5, 500.0]); + assert_eq!(bounds(&app), Rect::new(302, 0, 303, 600), "a column inside snaps to the guide"); + assert_eq!(step(&app).as_deref(), Some("Single Column Marquee")); + + // No guide within reach: the line under the pointer, not the selection's own edge. + two_rows(&mut app); + snapped_click(&mut app, Tool::SingleRowMarquee, [600.0, 150.5]); + assert_eq!(bounds(&app), Rect::new(0, 150, 800, 151)); +} + +#[test] +fn combining_drag_from_inside_selects_a_line() { + let mut app = app(); + two_rows(&mut app); + // ⇧ adds the row at the release; nothing moves. + drag(&mut app, Tool::SingleRowMarquee, [300.0, 250.5], [400.0, 320.5], Modifiers::SHIFT); + assert!(selected(&app, 10, 150) && selected(&app, 10, 250) && selected(&app, 10, 320)); + // ⌥ subtracts the row under the release. + drag(&mut app, Tool::SingleRowMarquee, [300.0, 320.5], [350.0, 250.5], Modifiers::ALT); + assert!(selected(&app, 10, 150) && !selected(&app, 10, 250) && selected(&app, 10, 320)); + + two_columns(&mut app); + drag(&mut app, Tool::SingleColumnMarquee, [300.5, 250.0], [400.5, 320.0], Modifiers::SHIFT); + assert!(selected(&app, 200, 10) && selected(&app, 300, 10) && selected(&app, 400, 10)); +} + +/// Ctrl held at the release turns snapping off for a click inside, as it does during a drag: the +/// line under the pointer, not the guide 2 px away. +#[test] +fn ctrl_release_inside_does_not_snap_the_line_to_a_guide() { + let mut app = app(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 252})).unwrap(); + two_rows(&mut app); + app.ui.extras.snap = true; + app.ui.tool = Tool::SingleRowMarquee; + tool_event(&mut app, ToolEvent::Down { x: 600.0, y: 250.5, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 600.0, y: 250.5 }, Modifiers::CTRL); + assert_eq!(bounds(&app), Rect::new(0, 250, 800, 251)); +} + +/// A click on a floating piece drops it where it is and selects the line under the pointer, as +/// before a press on the piece dragged it: a click inside the selection selects a line with these +/// tools. It only dropped an unmoved piece (keeping its outline) and left a moved one floating. +#[test] +fn click_on_a_floating_piece_drops_it_and_selects_the_line() { + let floats = |app: &PhotocraftApp| app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap(); + app.ui.extras.snap = false; + app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 100})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + + app.run("select.float", json!({"dx": 0, "dy": 0})).unwrap(); + click(&mut app, Tool::SingleRowMarquee, [200.0, 150.5], Modifiers::NONE); + assert!(!floats(&app), "an unmoved piece is dropped"); + assert_eq!(bounds(&app), Rect::new(0, 150, 800, 151), "row 150, not the piece's outline"); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 100})).unwrap(); + app.run("select.float", json!({"dx": 50, "dy": 20})).unwrap(); + click(&mut app, Tool::SingleColumnMarquee, [250.5, 170.0], Modifiers::NONE); + assert!(!floats(&app), "a moved piece is dropped where it is"); + assert_eq!(bounds(&app), Rect::new(250, 0, 251, 600)); + assert_eq!(step(&app).as_deref(), Some("Single Column Marquee")); +} + +/// With snapping on, a drag pressed just outside a selected row is a drag from outside: a new row +/// at the release. The snapped press landed on the row's edge, which counted as inside, so the drag +/// moved the selection instead. +#[test] +fn a_press_that_snaps_onto_the_selection_is_still_outside_it() { + let mut app = app(); + two_rows(&mut app); + app.ui.extras.snap = true; + drag(&mut app, Tool::SingleRowMarquee, [300.0, 148.5], [300.0, 100.5], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(0, 100, 800, 101), "a new row at the release, not both rows moved up"); + assert_eq!(step(&app).as_deref(), Some("Single Row Marquee")); + + two_columns(&mut app); + drag(&mut app, Tool::SingleColumnMarquee, [198.5, 300.0], [100.5, 300.0], Modifiers::NONE); + assert_eq!(bounds(&app), Rect::new(100, 0, 101, 600)); + assert_eq!(step(&app).as_deref(), Some("Single Column Marquee")); +} + +/// A Move-tool drag of a floating piece that snapping pulls back to its start, released exactly at +/// the press, is not a click: the piece keeps floating. The Move tool's click shortcut dropped it. +#[test] +fn a_snapped_back_move_tool_drag_keeps_the_piece_floating() { + let floats = |app: &PhotocraftApp| app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 150})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 150, "width": 200, "height": 100})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.float", json!({"dx": 0, "dy": 0})).unwrap(); + app.ui.extras.snap = true; + app.ui.tool = Tool::Move; + let p = [200.0, 200.5]; + tool_event(&mut app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: p[0], y: p[1] + 2.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: p[0], y: p[1] }, Modifiers::NONE); + assert!(floats(&app), "the piece still floats"); + assert!(app.move_preview.is_none(), "no preview left from the drag"); +}