Skip to content

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

Lasso: ⌘-drag outside the selection moves the layer (#2725) (#2737)

Valentin Klein719465dc5b8f
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rs
index 67d5ace98..b03eea3b4 100644
--- a/crates/ui-egui/src/lasso_ui.rs
+++ b/crates/ui-egui/src/lasso_ui.rs
@@ -54,9 +54,10 @@ fn moving_selection(app: &PhotocraftApp) -> bool {
}
/// A drag the canvas handles rather than this module, which only feeds it the events: moving the
-/// selection, or a guide that ⌘ took (`canvas::command_guide_at`, #2690).
+/// selection, a guide that ⌘ took (`canvas::command_guide_at`, #2690), or the layer that ⌘ moves
+/// (`canvas::command_moves_layer`, refused on a locked layer: `move_blocked`, #2725).
fn handed_over(app: &PhotocraftApp) -> bool {
- moving_selection(app) || app.guide_drag.is_some_and(|d| d.index.is_some())
+ moving_selection(app) || app.guide_drag.is_some_and(|d| d.index.is_some()) || app.drag.as_ref().is_some_and(|d| d.tool == Tool::Move) || app.move_blocked
}
pub fn cancel_stale(app: &mut PhotocraftApp) {
@@ -112,11 +113,12 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
let p = match ev {
ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } | ToolEvent::Up { x, y } => [x, y],
};
- // A press inside the selection (no outline in progress) drags the selection instead.
+ // A press inside the selection (no outline in progress) drags the selection instead; a ⌘
+ // press outside it moves the layer, as the Move tool (#2725).
if matches!(ev, ToolEvent::Down { .. })
&& !active(app)
&& p.iter().all(|n| n.is_finite())
- && crate::canvas::selection_drag_kind(app, Tool::Lasso, p, mods).is_some()
+ && (crate::canvas::selection_drag_kind(app, Tool::Lasso, p, mods).is_some() || crate::canvas::command_moves_layer(app, Tool::Lasso, p, mods))
{
return false;
}
diff --git a/crates/ui-egui/src/lasso_ui/tests.rs b/crates/ui-egui/src/lasso_ui/tests.rs
index e76355b56..316c7fada 100644
--- a/crates/ui-egui/src/lasso_ui/tests.rs
+++ b/crates/ui-egui/src/lasso_ui/tests.rs
@@ -291,3 +291,30 @@ fn clicking_the_last_point_again_closes_the_outline() {
assert!(!active(&app));
assert!(app.session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(150, 100, 0) > 0.9);
}
+
+/// ⌘ (Ctrl) with the Lasso is the Move tool for the drag: through the real canvas, a ⌘-drag outside
+/// the selection (here: without one) moves the layer and draws no outline (`canvas::command_moves_layer`).
+#[test]
+fn real_canvas_cmd_drag_outside_the_selection_moves_the_layer() {
+ let mut h = harness();
+ h.state_mut().ui.extras.snap = false;
+ h.state_mut().run("layer.new.layer", json!({})).unwrap();
+ let layer = h.state().session.active().unwrap().active_layer.unwrap();
+ h.state_mut()
+ .session
+ .edit("paint", |doc, _| {
+ doc.layer_mut(layer).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(100, 100, 140, 140), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ h.run_steps(1);
+ mouse(&mut h, "down", 120.0, 120.0, Modifiers::COMMAND);
+ mouse(&mut h, "move", 130.0, 120.0, Modifiers::COMMAND);
+ mouse(&mut h, "move", 150.0, 120.0, Modifiers::COMMAND);
+ mouse(&mut h, "up", 150.0, 120.0, Modifiers::COMMAND);
+ let app = h.state();
+ let alpha = |x, y| app.session.active().unwrap().doc.layer(layer).unwrap().surface().unwrap().rgba(x, y)[3];
+ assert!(app.drag.is_none() && !active(app), "no outline in progress");
+ assert!(app.session.active().unwrap().doc.selection.is_none(), "no selection was made");
+ assert_eq!((alpha(105, 120), alpha(165, 120)), (0.0, 1.0), "the layer moved 30 px to the right");
+}