From bfd9a2f0c5c5e2247e727ab968a48cdee91ca41f Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:46:40 +0100 Subject: [PATCH] Image > Duplicate: the copy starts without a selection (#14593) The source keeps its selection; alpha channels, guides and paths are still copied, for a full and a merged-layers-only copy. Fixes #14593 --- crates/engine/src/cmd/image_cmds.rs | 7 +++ .../image_cmds/duplicate_selection_tests.rs | 63 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 crates/engine/src/cmd/image_cmds/duplicate_selection_tests.rs diff --git a/crates/engine/src/cmd/image_cmds.rs b/crates/engine/src/cmd/image_cmds.rs index 318b5dbad2..65c8385a56 100644 --- a/crates/engine/src/cmd/image_cmds.rs +++ b/crates/engine/src/cmd/image_cmds.rs @@ -1186,6 +1186,9 @@ pub(crate) fn change_depth(doc: &mut Document, depth: SampleType) { fn duplicate(s: &mut Session, p: &Value) -> Result { let d = s.active().ok_or(EngineError::NoDocument)?; let mut doc = (*d.doc).clone(); + // As Photoshop: the copy starts without a selection (the source keeps its own); alpha + // channels, guides and paths are copied. + doc.selection = None; doc.id = photocraft_doc::DocId::fresh(); doc.name = p.get("name").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| format!("{} copy", d.doc.name)); if p.get("mergedOnly").and_then(Value::as_bool).unwrap_or(false) { @@ -1297,6 +1300,10 @@ pub fn specs() -> Vec { ] } +#[cfg(test)] +#[path = "image_cmds/duplicate_selection_tests.rs"] +mod duplicate_selection_tests; + #[cfg(test)] mod tests; diff --git a/crates/engine/src/cmd/image_cmds/duplicate_selection_tests.rs b/crates/engine/src/cmd/image_cmds/duplicate_selection_tests.rs new file mode 100644 index 0000000000..49aff37d7b --- /dev/null +++ b/crates/engine/src/cmd/image_cmds/duplicate_selection_tests.rs @@ -0,0 +1,63 @@ +//! #14593: Image › Duplicate leaves the copy without a selection, as the reference application +//! does, for a full copy and a merged-layers-only copy. The source keeps its selection, and the +//! copy keeps the source's alpha channels, guides and paths. + +use photocraft_doc::Document; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::Session; + +/// A source at `depth` with the issue's selection (10,10-110,60), one alpha channel saved from +/// it, a guide each way and a saved path. +fn source(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 200, "height": 100, "depth": depth, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Paint"})).unwrap(); + s.execute("select.rect", json!({"x": 10, "y": 10, "width": 100, "height": 50})).unwrap(); + s.execute("select.saveSelection", json!({"name": "Alpha 1"})).unwrap(); + s.execute("view.newGuide", json!({"orientation": "vertical", "position": 40})).unwrap(); + s.execute("view.newGuide", json!({"orientation": "horizontal", "position": 20})).unwrap(); + s.execute("path.set", json!({"name": "Saved", "path": {"subpaths": [{"knots": [[2, 2], [12, 2], [12, 8]]}]}})).unwrap(); + s +} + +fn doc(s: &Session, index: usize) -> &Document { + &s.documents()[index].doc +} + +fn selection(d: &Document) -> Option { + d.selection.as_ref().map(|sel| sel.content_bounds()) +} + +#[test] +fn a_duplicate_has_no_selection_and_the_source_keeps_its_own() { + for depth in [8, 16, 32] { + for merged in [false, true] { + let mut s = source(depth); + let before = selection(doc(&s, 0)); + assert_eq!(before, Some(Rect::from_xywh(10, 10, 100, 50)), "{depth}-bit source has the selection"); + let r = s.execute("image.duplicate", json!({"mergedOnly": merged})).unwrap(); + assert_eq!(r["document"], json!(1)); + assert_eq!(s.active_index(), Some(1), "the copy is active"); + let copy = doc(&s, 1); + assert!(copy.selection.is_none(), "{depth}-bit copy (mergedOnly {merged}) has no selection"); + assert_eq!(selection(doc(&s, 0)), before, "{depth}-bit source (mergedOnly {merged}) keeps its selection"); + } + } +} + +#[test] +fn a_duplicate_keeps_alpha_channels_guides_and_paths() { + for merged in [false, true] { + let mut s = source(8); + s.execute("image.duplicate", json!({"mergedOnly": merged})).unwrap(); + let (src, copy) = (doc(&s, 0), doc(&s, 1)); + assert_eq!(copy.channels.len(), 1, "mergedOnly {merged}: the alpha channel is copied"); + assert_eq!(copy.channels[0].name, src.channels[0].name); + assert_eq!(copy.guides.vertical, vec![40.0], "mergedOnly {merged}: vertical guide copied"); + assert_eq!(copy.guides.horizontal, vec![20.0], "mergedOnly {merged}: horizontal guide copied"); + assert_eq!(copy.paths.len(), src.paths.len(), "mergedOnly {merged}: paths copied"); + assert!(copy.paths.iter().any(|p| p.name == "Saved"), "mergedOnly {merged}: the saved path is copied"); + } +}