From 6d486a4c29abc3ebb800f4b0bd64dd2ff6847553 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 05:59:21 -0500 Subject: [PATCH] Selection tools: dragging the outline records "Move Selection", as the reference does (#11390) A drag inside the selection with the Rectangular Marquee, Elliptical Marquee or Lasso now records the History step "Move Selection" instead of "Transform Selection". select.transformSelection takes an optional stepName parameter that accepts only "Move Selection", and the canvas sends it from finish_selection_drag. Select > Transform Selection and arrow-key nudges keep their name. Co-Authored-By: Claude Fable 5.1 --- crates/engine/src/cmd/select_extra_cmds.rs | 26 ++++- crates/ui-egui/src/canvas/selection.rs | 2 +- crates/ui-egui/src/lib.rs | 3 + .../ui-egui/src/selection_move_step_tests.rs | 99 +++++++++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/src/selection_move_step_tests.rs diff --git a/crates/engine/src/cmd/select_extra_cmds.rs b/crates/engine/src/cmd/select_extra_cmds.rs index 55e2ec10d9..9fd0d65356 100644 --- a/crates/engine/src/cmd/select_extra_cmds.rs +++ b/crates/engine/src/cmd/select_extra_cmds.rs @@ -163,6 +163,11 @@ fn affine_params(cmd: &str, p: &Value, r: [f64; 4]) -> Result { /// scale/rotate/move, refused when the result would not fit the transform budget. fn transform_selection(s: &mut Session, p: &Value) -> Result { const CMD: &str = "select.transformSelection"; + let step_name = match p.get("stepName") { + None | Some(Value::Null) => "Transform Selection", + Some(Value::String(n)) if n == "Move Selection" => "Move Selection", + Some(_) => return Err(bad(CMD, "`stepName` must be \"Move Selection\"")), + }; let d = s.active().ok_or(EngineError::NoDocument)?; let sel = d.doc.selection.as_ref().ok_or_else(|| EngineError::Other("no selection".into()))?; let b = sel.content_bounds(); @@ -206,7 +211,7 @@ fn transform_selection(s: &mut Session, p: &Value) -> Result { if h.0.iter().zip(Homography::IDENTITY.0).all(|(a, b)| (a - b).abs() < 1e-12) { return Ok(json!({"changed": false, "selected": true})); } - let selected = s.edit("Transform Selection", |doc, _| { + let selected = s.edit(step_name, |doc, _| { if let Some(sel) = &doc.selection { crate::transform_cmds::check_budget(sel.content_bounds(), &h, "selection", sel.format())?; doc.selection = Some(crate::transform_cmds::warp_gray(sel, &h, interp)?).filter(|s| !s.content_bounds().is_empty()); @@ -250,7 +255,7 @@ pub fn specs() -> Vec { label: "Transform Selection", menu: &["Select"], shortcut: None, - params: r##"{"rect":[x0,y0,x1,y1]? (frame; default = selection bounds),"quad"|"corners":[[x,y]×4]? (where the frame's corners go: distort/perspective),"matrix":[a,b,c,d,e,f]?,"scaleX":%=100,"scaleY":%=100,"rotate":deg=0,"skewX":deg=0,"skewY":deg=0,"dx":px=0,"dy":px=0,"reference":"center|topLeft|top|topRight|left|right|bottomLeft|bottom|bottomRight"|[x,y]="center","style"|"mesh"|"grid"|"warp":… (warp, as edit.transform.warp),"interpolation":"bilinear|bicubic|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bilinear" (Photoshop's bicubic variants render as Bicubic)}"##, + params: r##"{"rect":[x0,y0,x1,y1]? (frame; default = selection bounds),"quad"|"corners":[[x,y]×4]? (where the frame's corners go: distort/perspective),"matrix":[a,b,c,d,e,f]?,"scaleX":%=100,"scaleY":%=100,"rotate":deg=0,"skewX":deg=0,"skewY":deg=0,"dx":px=0,"dy":px=0,"stepName":"Move Selection"? (History step name for a plain move; the UI sends it for a drag of the outline; default "Transform Selection"; any other value is refused),"reference":"center|topLeft|top|topRight|left|right|bottomLeft|bottom|bottomRight"|[x,y]="center","style"|"mesh"|"grid"|"warp":… (warp, as edit.transform.warp),"interpolation":"bilinear|bicubic|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bilinear" (Photoshop's bicubic variants render as Bicubic)}"##, enabled: has_selection, run: transform_selection, journal: true, @@ -422,6 +427,23 @@ mod tests { assert!(s.execute("select.transformSelection", json!({"scaleX": 0})).is_err()); } + #[test] + fn transform_selection_step_name_is_restricted() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 60, "height": 40})).unwrap(); + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + let last = |s: &Session| s.active().unwrap().history.entries().last().cloned().unwrap(); + s.execute("select.transformSelection", json!({"dx": 5, "dy": 0})).unwrap(); + assert_eq!(last(&s), "Transform Selection"); + s.execute("select.transformSelection", json!({"dx": 5, "dy": 0, "stepName": "Move Selection"})).unwrap(); + assert_eq!(last(&s), "Move Selection"); + let n = s.active().unwrap().history.past_len(); + for bad_name in [json!("Crop"), json!(""), json!(7)] { + assert!(s.execute("select.transformSelection", json!({"dx": 5, "dy": 0, "stepName": bad_name})).is_err()); + } + assert_eq!(s.active().unwrap().history.past_len(), n); + } + #[test] fn transform_selection_needs_a_selection() { let mut s = Session::new(); diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs index 67577e2a27..b3e37d1f18 100644 --- a/crates/ui-egui/src/canvas/selection.rs +++ b/crates/ui-egui/src/canvas/selection.rs @@ -96,7 +96,7 @@ pub(super) fn finish_selection_drag(app: &mut PhotocraftApp, floating: bool, sta (false, true) if app.session.is_enabled("select.deselect") => app.run("select.deselect", json!({})), (false, true) => return, (true, false) => app.run("select.float", json!({"dx": dx, "dy": dy})), - (false, false) => app.run("select.transformSelection", json!({"dx": dx, "dy": dy})), + (false, false) => app.run("select.transformSelection", json!({"dx": dx, "dy": dy, "stepName": "Move Selection"})), }; if let Err(e) = r { app.ui.status = e; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index bfe48f9e67..23ec3c5c4a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -913,3 +913,6 @@ mod saved_path_edit_tests; #[cfg(test)] mod pen_path_ownership_tests; + +#[cfg(test)] +mod selection_move_step_tests; diff --git a/crates/ui-egui/src/selection_move_step_tests.rs b/crates/ui-egui/src/selection_move_step_tests.rs new file mode 100644 index 0000000000..5c3ec336fd --- /dev/null +++ b/crates/ui-egui/src/selection_move_step_tests.rs @@ -0,0 +1,99 @@ +//! #11390: dragging the selection outline with a marquee or lasso records "Move Selection", as the +//! reference application does; Select > Transform Selection keeps "Transform Selection". + +use egui::Modifiers; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// A 400 x 300 document with 100,100 to 300,300 selected and `tool` active. +fn app_with_selection(tool: Tool) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + app.sync_views(); + app.ui.extras.snap = false; + app.ui.tool = tool; + app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 200})).unwrap(); + app +} + +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2]) { + let m = Modifiers::NONE; + tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, m); + tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, m); + tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, m); +} + +fn bounds(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds()) +} + +fn last_step(app: &PhotocraftApp) -> String { + app.session.active().unwrap().history.entries().last().cloned().unwrap() +} + +fn past(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn check_drag_records_move_selection(tool: Tool) { + let mut app = app_with_selection(tool); + let before = past(&app); + drag(&mut app, [200.0, 200.0], [300.0, 250.0]); + assert_eq!(bounds(&app), Some(Rect::new(200, 150, 400, 350)), "moved by +100,+50"); + assert_eq!(past(&app), before + 1, "one step"); + assert_eq!(last_step(&app), "Move Selection"); +} + +#[test] +fn rectangular_marquee_drag_inside_records_move_selection() { + check_drag_records_move_selection(Tool::RectMarquee); +} + +#[test] +fn elliptical_marquee_drag_inside_records_move_selection() { + check_drag_records_move_selection(Tool::EllipseMarquee); +} + +#[test] +fn lasso_drag_inside_records_move_selection() { + check_drag_records_move_selection(Tool::Lasso); +} + +#[test] +fn undo_after_the_drag_restores_the_outline() { + let mut app = app_with_selection(Tool::RectMarquee); + drag(&mut app, [200.0, 200.0], [300.0, 250.0]); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(bounds(&app), Some(Rect::new(100, 100, 300, 300))); +} + +#[test] +fn transform_selection_keeps_its_step_name() { + let mut app = app_with_selection(Tool::RectMarquee); + app.run("select.transformSelection", json!({"dx": 100, "dy": 50})).unwrap(); + assert_eq!(bounds(&app), Some(Rect::new(200, 150, 400, 350))); + assert_eq!(last_step(&app), "Transform Selection"); +} + +#[test] +fn a_click_without_moving_still_deselects() { + let mut app = app_with_selection(Tool::RectMarquee); + drag(&mut app, [200.0, 200.0], [200.0, 200.0]); + assert_eq!(bounds(&app), None); + assert_eq!(last_step(&app), "Deselect"); +} + +#[test] +fn an_unknown_step_name_is_refused() { + let mut app = app_with_selection(Tool::RectMarquee); + let before = past(&app); + for name in [json!("Delete Layer"), json!(""), json!(3)] { + assert!(app.run("select.transformSelection", json!({"dx": 10, "dy": 0, "stepName": name})).is_err()); + } + assert_eq!(past(&app), before); + assert_eq!(bounds(&app), Some(Rect::new(100, 100, 300, 300))); +}