From f5ee04545a00e05f1b5c201e7fd022fec4471c95 Mon Sep 17 00:00:00 2001 From: x-labb <154824493+x-labb@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:29:40 +0100 Subject: [PATCH] fix(engine): Auto-Blend Layers is unavailable with a type layer or smart object selected (#14204) Auto-Blend collected only the selected pixel layers, so with a type layer or a smart object in the selection it blended the pixel layers and ignored the rest. The reference application refuses: the command is not available and no history state is added. Gives Auto-Blend its own enabled check (a type layer or smart object among the selected layers makes it unavailable) and the same guard at the start of the run. Auto-Align keeps its check. --- crates/engine-vector/src/cmd/align_cmds.rs | 27 ++++++- .../engine/tests/it/auto_blend_layer_kinds.rs | 74 +++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 crates/engine/tests/it/auto_blend_layer_kinds.rs diff --git a/crates/engine-vector/src/cmd/align_cmds.rs b/crates/engine-vector/src/cmd/align_cmds.rs index 8e283e5393..c93641ad59 100644 --- a/crates/engine-vector/src/cmd/align_cmds.rs +++ b/crates/engine-vector/src/cmd/align_cmds.rs @@ -57,6 +57,24 @@ fn two_layers(s: &Session) -> std::result::Result<(), String> { if selected_pixel_layers(s).len() < 2 { Err("select two or more pixel layers in the Layers panel".into()) } else { Ok(()) } } +/// Auto-Blend's message when a type layer or a smart object is among the selected layers (#14204). +const BLEND_UNAVAILABLE: &str = "The command \"Auto-Blend Layers\" is not currently available."; + +/// Whether a type layer or a smart object is among the selected layers. Auto-Blend is unavailable +/// then, instead of quietly blending only the pixel layers; shape, adjustment and group layers in +/// the selection were not measured and keep today's behaviour. +fn has_type_or_smart_object(s: &Session) -> bool { + let Some(st) = s.active() else { return false }; + st.selected_layers().iter().any(|id| st.doc.layer(*id).is_some_and(|l| matches!(l.content, LayerContent::Text(_) | LayerContent::Smart(_)))) +} + +fn blend_enabled(s: &Session) -> std::result::Result<(), String> { + if has_type_or_smart_object(s) { + return Err(BLEND_UNAVAILABLE.into()); + } + two_layers(s) +} + fn auto_align(s: &mut Session, p: &Value) -> Result { let cmd = "edit.autoAlignLayers"; let ids = selected_pixel_layers(s); @@ -184,6 +202,9 @@ const BLEND_BUFFERS: &str = "Auto-Blend Layers"; fn auto_blend(s: &mut Session, p: &Value) -> Result { let cmd = "edit.autoBlendLayers"; + if has_type_or_smart_object(s) { + return Err(EngineError::Other(BLEND_UNAVAILABLE.into())); + } let ids = selected_pixel_layers(s); if ids.len() < 2 { return Err(bad(cmd, "select two or more pixel layers")); @@ -372,8 +393,8 @@ fn auto_blend(s: &mut Session, p: &Value) -> Result { } macro_rules! spec { - ($id:literal, $label:literal, $params:literal, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &["Edit"], shortcut: None, params: $params, enabled: two_layers, run: $run, journal: true } + ($id:literal, $label:literal, $params:literal, $enabled:expr, $run:expr) => { + CommandSpec { id: $id, label: $label, menu: &["Edit"], shortcut: None, params: $params, enabled: $enabled, run: $run, journal: true } }; } @@ -383,12 +404,14 @@ pub fn specs() -> Vec { "edit.autoAlignLayers", "Auto-Align Layers…", r##"{"projection":"auto|perspective|cylindrical|spherical|collage|reposition","reference":layer id?=the locked selected layer, else the bottom selected layer,"geometricCorrection":bool=false,"interpolation":"bicubic|bilinear|nearest","registration":"auto|features|intensity"}"##, + two_layers, auto_align ), spec!( "edit.autoBlendLayers", "Auto-Blend Layers…", r##"{"method":"panorama|stack","seamlessTones":bool=true,"haloControl":0..8=2,"depthMap":bool=false}"##, + blend_enabled, auto_blend ), ] diff --git a/crates/engine/tests/it/auto_blend_layer_kinds.rs b/crates/engine/tests/it/auto_blend_layer_kinds.rs new file mode 100644 index 0000000000..9b39b1ff5d --- /dev/null +++ b/crates/engine/tests/it/auto_blend_layer_kinds.rs @@ -0,0 +1,74 @@ +//! #14204: Auto-Blend Layers is unavailable when a type layer or a smart object is among the +//! selected layers, instead of quietly blending only the pixel layers; a run is refused too and +//! changes nothing. Pixel layers alone still blend; Auto-Align's enabled state is unchanged. + +use photocraft_doc::{Document, LayerId}; +use photocraft_engine::Session; +use serde_json::json; + +const BLEND_UNAVAILABLE: &str = "The command \"Auto-Blend Layers\" is not currently available."; + +/// A document with two textured pixel layers, `a` and `b`, both selected. +fn two_pixel_layers() -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 300, "height": 240, "depth": 8, "background": "transparent"})).unwrap(); + let mut ids = Vec::new(); + for (name, dx) in [("A", 20), ("B", 31)] { + s.execute("layer.new.layer", json!({"name": name})).unwrap(); + for i in 0..40 { + let (x, y) = (dx + (i * 37) % 200, 20 + (i * 53) % 150); + s.execute("select.rect", json!({"x": x, "y": y, "width": 9, "height": 7})).unwrap(); + s.execute("edit.fill", json!({"color": format!("#{:02x}{:02x}{:02x}", i * 6 % 256, 255 - i * 5 % 256, i * 11 % 256)})).unwrap(); + } + s.execute("select.deselect", json!({})).unwrap(); + ids.push(s.active().unwrap().active_layer.unwrap()); + } + s.execute("layer.select", json!({"layers": [ids[0].0, ids[1].0]})).unwrap(); + (s, ids[0], ids[1]) +} + +/// The document and History, to see that a refused run changed nothing. +fn snapshot(s: &Session) -> (std::sync::Arc, usize) { + let st = s.active().unwrap(); + (st.doc.clone(), st.history.past_len()) +} + +fn assert_unavailable(s: &mut Session) { + assert!(!s.is_enabled("edit.autoBlendLayers"), "the menu item is disabled"); + let (doc, steps) = snapshot(s); + let err = s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap_err().to_string(); + assert!(err.ends_with(BLEND_UNAVAILABLE), "{err}"); + assert!(std::sync::Arc::ptr_eq(&doc, &s.active().unwrap().doc), "document unchanged"); + assert_eq!(s.active().unwrap().history.past_len(), steps, "no history state"); +} + +#[test] +fn pixel_layers_alone_still_blend() { + let (mut s, ..) = two_pixel_layers(); + assert!(s.is_enabled("edit.autoBlendLayers")); + s.execute("edit.autoBlendLayers", json!({"method": "panorama"})).unwrap(); +} + +#[test] +fn a_type_layer_among_the_selected_layers() { + let (mut s, a, b) = two_pixel_layers(); + let text = s.execute("type.create", json!({"x": 2, "y": 20, "text": "I", "size": 12})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.select", json!({"layers": [a.0, b.0, text]})).unwrap(); + assert_unavailable(&mut s); +} + +#[test] +fn a_smart_object_among_the_selected_layers() { + let (mut s, a, b) = two_pixel_layers(); + let smart = s.execute("layer.smartObjects.convertToSmartObject", json!({"layer": a.0})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.select", json!({"layers": [smart, b.0]})).unwrap(); + assert_unavailable(&mut s); +} + +#[test] +fn auto_align_keeps_its_enabled_state() { + let (mut s, a, b) = two_pixel_layers(); + let text = s.execute("type.create", json!({"x": 2, "y": 20, "text": "I", "size": 12})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.select", json!({"layers": [a.0, b.0, text]})).unwrap(); + assert!(s.is_enabled("edit.autoAlignLayers"), "Auto-Align still counts the pixel layers only"); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index dc3669251c..3feb68561a 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -33,6 +33,7 @@ mod atn_stroke_width; mod atn_type_path; mod atn_v12; mod atn_video_frames; +mod auto_blend_layer_kinds; mod average_smart; mod background_and_mask_depth; mod background_job_target;