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.
This commit is contained in:
x-labb committed 2026-11-24 20:40:11 +01:00
1 parent e56522780f
commit f5ee04545a
3 files changed
+100 -2

No files matched your search

+25 -2
View File
@@ -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<Value> {
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<Value> {
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<Value> {
}
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<CommandSpec> {
"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
),
]
@@ -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<Document>, 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");
}
+1
View File
@@ -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;