Skip to content

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

Move tool: ⌘-drag with a painting tool floats the selected pixels from the control channel too (#2768) (#2776)

Valentin Klein5963e5b7f0ae
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index dcb61fdbe..783d56466 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -128,7 +128,7 @@ pub struct Drag {
pub reposition: bool,
/// A marquee or lasso drag that started inside the selection moves it instead of drawing:
/// `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`).
+ /// every Move-tool drag with a selection does (`move_ui::moves_selected_pixels_with`).
pub sel_move: Option<bool>,
pub lasso: Option<crate::lasso_ui::Lasso>,
}
@@ -3787,10 +3787,10 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
// View › Snap / Snap To and smart guides (snap_ui.rs).
let raw = ev;
// A press anywhere but on the floating piece (or with ⇧ / ⌥, to draw) drops it first; the
- // Move tool drags it from anywhere.
+ // Move tool (⌘ with a painting tool too, `event_tool`) drags it from anywhere.
if let ToolEvent::Down { x, y, .. } = raw
&& app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some())
- && !crate::move_ui::moves_selected_pixels(app)
+ && !crate::move_ui::moves_selected_pixels_with(app, event_tool(app, mods))
&& selection_drag_kind(app, app.ui.tool, [x, y], mods) != Some(true)
{
let _ = app.run("select.drop", json!({}));
@@ -3896,7 +3896,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
}
// Auto-Select (or ⌘-click while it is off) picks the layer under the pointer first
// (not when the selected pixels move: those are the active layer's).
- if !crate::move_ui::moves_selected_pixels(app) && app.ui.tool_options.move_auto_select != mods.command {
+ if !crate::move_ui::moves_selected_pixels_with(app, tool) && 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());
@@ -3913,7 +3913,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
}
// With a selection: cut the selected pixels (⌥ copies them) and drag them as a floating
// piece, from anywhere, as a marquee ⌘-drag does.
- if crate::move_ui::moves_selected_pixels(app) {
+ if crate::move_ui::moves_selected_pixels_with(app, tool) {
if crate::move_ui::float_selected(app, mods.alt, 0.0, 0.0) {
let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false);
d.sel_move = Some(true);
@@ -4206,7 +4206,7 @@ pub enum SelCursor {
/// The cursor at document point `p` with `tool` in effect and `mods` held (`None`: not over a
/// selection the press would move). The Move tool moves the selected pixels from anywhere
-/// (`move_ui::moves_selected_pixels`); a selection tool from inside the selection, or on the
+/// (`move_ui::moves_selected_pixels_with`); a selection tool from inside the selection, or on the
/// floating piece (`selection_drag_kind`).
pub fn selection_cursor(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option<SelCursor> {
if let Some(d) = &app.drag {
diff --git a/crates/ui-egui/src/move_lock.rs b/crates/ui-egui/src/move_lock.rs
index 2cef0f223..d7fedbf62 100644
--- a/crates/ui-egui/src/move_lock.rs
+++ b/crates/ui-egui/src/move_lock.rs
@@ -29,9 +29,9 @@ pub fn blocked(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifie
})
};
if tool == Tool::Move {
- // `moves_selected_pixels` is false for a layer whose pixels can't float: then the Move tool
+ // `moves_selected_pixels_with` is false for a layer whose pixels can't float: then the Move tool
// moves the layer, which its position lock refuses too.
- if crate::move_ui::moves_selected_pixels(app) {
+ if crate::move_ui::moves_selected_pixels_with(app, tool) {
return pixels_locked();
}
return !floating && st.doc.selection.is_some() && pixels_locked() || layers_locked();
diff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rs
index 630d66068..c99c23038 100644
--- a/crates/ui-egui/src/move_ui.rs
+++ b/crates/ui-egui/src/move_ui.rs
@@ -17,15 +17,11 @@ use crate::PhotocraftApp;
use crate::state::Tool;
/// Does the Move tool drag (and do the arrow keys nudge) the selected pixels of the active layer
-/// rather than the whole layer? With a selection, as in Photoshop; a layer whose pixels can't
-/// float (type, shape, Smart Object, group, position-locked), a hidden layer or several selected
-/// layers still move whole, as without one.
-pub(crate) fn moves_selected_pixels(app: &PhotocraftApp) -> bool {
- // The Move tool, or ⌘ held with a painting tool (`hold_keys::cmd_moves`).
- moves_selected_pixels_with(app, app.active_tool())
-}
-
-/// [`moves_selected_pixels`] with `tool` in effect (the canvas cursor asks before the press).
+/// rather than the whole layer, with `tool` in effect? With a selection, as in Photoshop; a layer
+/// whose pixels can't float (type, shape, Smart Object, group, position-locked), a hidden layer or
+/// several selected layers still move whole, as without one. The canvas cursor asks before the
+/// press, and a pointer event passes `canvas::event_tool`, which also sees a ⌘ sent with the event
+/// (#2768).
pub(crate) fn moves_selected_pixels_with(app: &PhotocraftApp, tool: Tool) -> bool {
let Some(st) = app.session.active() else { return false };
tool == Tool::Move
@@ -408,6 +404,23 @@ mod tests {
}
}
+ /// ⌘-drag with a painting tool drags the selected pixels as the Move tool does, also when ⌘
+ /// comes with the event (control channel, MCP) rather than as the held-key override.
+ #[test]
+ fn command_drag_with_a_painting_tool_moves_only_the_selected_pixels() {
+ let (mut app, id) = selected_pixels(8);
+ app.ui.tool = Tool::Brush;
+ app.ui.tool_options.move_auto_select = false;
+ let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() };
+ drag(&mut app, [50.0, 40.0], [60.0, 45.0], cmd);
+ assert_eq!(offset(&app), Some((10, 5)), "the selected pixels float");
+ drag(&mut app, [50.0, 40.0], [60.0, 45.0], cmd);
+ assert_eq!(offset(&app), Some((20, 10)), "a second drag moves the same floating piece");
+ app.run("select.drop", json!({})).unwrap();
+ assert!(alpha(&app, id, 10, 10) == 0.0 && alpha(&app, id, 40, 30) == 1.0, "the red square moved");
+ assert_eq!(alpha(&app, id, 48, 16), 1.0, "the unselected blue one stayed");
+ }
+
/// A click moves nothing and leaves nothing floating (Undo isn't spent putting it back).
#[test]
fn a_click_with_a_selection_moves_nothing() {
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index 12d59f277..096d0262a 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -79,11 +79,11 @@ fn build(app: &PhotocraftApp, exclude: &[LayerId], smart: bool) -> SnapTargets {
if smart { t.filtered(SnapKind::is_smart) } else { t }
}
-/// What the Move tool drags: the selection's bounds where its pixels float (`move_ui`), else the
-/// union of the selected layers' bounds.
-fn moving_rect(app: &PhotocraftApp) -> Option<[f64; 4]> {
+/// What a move with `tool` in effect drags: the selection's bounds where its pixels float
+/// (`move_ui`), else the union of the selected layers' bounds.
+fn moving_rect(app: &PhotocraftApp, tool: Tool) -> Option<[f64; 4]> {
let st = app.session.active()?;
- if crate::move_ui::moves_selected_pixels(app) {
+ if crate::move_ui::moves_selected_pixels_with(app, tool) {
return selection_rect(app);
}
union(st.selected_layers().into_iter().filter_map(|id| layer_rect(&st.doc, id)))
@@ -184,7 +184,7 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
Some((Gesture::Guide, Vec::new()))
} else if tool == Tool::Move || moves_layer {
let exclude = app.session.active().map(|s| s.selected_layers()).unwrap_or_default();
- moving_rect(app).map(|rect| (Gesture::Move { rect }, exclude))
+ moving_rect(app, tool).map(|rect| (Gesture::Move { rect }, exclude))
} else if tool == Tool::Crop
&& (crate::crop_mode::classic(app) || app.crop.default_frame)
&& let Some(rect) = app.ui.crop_rect.filter(|r| crate::crop_ui::angle(app) == 0.0 && crate::crop_ui::hit(*r, p, tol) == crate::crop_ui::Hit::Inside)