Fix #2091 copy/paste masks between layers

Edit > Copy with masks selected copied the layer, so Paste duplicated it
instead of adding the mask. Masks now go to their own clipboard and Paste
appends them, with unique names, to the selected unlocked layers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jerome CAMUS
2026-10-30 07:55:23 +01:00
co-authored by Claude Sonnet 5.5
parent fbb5488214
commit edb984a75a
6 changed files with 123 additions and 0 deletions
+14
View File
@@ -25,6 +25,7 @@ fn has_clip(s: &Session) -> std::result::Result<(), String> {
&& s.state.key_clipboard.is_empty()
&& s.state.effect_clipboard.is_empty()
&& s.state.contents_clipboard.is_empty()
&& s.state.mask_clipboard.is_empty()
&& s.state.link_clipboard.is_none()
&& s.state.value_clipboard.is_empty()
{
@@ -285,6 +286,7 @@ fn clear_clipboards(s: &mut Session) {
s.state.key_clipboard.clear();
s.state.effect_clipboard.clear();
s.state.contents_clipboard.clear();
s.state.mask_clipboard.clear();
s.state.link_clipboard = None;
s.state.value_clipboard.clear();
s.state.keyframe_text = None;
@@ -311,6 +313,15 @@ fn copy(s: &mut Session, p: &Value) -> Result<Value> {
if p.get("layers").is_none() && !super::effect::selected_effects(s).is_empty() {
return s.execute("effect.copy", json!({}));
}
// Masks selected → copy them (not their layer), for Paste onto other layers (#2091).
if p.get("layers").is_none() {
let masks = super::prop_groups::copy_masks(s);
if !masks.is_empty() {
clear_clipboards(s);
s.state.mask_clipboard = masks;
return Ok(json!({"masks": s.state.mask_clipboard.len()}));
}
}
// Shape items selected in a shape layer's Contents → copy them (not their layer).
if p.get("layers").is_none() {
let items = super::prop_groups::copy_contents(s);
@@ -597,6 +608,9 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
if !s.state.contents_clipboard.is_empty() {
return super::prop_groups::paste_contents(s, p);
}
if !s.state.mask_clipboard.is_empty() {
return super::prop_groups::paste_masks(s, p);
}
let cid = super::comp_id(s, p)?;
let clip = s.state.clipboard.clone();
let new = s.edit("Paste", None, |proj, st| {
+1
View File
@@ -851,6 +851,7 @@ fn copy(s: &mut Session, p: &Value) -> Result<Value> {
s.state.key_clipboard.clear();
s.state.keyframe_text = None;
s.state.contents_clipboard.clear();
s.state.mask_clipboard.clear();
s.state.link_clipboard = None;
s.state.clip_is_keys = false;
let n = groups.len();
+53
View File
@@ -135,6 +135,59 @@ pub(crate) fn copy_contents(s: &Session) -> Vec<PropGroup> {
selected_contents(s).into_iter().filter_map(|(lid, uid)| c.layer(lid)?.props.find_group(uid).cloned()).collect()
}
/// Masks selected on the active comp's layers, top to bottom (#2091).
pub(crate) fn selected_masks(s: &Session) -> Vec<(LayerId, Uid)> {
let Some(c) = s.active_comp().filter(|_| !s.state.selected_props.is_empty()) else { return vec![] };
c.layers
.iter()
.flat_map(|l| {
let masks = l.masks().into_iter().flat_map(|m| m.groups());
masks.filter(|m| s.state.selected_props.contains(&(l.id, m.uid))).map(|m| (l.id, m.uid))
})
.collect()
}
/// Copies of the selected masks (Edit ▸ Copy), top to bottom.
pub(crate) fn copy_masks(s: &Session) -> Vec<PropGroup> {
let Some(c) = s.active_comp() else { return vec![] };
selected_masks(s).into_iter().filter_map(|(lid, uid)| c.layer(lid)?.props.find_group(uid).cloned()).collect()
}
/// Edit ▸ Paste with masks on the clipboard: each is added, with a unique name, below the masks of
/// every selected unlocked layer that can have masks, and the copies are selected (#2091).
pub(crate) fn paste_masks(s: &mut Session, p: &Value) -> Result<Value> {
let c = "edit.paste";
let clip = s.state.mask_clipboard.clone();
let (cid, ids) = layers_p(s, p)?;
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?;
let targets: Vec<LayerId> = ids.into_iter().filter(|l| comp.layer(*l).is_some_and(|l| l.masks().is_some() && !l.switches.locked)).collect();
if targets.is_empty() {
return Err(bad(c, "select an unlocked layer that can have masks to paste masks into"));
}
let label = match clip.as_slice() {
[g] => format!("Paste {}", g.name),
_ => "Paste".to_string(),
};
let pasted = s.edit(&label, None, |proj, st| {
let mut next = proj.next_id;
let mut out = vec![];
for lid in &targets {
let masks = layer_mut(proj, cid, *lid)?.props.sub_mut("masks").ok_or_else(|| bad(c, "the layer can't have masks"))?;
for g in &clip {
let mut g = g.clone();
g.reassign_uids(&mut next);
g.name = super::effect::unique_name(masks, &g.name);
out.push((*lid, g.uid));
masks.children.push(g.into());
}
}
proj.next_id = next + 1;
st.selected_props = out.clone();
Ok(out)
})?;
Ok(json!({"masks": pasted.iter().map(|(_, u)| *u).collect::<Vec<_>>()}))
}
/// Edit ▸ Cut's removal of the selected shape items (once copied).
pub(crate) fn cut_contents(s: &mut Session, items: &[(LayerId, Uid)]) -> Result<Value> {
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;