Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Move tool: the drag's box and snapping follow the Auto-Selected layer (#2666) (#2675)

Valentin Kleinf158cd45aad9
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index cd112da0a..ae7b63650 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3860,7 +3860,12 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
if !crate::move_ui::moves_selected_pixels(app) && app.ui.tool_options.move_auto_select != mods.command {
let target = app.ui.tool_options.move_target.clone();
let mode = if mods.shift { "add" } else { "replace" };
+ let before = app.session.active().map(|st| st.selected_layers());
let _ = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode}));
+ // Snapping (and the drag's box) started before the pick: point it at what moves.
+ if app.session.active().map(|st| st.selected_layers()) != before {
+ crate::snap_ui::retarget_move(app, [x, y]);
+ }
}
// A locked layer: no drag, and Photoshop's message once the pointer moves (`move_lock`).
if crate::move_lock::blocked(app, tool, [x, y], mods) {
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index 7e9875d24..edfb0e11c 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -140,6 +140,13 @@ fn override_held(mods: egui::Modifiers) -> bool {
mods.ctrl && !mods.mac_cmd
}
+/// The Move tool's press at `p` picked other layers (Auto-Select): the drag moves those, so its
+/// box, what it snaps and what it leaves out of the targets are theirs, not the ones selected
+/// before (a group or an empty layer selected before had no box at all).
+pub(crate) fn retarget_move(app: &mut PhotocraftApp, p: [f64; 2]) {
+ begin(app, p);
+}
+
/// Start snapping for a drag beginning at `p` (called on pointer down).
fn begin(app: &mut PhotocraftApp, p: [f64; 2]) {
app.prefs_rt.snap = None;
@@ -402,6 +409,35 @@ mod tests {
assert!(app.prefs_rt.snap.is_none());
}
+ /// The drag's box and snapping belong to the layer Auto-Select picks on the press, not to the
+ /// one selected before: another layer (its box would move instead), or an empty layer (no box,
+ /// no snapping at all).
+ #[test]
+ fn move_tool_box_and_snapping_follow_the_auto_selected_layer() {
+ for before in ["target", "empty"] {
+ let mut app = app_with_box();
+ let mover = app.session.active().unwrap().active_layer.unwrap();
+ if before == "empty" {
+ app.run("layer.new.layer", json!({"name": "empty"})).unwrap();
+ } else {
+ let st = app.session.active().unwrap();
+ let target = st.doc.walk().into_iter().find(|(_, _, l)| l.name == "target").unwrap().2.id;
+ app.run("layer.select", json!({"layer": target.0})).unwrap();
+ }
+ app.ui.tool = Tool::Move;
+ app.ui.tool_options.move_auto_select = true;
+ let m = egui::Modifiers::NONE;
+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m);
+ assert_eq!(app.session.active().unwrap().active_layer, Some(mover), "{before}: Auto-Select picked the mover");
+ let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone());
+ assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 90.0, 80.0] }), "{before}: the box is the mover's");
+ // The mover's left edge lands 3 px right of the target's and snaps to it.
+ crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m);
+ crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m);
+ assert_eq!(mover_bounds(&app).x0, 300, "{before}: snapped to the target's edge");
+ }
+ }
+
#[test]
fn ctrl_or_snap_off_disables_snapping() {
let mut app = app_with_box();