diff --git a/crates/engine/src/transform_cmds.rs b/crates/engine/src/transform_cmds.rs index aeb31fca47f..f1b257526e4 100644 --- a/crates/engine/src/transform_cmds.rs +++ b/crates/engine/src/transform_cmds.rs @@ -845,7 +845,16 @@ fn transform(s: &mut Session, p: &Value) -> Result { move |doc, _, ctx| { ctx.stage(0.0, 1.0, "Transforming", |ctl| { if let Some(ids) = &layers_for_job { - transform_layers_doc(doc, ids, &h, affine, interp, ctl) + let several = crate::layer_multi_cmds::top_level(doc, ids).len() > 1; + transform_layers_doc(doc, ids, &h, affine, interp, ctl)?; + // Photoshop drops the selection after transforming several layers (whole + // layers move, so the outline no longer frames anything); in the same edit, + // so one undo brings both back. A list that is one layer (or a group and + // its own children) keeps it. + if several && !h.is_identity() { + doc.selection = None; + } + Ok(()) } else { transform_doc(doc, id, &p, lone, &h, affine, interp, ctl) } @@ -857,8 +866,8 @@ fn transform(s: &mut Session, p: &Value) -> Result { /// Apply one homography to several top-level layers in one edit. Multi-layer transforms move the /// layers as whole objects: a pixel selection does not crop each selected layer independently. -/// The selection outline therefore stays where it is; this matches the Move tool's existing -/// multi-layer move semantics. +/// The selection is left alone here (the live preview keeps showing it); the committed +/// `edit.transform` drops it, as Photoshop does. fn transform_layers_doc(doc: &mut Document, ids: &[LayerId], h: &Homography, affine: Option, interp: Interp, ctl: &Interrupt) -> Result<()> { let ids = crate::layer_multi_cmds::top_level(doc, ids); if ids.is_empty() { @@ -961,7 +970,7 @@ pub fn specs() -> Vec { label: "Free Transform", menu: &[], shortcut: None, - params: r##"{"layer":id? OR "layers":[id,…]? (shared multi-layer transform; top-level layers only),"rect":[x0,y0,x1,y1]? (source frame; default = layer content ∩ selection, or the union of multi-layer content),"quad":[[x,y]×4]? (where the frame's corners go, clockwise from top-left),"matrix":[a,b,c,d,e,f]? (affine alternative),"scaleX":%?,"scaleY":%?,"rotate":deg?,"dx":px?,"dy":px? (relative alternative: scale and rotate about the frame's centre, then move, as a Photoshop action records a Free Transform),"interpolation":"bicubic|bilinear|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bicubic" (Photoshop's bicubic variants render as Bicubic),"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (single-layer only; an unlinked mask, an alpha channel or the Quick Mask transforms alone; a linked mask moves with its layer)}"##, + params: r##"{"layer":id? OR "layers":[id,…]? (shared multi-layer transform; top-level layers only; two or more drop the selection),"rect":[x0,y0,x1,y1]? (source frame; default = layer content ∩ selection, or the union of multi-layer content),"quad":[[x,y]×4]? (where the frame's corners go, clockwise from top-left),"matrix":[a,b,c,d,e,f]? (affine alternative),"scaleX":%?,"scaleY":%?,"rotate":deg?,"dx":px?,"dy":px? (relative alternative: scale and rotate about the frame's centre, then move, as a Photoshop action records a Free Transform),"interpolation":"bicubic|bilinear|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bicubic" (Photoshop's bicubic variants render as Bicubic),"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (single-layer only; an unlinked mask, an alpha channel or the Quick Mask transforms alone; a linked mask moves with its layer)}"##, enabled: has_layer, journal: true, run: transform, @@ -1025,6 +1034,58 @@ mod tests { assert_eq!(b, Rect::new(65, 60, 85, 80)); } + /// Photoshop, with two layers selected and a marquee active: Free Transform moves both whole + /// layers and drops the selection. One undo restores the layers and the selection. + #[test] + fn shared_transform_moves_whole_layers_and_drops_the_selection() { + let mut s = session(); + let first = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let second = s.active().unwrap().active_layer.unwrap(); + s.edit("paint second", |doc, _| { + doc.layer_mut(second).unwrap().surface_mut().unwrap().fill_rect(Rect::new(50, 40, 70, 60), &[0.0, 1.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 15, "height": 100})).unwrap(); + let before_a = s.active().unwrap().doc.layer(first).unwrap().surface().unwrap().content_bounds(); + let sel_before = s.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + + s.execute("edit.transform", json!({"layers": [first.0, second.0], "dx": 20, "dy": 0, "interpolation": "nearest"})).unwrap(); + let st = s.active().unwrap(); + let a = st.doc.layer(first).unwrap().surface().unwrap().content_bounds(); + let b = st.doc.layer(second).unwrap().surface().unwrap().content_bounds(); + assert_eq!(a, Rect::new(before_a.x0 + 20, before_a.y0, before_a.x1 + 20, before_a.y1), "the whole layer moves"); + assert_eq!(b, Rect::new(70, 40, 90, 60)); + assert!(st.doc.selection.is_none(), "the selection is dropped"); + + s.execute("edit.undo", json!({})).unwrap(); + let st = s.active().unwrap(); + assert_eq!(st.doc.layer(first).unwrap().surface().unwrap().content_bounds(), before_a); + assert_eq!(st.doc.selection.as_ref().map(|m| m.content_bounds()), Some(sel_before), "one undo brings the selection back"); + } + + /// A `layers` list that is only one layer is no multi-layer transform: the selection stays. + #[test] + fn a_one_layer_list_keeps_the_selection() { + let mut s = session(); + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 15, "height": 100})).unwrap(); + s.execute("edit.transform", json!({"layers": [id.0], "dx": 20, "dy": 0})).unwrap(); + assert!(s.active().unwrap().doc.selection.is_some()); + } + + /// A single layer keeps its behaviour: only the selected pixels move, and the selection with them. + #[test] + fn single_layer_transform_still_moves_the_selection() { + let mut s = session(); + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); + s.execute("edit.transform", json!({"layer": id.0, "dx": 20, "dy": 0})).unwrap(); + let sel = s.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!((sel.x0, sel.x1), (30, 40)); + } + #[test] fn shared_transform_rejects_invalid_layer_lists() { let mut s = session(); diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs index e19c95a2e78..7adcf8fd34a 100644 --- a/crates/ui-egui/src/snap_ui.rs +++ b/crates/ui-egui/src/snap_ui.rs @@ -215,16 +215,18 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) { let moves_layer = crate::canvas::command_moves_layer(app, tool, p, mods); let crop_tol = crate::crop_ui::tolerance(app); let gesture = if let Some(t) = &app.ui.transform { + // A shared box moves every layer of its session: none of them is a target. + let moving = crate::transform_tool::moving_layers(app).unwrap_or_else(|| vec![LayerId(t.layer)]); let corner = match crate::transform_tool::hit(t, p, crate::transform_tool::handle_tolerance(app)) { - crate::transform_tool::Hit::Corner(index) => t.quad.get(index).map(|&point| (Gesture::TransformCorner { index, point }, vec![LayerId(t.layer)])), + crate::transform_tool::Hit::Corner(index) => t.quad.get(index).map(|&point| (Gesture::TransformCorner { index, point }, moving.clone())), _ => None, }; if t.mode == crate::state::TransformMode::Distort { corner } else if near_handle(&t.quad, p, crate::transform_tool::handle_tolerance(app)) { - corner.or_else(|| Some((Gesture::Point, vec![LayerId(t.layer)]))) + corner.or(Some((Gesture::Point, moving))) } else if in_quad(&t.quad, p) { - Some((Gesture::TransformMove { rect: quad_rect(&t.quad) }, vec![LayerId(t.layer)])) + Some((Gesture::TransformMove { rect: quad_rect(&t.quad) }, moving)) } else { None } @@ -1223,6 +1225,35 @@ mod tests { assert!(app.prefs_rt.snap_lines.is_empty()); } + /// A shared Free Transform box for several layers doesn't snap to those layers' original + /// edges: they move with it. Its left edge starts exactly on the mover's left edge, so a + /// 2 px drag snapped straight back when only the active layer was left out. + #[test] + fn a_shared_transform_box_does_not_snap_to_its_own_layers() { + let mut app = app_with_box(); + let mover = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.new.layer", json!({"name": "second"})).unwrap(); + app.run("select.rect", json!({"x": 120, "y": 40, "width": 40, "height": 40})).unwrap(); + app.run("edit.fill", json!({"color": "#00ff00"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + let second = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.select", json!({"layer": mover.0, "mode": "add"})).unwrap(); + app.ui.tool = Tool::Move; + crate::transform_tool::begin_layers(&mut app, vec![mover, second], [40.0, 40.0, 160.0, 80.0]).unwrap(); + assert_eq!(crate::transform_tool::moving_layers(&app).map(|l| l.len()), Some(2)); + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 50.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 51.0, y: 50.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 52.0, y: 50.0 }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert!((q[0][0] - 42.0).abs() < 1e-6, "the box follows the pointer: {q:?}"); + // The snap still works against a layer that stays put (the target's left edge, x = 300). + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 50.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 306.0, y: 50.0 }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert!((q[0][0] - 300.0).abs() < 1e-6, "{q:?}"); + } + #[test] fn free_transform_drags_and_handles_snap() { let mut app = app_with_box(); diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rs index 73b93221a30..4243de7d858 100644 --- a/crates/ui-egui/src/transform_tool.rs +++ b/crates/ui-egui/src/transform_tool.rs @@ -304,6 +304,11 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> Ok(()) } +/// The layers a shared transform box moves (none outside one): snapping must not target them. +pub(crate) fn moving_layers(app: &PhotocraftApp) -> Option> { + app.multi_transform.as_ref().map(|m| m.layers.clone()) +} + /// Start one shared transform box for several selected layers from Move › Show Transform /// Controls. Every layer is transformed by the same homography around `rect`; the engine commits /// them together as one history step. This path deliberately does not use the single-layer lifted