Files
photocraft/crates/engine/src/layer_copy_cmds.rs
T
20fb1903dd Drag layers onto another document's tab to copy them there (#589) (#851)
* Drag layers onto another document's tab to copy them there (#589)

New engine command layer.copyToDocument copies layers into another
open document as one history step there, converting them to the
destination's colour profile and bit depth, and places them at the
same canvas position, centred, on a point, or at an offset.

In the UI, dragging layers from the Layers panel or with the Move tool
onto another document's tab shows that document (spring-loaded tabs),
and releasing over its canvas drops the copies under the pointer;
Shift keeps the position for same-size documents and centres them
otherwise, as in Photoshop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Tab labels: keep #832's full-title label on Photoshop-style tabs; the layer_transfer tests find a tab by it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
2026-10-07 23:37:42 -04:00

179 lines
7.8 KiB
Rust

//! Copy layers into another open document (#589): what Photoshop does when layers are dragged from
//! the Layers panel, or with the Move tool from the canvas, onto another document's tab or window.
//! Photoshop has no menu item for it; `layer.copyToDocument` is the command the drag dispatches and
//! agents call.
//!
//! The copies keep their names, content, masks, styles and blending; they land above the
//! destination's active layer as one history step there, and the destination becomes the active
//! document. Pixels are converted to the destination's colour profile (Color Settings' intent and
//! black point compensation) and bit depth, as Photoshop converts dragged layers.
use photocraft_color::ColorMode;
use photocraft_doc::{Document, LayerId};
use photocraft_geom::Rect;
use serde_json::{Value, json};
use crate::commands::CommandSpec;
use crate::{EngineError, Result, Session};
const CMD: &str = "layer.copyToDocument";
fn bad(msg: impl Into<String>) -> EngineError {
EngineError::BadParams { cmd: CMD.into(), msg: msg.into() }
}
fn enabled(s: &Session) -> std::result::Result<(), String> {
let d = s.active().ok_or("no document open")?;
d.active_layer.ok_or("no active layer")?;
if s.documents().len() < 2 { Err("no other document is open".into()) } else { Ok(()) }
}
/// A document index param (`None` when absent), checked against the open documents.
fn doc_index(s: &Session, p: &Value, key: &str) -> Result<Option<usize>> {
let Some(v) = p.get(key) else { return Ok(None) };
let i = v.as_u64().and_then(|i| usize::try_from(i).ok()).ok_or_else(|| bad(format!("`{key}` must be a document index")))?;
if i >= s.documents().len() {
return Err(bad(format!("no document {i} (there are {})", s.documents().len())));
}
Ok(Some(i))
}
/// An `[x, y]` param of finite numbers within ±10⁷ pixels.
fn point(p: &Value, key: &str) -> Result<Option<[f64; 2]>> {
let Some(v) = p.get(key) else { return Ok(None) };
let xy = v.as_array().filter(|a| a.len() == 2).and_then(|a| Some([a[0].as_f64()?, a[1].as_f64()?]));
match xy {
Some(xy) if xy.iter().all(|c| c.is_finite() && c.abs() <= 1e7) => Ok(Some(xy)),
_ => Err(bad(format!("`{key}` must be [x, y] (pixels)"))),
}
}
/// Modes whose layers convert through the colour engine.
fn layered(mode: ColorMode) -> bool {
matches!(mode, ColorMode::Rgb | ColorMode::Grayscale | ColorMode::Cmyk | ColorMode::Lab)
}
fn center(r: Rect) -> [f64; 2] {
[(f64::from(r.x0) + f64::from(r.x1)) / 2.0, (f64::from(r.y0) + f64::from(r.y1)) / 2.0]
}
/// `layer.copyToDocument`.
fn copy_to_document(s: &mut Session, p: &Value) -> Result<Value> {
let dest = doc_index(s, p, "document")?.ok_or_else(|| bad("missing `document` (the destination's index)"))?;
let src = match doc_index(s, p, "source")? {
Some(i) => i,
None => s.active_index().ok_or(EngineError::NoDocument)?,
};
if src == dest {
return Err(bad("the destination is the source document (use layer.duplicate)"));
}
let (from, to) = (&s.documents()[src], &s.documents()[dest]);
let (sdoc, ddoc) = (from.doc.clone(), to.doc.clone());
let ids: Vec<LayerId> = match p.get("layers") {
None => from.selected_layers(),
Some(v) => v
.as_array()
.map(|a| a.iter().map(|id| id.as_u64().map(LayerId)).collect::<Option<Vec<_>>>())
.and_then(|ids| ids)
.ok_or_else(|| bad("`layers` must be an array of layer ids"))?,
};
if let Some(missing) = ids.iter().find(|id| sdoc.layer(**id).is_none()) {
return Err(EngineError::NoLayer(*missing));
}
let ids = crate::layer_multi_cmds::top_level(&sdoc, &ids);
if ids.is_empty() {
return Err(bad("no layers to copy"));
}
for (doc, role) in [(&sdoc, "source"), (&ddoc, "destination")] {
if !layered(doc.mode) {
return Err(EngineError::Other(format!("layers can't be copied with a {:?} {role} document", doc.mode)));
}
}
// The copies, bottom to top, in a scratch document of the source's colour so they convert
// the way a whole document does.
let mut copies = Document::new("", sdoc.size, sdoc.mode, sdoc.depth);
copies.icc_profile = sdoc.icc_profile.clone();
// Type re-lays out at the source's resolution, so it keeps its size in pixels.
copies.resolution_dpi = sdoc.resolution_dpi;
for id in &ids {
let src_layer = sdoc.layer(*id).ok_or(EngineError::NoLayer(*id))?;
let mut l = src_layer.duplicate();
// A dragged Background arrives as an ordinary layer (the destination keeps its own).
if crate::extra_cmds::is_background(src_layer) && sdoc.layers.first().is_some_and(|b| b.id == *id) {
l.name = ddoc.next_layer_name("Layer");
l.locks = Default::default();
}
copies.layers.push(l);
}
if (copies.mode, &copies.icc_profile) != (ddoc.mode, &ddoc.icc_profile) {
let profile = crate::color_cmds::document_profile(&ddoc);
crate::color_cmds::convert_document(&mut copies, &profile, s.color.settings.intent(), s.color.settings.bpc)?;
}
if copies.depth != ddoc.depth {
crate::image_cmds::for_each_surface(&mut copies.layers, true, &mut |surf, _| {
let f = surf.format().with_sample(ddoc.depth);
*surf = surf.convert(f);
});
}
// Placement: centred on the destination's canvas, centred on a point, or offset from where
// the layers are in the source (default: the same canvas position).
let bounds = copies.layers.iter().filter_map(crate::layer_multi_cmds::layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b));
let content = if bounds.is_empty() { sdoc.bounds() } else { bounds };
let (dx, dy) = if p.get("center").and_then(Value::as_bool).unwrap_or(false) {
let (c, d) = (center(content), center(ddoc.bounds()));
(d[0] - c[0], d[1] - c[1])
} else if let Some(at) = point(p, "at")? {
let c = center(content);
(at[0] - c[0], at[1] - c[1])
} else {
point(p, "offset")?.map_or((0.0, 0.0), |o| (o[0], o[1]))
};
let (dx, dy) = (dx.round() as i32, dy.round() as i32);
if dx != 0 || dy != 0 {
let snapshot = copies.clone();
for l in &mut copies.layers {
crate::commands::translate_layer(&snapshot, l, dx, dy);
crate::vector_cmds::translate_vectors(&snapshot, l, f64::from(dx), f64::from(dy));
}
}
let patterns: Vec<_> = sdoc.patterns.iter().filter(|pat| !ddoc.patterns.iter().any(|d| d.id == pat.id)).cloned().collect();
s.set_active(dest);
let label = if ids.len() == 1 { "Duplicate Layer" } else { "Duplicate Layers" };
let edited = s.edit(label, |doc, active| {
doc.patterns.extend(patterns);
let mut above = *active;
let mut new = Vec::with_capacity(copies.layers.len());
for l in copies.layers {
let id = doc.insert_above(above, l);
above = Some(id);
new.push(id);
}
*active = above;
Ok(new)
});
// Nothing changed: the source stays the active document.
if edited.is_err() {
s.set_active(src);
}
let new = edited?;
let last = new.last().copied();
crate::layer_multi_cmds::reselect(s, new.clone(), last);
Ok(json!({"document": dest, "layers": new.iter().map(|l| l.0).collect::<Vec<_>>(), "offset": [dx, dy]}))
}
pub fn specs() -> Vec<CommandSpec> {
vec![CommandSpec {
id: CMD,
label: "Copy Layers to Document",
menu: &[],
shortcut: None,
params: r##"{"document":index (destination),"layers":[id,…]?=the source's selected layers,"source":index?=active,"center":bool=false (centre on the destination's canvas),"at":[x,y]? (centre the copies on this point),"offset":[dx,dy]?=[0,0] (from their place in the source)}"##,
enabled,
run: copy_to_document,
journal: true,
}]
}
#[cfg(test)]
mod tests;