Skip to content

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

Move: with an unlinked layer mask targeted, the Move tool moves the mask alone (#3604) (#3611)

Valentin Klein9f49cae0934a
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 5fd7e5c97..a85ff75b7 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -608,7 +608,8 @@ pub(crate) fn mask_target_enabled(s: &Session, id: &str, p: &Value) -> Option<st
/// (filters, adjustments, paint and fill commands); the shell adds its mask target to these.
pub fn follows_target(id: &str) -> bool {
// Moving the selected pixels (⌘-drag, Move tool) moves the targeted mask's, not the layer's.
- routed(id) || id == "select.float"
+ // Moving a layer with its unlinked mask targeted moves the mask alone (`layer.translate`).
+ routed(id) || id == "select.float" || id == "layer.translate"
}
/// Commands whose target follows the Channels panel.
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index f12e017c4..2ce0b3762 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -878,7 +878,7 @@ fn build() -> Vec<CommandSpec> {
"Move Layer",
[],
None,
- r##"{"layer":id?,"dx":i32,"dy":i32} (no layer: every selected layer; linked layers follow)"##,
+ r##"{"layer":id?,"dx":i32,"dy":i32,"target":"pixels"|"mask"="pixels"} (no layer: every selected layer; linked layers follow; target mask: a layer whose mask is unlinked moves that mask alone, a linked mask moves with the layer)"##,
has_layer,
crate::layer_multi_cmds::translate
),
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index 31dc49d09..7f3cc0961 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -283,7 +283,14 @@ pub(crate) fn move_layers(doc: &mut Document, moves: &[(LayerId, i32, i32)]) ->
Ok(())
}
+/// Does a move aimed at the layer mask (`"target":"mask"`) move `l`'s mask alone? Only an
+/// unlinked mask does (the chain off); a linked one moves with its layer, as in Photoshop.
+pub fn moves_mask_alone(l: &Layer) -> bool {
+ l.mask.as_ref().is_some_and(|m| !m.linked)
+}
+
/// `layer.translate`: the explicit layer, or every selected layer, plus their linked layers.
+/// With `"target":"mask"` a layer whose mask is unlinked moves that mask alone.
pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
let dx = int_i32("layer.translate", p, "dx")?.unwrap_or(0);
let dy = int_i32("layer.translate", p, "dy")?.unwrap_or(0);
@@ -297,14 +304,27 @@ pub fn translate(s: &mut Session, p: &Value) -> Result<Value> {
if roots.is_empty() {
return Err(EngineError::Other("no active layer".into()));
}
+ let mask_target = p.get("target").and_then(Value::as_str) == Some("mask");
let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let ids = s.edit("Move", |doc, _| {
- let ids = move_targets(doc, &roots);
- if ids.is_empty() {
+ let (alone, rest): (Vec<LayerId>, Vec<LayerId>) = roots.iter().partition(|id| mask_target && doc.layer(**id).is_some_and(moves_mask_alone));
+ let mut ids = move_targets(doc, &rest);
+ if ids.is_empty() && alone.is_empty() {
return Err(EngineError::NoLayer(roots[0]));
}
let moves: Vec<_> = ids.iter().map(|&id| (id, dx, dy)).collect();
move_layers(doc, &moves)?;
+ for id in alone {
+ let locks = doc.effective_locks(id);
+ let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
+ if locks.position || locks.all {
+ return Err(EngineError::Other(format!("layer \"{}\" is position-locked", l.name)));
+ }
+ if let Some(m) = &mut l.mask {
+ m.surface = shift_surface(&m.surface, dx, dy);
+ }
+ ids.push(id);
+ }
// A board moved past any canvas edge grows the canvas, as a new one does (#1531, #3070).
if ids.iter().any(|id| doc.layer(*id).is_some_and(|l| l.artboard().is_some())) {
crate::artboard_cmds::fit_canvas(doc);
@@ -326,6 +346,11 @@ pub fn move_targets(doc: &Document, roots: &[LayerId]) -> Vec<LayerId> {
/// shift as they are instead of re-rendering (the commit re-renders them), so a drag frame costs
/// copying the moving pixels only.
pub fn moved(doc: &Document, ids: &[LayerId], dx: i32, dy: i32) -> Result<Document> {
+ moved_target(doc, ids, dx, dy, false)
+}
+
+/// [`moved`] for a drag aimed at the layer mask (`mask_target`): unlinked masks move alone.
+pub fn moved_target(doc: &Document, ids: &[LayerId], dx: i32, dy: i32, mask_target: bool) -> Result<Document> {
let mut out = doc.clone();
for &id in ids {
let locks = doc.effective_locks(id);
@@ -333,6 +358,12 @@ pub fn moved(doc: &Document, ids: &[LayerId], dx: i32, dy: i32) -> Result<Docume
if locks.position || locks.all {
return Err(EngineError::Other(format!("layer \"{}\" is position-locked", l.name)));
}
+ if mask_target && moves_mask_alone(l) {
+ if let Some(m) = &mut l.mask {
+ m.surface = shift_surface(&m.surface, dx, dy);
+ }
+ continue;
+ }
shift_shown(doc, l, dx, dy);
}
Ok(out)
@@ -1443,6 +1474,59 @@ mod tests {
s.active().unwrap().last_damage
}
+ /// A mask with its chain off stays put while the layer moves; a linked one moves along.
+ #[test]
+ fn translate_leaves_an_unlinked_mask_in_place() {
+ for depth in [8, 16] {
+ let mut s = session(depth);
+ let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20));
+ s.execute("layer.select", json!({"layer": a.0})).unwrap();
+ s.execute("layer.layerMask.hideAll", json!({})).unwrap();
+ s.edit("mask", |doc, _| {
+ doc.layer_mut(a).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(12, 12, 18, 18), &[1.0]);
+ Ok(())
+ })
+ .unwrap();
+ let mval = |s: &Session, x, y| doc(s).layer(a).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0);
+ s.execute("layer.layerMask.linked", json!({"linked": false})).unwrap();
+ s.execute("layer.translate", json!({"dx": 30, "dy": 0})).unwrap();
+ assert!(mval(&s, 14, 14) > 0.9, "{depth}-bit: unlinked mask stays");
+ assert!(mval(&s, 44, 14) < 0.1);
+ s.execute("edit.undo", json!({})).unwrap();
+ s.execute("layer.layerMask.linked", json!({"linked": true})).unwrap();
+ s.execute("layer.translate", json!({"dx": 30, "dy": 0})).unwrap();
+ assert!(mval(&s, 44, 14) > 0.9, "{depth}-bit: linked mask moves");
+ }
+ }
+
+ /// Aimed at the mask, a move shifts an unlinked mask alone and leaves a linked one with its layer.
+ #[test]
+ fn translate_with_the_mask_targeted_moves_an_unlinked_mask_alone() {
+ for depth in [8, 16] {
+ let mut s = session(depth);
+ let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20));
+ s.execute("layer.select", json!({"layer": a.0})).unwrap();
+ s.execute("layer.layerMask.hideAll", json!({})).unwrap();
+ s.edit("mask", |doc, _| {
+ doc.layer_mut(a).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(12, 12, 18, 18), &[1.0]);
+ Ok(())
+ })
+ .unwrap();
+ let mval = |s: &Session, x, y| doc(s).layer(a).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0);
+ // Linked: the layer and its mask move together.
+ s.execute("layer.translate", json!({"dx": 30, "dy": 0, "target": "mask"})).unwrap();
+ assert_eq!(bounds(&s, a), Rect::new(40, 10, 50, 20), "{depth}-bit");
+ assert!(mval(&s, 44, 14) > 0.9);
+ s.execute("layer.layerMask.linked", json!({"linked": false})).unwrap();
+ s.execute("layer.translate", json!({"dx": -30, "dy": 0, "target": "mask"})).unwrap();
+ assert_eq!(bounds(&s, a), Rect::new(40, 10, 50, 20), "{depth}-bit: the layer stays");
+ assert!(mval(&s, 14, 14) > 0.9, "the mask moved");
+ assert!(mval(&s, 44, 14) < 0.1);
+ s.execute("edit.undo", json!({})).unwrap();
+ assert!(mval(&s, 44, 14) > 0.9, "undo puts the mask back");
+ }
+ }
+
#[test]
fn moves_and_layer_props_report_only_their_area() {
let mut s = session(8);
diff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rs
index c99c23038..5c2f2e6c6 100644
--- a/crates/ui-egui/src/move_ui.rs
+++ b/crates/ui-egui/src/move_ui.rs
@@ -114,6 +114,7 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Do
app.move_preview = None;
return None;
};
+ let mask_target = app.layer_mask_targeted();
let st = app.session.documents().get(idx)?;
let (doc_id, revision, doc) = (st.doc.id, st.revision, st.doc.clone());
let fresh = app.move_preview.as_ref().is_some_and(|p| p.doc == doc_id && p.revision == revision);
@@ -134,7 +135,7 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Do
}
if p.offsets.last() != Some(&offset) {
let t0 = crate::gpu_canvas::now_ms();
- match photocraft_engine::layer_multi_cmds::moved(&doc, &p.ids, offset.0, offset.1) {
+ match photocraft_engine::layer_multi_cmds::moved_target(&doc, &p.ids, offset.0, offset.1, mask_target) {
Ok(d) => {
// Duotone documents display through their inks.
let d = photocraft_engine::mode_cmds::display_document(&d).unwrap_or(d);
@@ -298,6 +299,38 @@ mod tests {
assert_eq!(r, Rect::new(0, 0, 64, 64));
}
+ /// Dragging with an unlinked mask targeted moves the mask alone, shown live and committed;
+ /// with the mask linked (or the pixels targeted) the layer moves.
+ #[test]
+ fn dragging_with_an_unlinked_mask_targeted_moves_the_mask_alone() {
+ for (linked, mask_target) in [(false, true), (false, false), (true, true)] {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap();
+ app.sync_views();
+ app.session.execute("layer.new.layer", json!({})).unwrap();
+ app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap();
+ app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.session.execute("select.deselect", json!({})).unwrap();
+ app.session.execute("layer.layerMask.revealAll", json!({})).unwrap();
+ app.session.execute("layer.layerMask.linked", json!({"linked": linked})).unwrap();
+ app.ui.mask_target = mask_target;
+ app.ui.tool = Tool::Move;
+ app.ui.extras.snap = false;
+ app.ui.view.show.smart_guides = false;
+ let m = egui::Modifiers::NONE;
+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, m);
+ crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 36.0, y: 16.0, pressure: 1.0 }, m);
+ let shown = super::display_doc(&mut app, 0).unwrap().0;
+ let id = app.session.active().unwrap().active_layer.unwrap();
+ let layer_moved = |d: &photocraft_doc::Document| d.layer(id).and_then(|l| l.surface()).is_some_and(|s| s.pixel(28, 16)[3] > 0.5);
+ let alone = !linked && mask_target;
+ assert_eq!(layer_moved(&shown), !alone, "preview, linked {linked} mask target {mask_target}");
+ crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 36.0, y: 16.0 }, m);
+ let d = &app.session.active().unwrap().doc;
+ assert_eq!(layer_moved(d), !alone, "commit, linked {linked} mask target {mask_target}");
+ }
+ }
+
/// Mid-drag on a painted layer: is the layer shown at the pointer?
fn live(outline: bool) -> bool {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());