Drag a layer mask onto another layer or group to move it, ⌥ to copy it (#10485)

In Photoshop, dragging a layer mask's thumbnail onto another layer or group moves the mask
there, and ⌥-dragging copies it. PhotoCraft's mask thumbnails could only be dragged to the
trash, so dropping one on a group did nothing.

- engine: layer.layerMask.moveTo {layer, to, copy, replace}: one history step; a layer
  that already has a mask keeps it unless `replace`; the Background becomes a layer first.
- Layers panel: a layer mask thumbnail dropped on another row (layer or group, without a
  mask of its own) moves the mask there, ⌥ copies it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-05 21:54:45 -06:00
1 parent 3d60ad01f9
commit 911aa4dede
5 files changed
+129 -2

No files matched your search

+10 -1
View File
@@ -169,7 +169,7 @@ use enabled::{can_redo, can_undo, has_doc, has_layer, has_pixel_layer, has_pixel
pub use layer_params::blend_from_str;
pub(crate) use layer_params::{NEW_LAYER_PATH, explicit_layer, layer_listing, layer_panel_option, layer_param, mask_at_depth, mask_new_layer, selection_mask};
pub(crate) use layer_references::{recorded_drop, recorded_layer};
use ops::{arrange, merge_group, set_mask, set_mask_and_deselect};
use ops::{arrange, merge_group, move_mask, set_mask, set_mask_and_deselect};
pub(crate) use ops::{layer_copy, translate_layer};
use params::{bad, set_clipped};
pub(crate) use params::{check_f32_values, cmyk_param, color_param, int, int_i32, parse_hex, u32_id, u32_id_param};
@@ -807,6 +807,15 @@ fn build() -> Vec<CommandSpec> {
"Delete Layer Mask",
None
)),
cmd!(
"layer.layerMask.moveTo",
"Move Layer Mask",
[],
None,
r##"{"layer":id? (the mask's layer, default active),"to":id (the layer or group it goes to),"copy":bool=false (⌥-drag: a copy goes and the original stays),"replace":bool=false (replace a mask already there; otherwise refused)} → {layer,to,copy}"##,
has_layer,
move_mask
),
cmd!("layer.mergeDown", "Merge Down", ["Layer"], None, r##"{"layer":id?}"##, has_layer, |s, p| {
let id = layer_param(s, p)?;
s.edit("Merge Down", |doc, active| {
+73
View File
@@ -97,6 +97,39 @@ fn set_mask_with(s: &mut Session, p: &Value, label: &str, mask: Option<LayerMask
Ok(Value::Null)
}
/// Drag a layer mask's thumbnail onto another layer or group in the Layers panel: the mask moves
/// there (⌥: a copy goes there and the original stays), as in Photoshop (#10485). A layer that
/// already has a mask keeps it unless `replace` (Photoshop asks first). One history step.
pub(super) fn move_mask(s: &mut Session, p: &Value) -> Result<Value> {
let bad = |m: &str| EngineError::BadParams { cmd: "layer.layerMask.moveTo".into(), msg: m.into() };
let from = layer_param(s, p)?;
let to = p.get("to").and_then(Value::as_u64).map(LayerId).ok_or_else(|| bad("`to` must be a layer id"))?;
let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false);
let replace = p.get("replace").and_then(Value::as_bool).unwrap_or(false);
if from == to {
return Err(bad("the mask is already on that layer"));
}
let label = if copy { "Copy Layer Mask" } else { "Move Layer Mask" };
s.edit(label, |doc, _| {
let mask = doc.layer(from).ok_or(EngineError::NoLayer(from))?.mask.clone().ok_or_else(|| bad("the layer has no layer mask"))?;
if doc.layer(to).ok_or(EngineError::NoLayer(to))?.mask.is_some() && !replace {
return Err(bad("the layer already has a layer mask (pass `replace`)"));
}
check_all_unlocked(doc, to)?;
if !copy {
check_all_unlocked(doc, from)?;
}
// The Background can't hold a mask, so it becomes a normal layer first, as for Add Mask.
crate::extra_cmds::background_to_layer_for_mask(doc, to);
doc.layer_mut(to).ok_or(EngineError::NoLayer(to))?.mask = Some(mask);
if !copy {
doc.layer_mut(from).ok_or(EngineError::NoLayer(from))?.mask = None;
}
Ok(())
})?;
Ok(json!({"layer": from.0, "to": to.0, "copy": copy}))
}
/// Move a layer's pixels, linked mask and type by whole pixels (vectors move via
/// `vector_cmds::translate_vectors`).
pub(crate) fn translate_layer(doc: &Document, l: &mut Layer, dx: i32, dy: i32) {
@@ -188,3 +221,43 @@ pub(super) fn merge_group(s: &mut Session, id: LayerId) -> Result<Value> {
})?;
Ok(json!({ "layer": id.0 }))
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::Session;
/// #10485: dragging a layer's mask onto a group moves it there, ⌥ copies it (Photoshop). A
/// layer that has a mask already keeps it unless asked to replace it; one undo puts it back.
#[test]
fn a_layer_mask_moves_or_copies_onto_another_layer_or_group() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
let b = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap();
let group = s.execute("layer.groupLayers", json!({"layer": b})).unwrap()["layer"].as_u64().unwrap();
let mask_of = |s: &Session, id: u64| s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().mask.clone();
let original = mask_of(&s, a).expect("a has a mask");
// Move onto the group.
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).unwrap();
assert!(mask_of(&s, a).is_none(), "moved away");
assert_eq!(mask_of(&s, group).unwrap().surface, original.surface, "the same mask on the group");
// One undo puts it back.
s.execute("edit.undo", json!({})).unwrap();
assert!(mask_of(&s, a).is_some() && mask_of(&s, group).is_none());
// ⌥: a copy.
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group, "copy": true})).unwrap();
assert!(mask_of(&s, a).is_some() && mask_of(&s, group).is_some());
// The group has one now: refused unless replacing.
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).is_err());
s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group, "replace": true})).unwrap();
assert!(mask_of(&s, a).is_none());
// Bad input.
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": a, "to": group})).is_err(), "no mask to move");
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": group, "to": group})).is_err(), "onto itself");
assert!(s.execute("layer.layerMask.moveTo", json!({"layer": group})).is_err(), "no target");
}
}
+16
View File
@@ -314,6 +314,22 @@ pub(super) fn fx_drop(ctx: &egui::Context, ui: &egui::Ui, l: &Layer, rect: Rect,
}
}
/// A layer mask's thumbnail dropped on another layer or group moves the mask there, ⌥ copies it,
/// as in Photoshop (#10485). A row that has a layer mask already doesn't take the drop (Photoshop
/// asks whether to replace it).
pub(super) fn mask_drop(ctx: &egui::Context, ui: &egui::Ui, l: &Layer, rect: Rect, actions: &mut Vec<(String, Value)>) {
let Some((from, MaskKind::Pixel)) = ctx.data(|d| d.get_temp::<MaskDrag>(mask_drag_key())) else { return };
let Some(p) = ctx.input(|i| i.pointer.interact_pos()) else { return };
if from == l.id.0 || l.mask.is_some() || !rect.contains(p) {
return;
}
let t = Tokens::get(ctx);
ui.painter().rect_stroke(rect.shrink(1.0), t.radius_sm, Stroke::new(2.0, t.accent), StrokeKind::Inside);
if ctx.input(|i| i.pointer.any_released()) {
actions.push(("layer.layerMask.moveTo".into(), json!({"layer": from, "to": l.id.0, "copy": ctx.input(|i| i.modifiers.alt)})));
}
}
/// Photoshop shows a layer's effects as indented sub-rows ("Effects", then each effect). The eye
/// on "Effects" shows or hides them all, the eye on an effect's row just that one (#1622).
pub(super) fn effect_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui, l: &Layer, depth: usize, actions: &mut Vec<(String, Value)>) {
+2 -1
View File
@@ -1,4 +1,4 @@
use super::layer_drag::{MaskDrag, fx_drop, layer_drag_and_drop, mask_drag_key, start_fx_drag, starts_mask_drag};
use super::layer_drag::{MaskDrag, fx_drop, layer_drag_and_drop, mask_drag_key, mask_drop, start_fx_drag, starts_mask_drag};
use super::layers::simple_lock_toggle;
use super::{
Color32, CornerRadius, Layer, LayerContent, PhotocraftApp, Pos2, Rect, Sense, Stroke, StrokeKind, Tokens, Value, icons, json, pos2, theme, vec2, widgets,
@@ -207,6 +207,7 @@ pub(super) fn layer_row(
let mask_drag = mask_drag.is_some();
layer_drag_and_drop(app, ctx, ui, l, rect, &resp, actions);
fx_drop(ctx, ui, l, rect, actions);
mask_drop(ctx, ui, l, rect, actions);
crate::smart_ui::drop_on_row(ctx, ui, l, rect, actions);
if resp.drag_started() && !mask_drag {
crate::layer_transfer::begin_from_panel(app, ctx, l.id);
@@ -869,3 +869,31 @@ fn selecting_an_adjustment_layer_from_another_layer_targets_its_mask() {
assert!(!h.state().ui.mask_target && !h.state().ui.vector_mask_target, "{command}: a pixel layer targets its contents");
}
}
/// #10485: dragging the layer mask's thumbnail onto a group's row moves the mask to the group, and
/// ⌥ copies it, as in Photoshop. It had no effect.
#[test]
fn dragging_the_mask_thumbnail_onto_a_group_moves_or_copies_it() {
for copy in [false, true] {
let (mut s, masked, shape) = session();
let group = s.execute("layer.groupLayers", json!({"layer": shape, "name": "Grp"})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.select", json!({"layer": masked})).unwrap();
let mut h = harness(s, 0, 1.0, 290.0);
let from = mask_rect(&h, masked, MaskKind::Pixel).center();
let to = h.get_by_label("Grp").rect().center();
let modifiers = if copy { Modifiers::ALT } else { Modifiers::NONE };
h.hover_at(from);
h.event(egui::Event::ModifiersChanged(modifiers));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers });
h.run_steps(1);
for k in 1..=8 {
h.event(egui::Event::PointerMoved(from + (to - from) * (k as f32 / 8.0)));
h.run_steps(1);
}
h.event(egui::Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers });
h.run_steps(3);
assert!(layer(&h, group).mask.is_some(), "copy: {copy}: the group has the mask");
assert_eq!(layer(&h, masked).mask.is_some(), copy, "copy: {copy}: the original stays only with ⌥");
}
}