From 7b86f6c295b9fa5ed8b3576e0bfbca53dad061a3 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:23:47 +0100 Subject: [PATCH] Background from Layer: greyed with several layers selected (#17306) With two or more layers selected the command is disabled and a call without a layer is refused with the reason; a call that names its layer converts that layer as before. --- crates/engine-core/src/cmd/background_cmds.rs | 25 +++- crates/engine-core/src/session/execute.rs | 5 + .../it/background_from_layer_multi_select.rs | 133 ++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 4 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 crates/engine/tests/it/background_from_layer_multi_select.rs diff --git a/crates/engine-core/src/cmd/background_cmds.rs b/crates/engine-core/src/cmd/background_cmds.rs index b1fe7ce2cc6..67685db92e7 100644 --- a/crates/engine-core/src/cmd/background_cmds.rs +++ b/crates/engine-core/src/cmd/background_cmds.rs @@ -12,7 +12,10 @@ use serde_json::{Value, json}; use crate::commands::CommandSpec; use crate::{EngineError, Result, Session}; -const CMD: &str = "layer.new.backgroundFromLayer"; +pub(crate) const CMD: &str = "layer.new.backgroundFromLayer"; + +/// Why Background from Layer is greyed with two or more layers selected (#17306). +const SEVERAL_SELECTED: &str = "several layers selected; select one layer to make it the Background"; /// The document's Background layer, if it has one (always the bottom layer). pub(crate) fn background_id(doc: &Document) -> Option { @@ -41,7 +44,19 @@ fn convertible(l: &Layer) -> std::result::Result<(), String> { } } +/// The menu's precondition: greyed, as in the reference, with two or more layers selected +/// (#17306); otherwise [`can_convert_active`]. fn can_convert(s: &Session) -> std::result::Result<(), String> { + let d = s.active().ok_or("no document open")?; + if d.selected_layers().len() > 1 { + return Err(SEVERAL_SELECTED.into()); + } + can_convert_active(s) +} + +/// Can the active layer become the Background? A call that names its `"layer"` (scripts, +/// actions) is checked with this alone: it converts that layer whatever else is selected. +pub(crate) fn can_convert_active(s: &Session) -> std::result::Result<(), String> { let d = s.active().ok_or("no document open")?; if background_id(&d.doc).is_some() { return Err("the document already has a Background layer".into()); @@ -52,7 +67,13 @@ fn can_convert(s: &Session) -> std::result::Result<(), String> { fn background_from_layer(s: &mut Session, p: &Value) -> Result { let id = match p.get("layer") { - None | Some(Value::Null) => crate::commands::layer_param(s, p)?, + None | Some(Value::Null) => { + // Without a named layer the selection is the target, and it must be one layer. + if s.active().is_some_and(|d| d.selected_layers().len() > 1) { + return Err(EngineError::Disabled(CMD.into(), SEVERAL_SELECTED.into())); + } + crate::commands::layer_param(s, p)? + } Some(v) => LayerId(v.as_u64().ok_or_else(|| EngineError::BadParams { cmd: CMD.into(), msg: "`layer` must be a layer id".into() })?), }; let bg = s.tools.background; diff --git a/crates/engine-core/src/session/execute.rs b/crates/engine-core/src/session/execute.rs index 6bbecf98ff0..33dd3e5d805 100644 --- a/crates/engine-core/src/session/execute.rs +++ b/crates/engine-core/src/session/execute.rs @@ -159,6 +159,11 @@ impl Session { if let Some(why) = bitmap_mode_refusal(self, spec.id, params) { return Err(why.into()); } + // A named layer is converted whatever else is selected; the menu (no params) is greyed + // with several layers selected (#17306). + if spec.id == crate::background_cmds::CMD && params.get("layer").is_some_and(|l| !l.is_null()) { + return crate::background_cmds::can_convert_active(self); + } channel_cmds::mask_target_enabled(self, spec.id, params).unwrap_or_else(|| (spec.enabled)(self)) } diff --git a/crates/engine/tests/it/background_from_layer_multi_select.rs b/crates/engine/tests/it/background_from_layer_multi_select.rs new file mode 100644 index 00000000000..1c1bca541fd --- /dev/null +++ b/crates/engine/tests/it/background_from_layer_multi_select.rs @@ -0,0 +1,133 @@ +//! Layer › New › Background from Layer with several layers selected (#17306). +//! +//! Measured in the reference: with two layers selected the menu item is greyed. With one layer it +//! is live for pixel, type, smart object, fill and shape layers and greyed for groups and +//! adjustment layers. A script or action that names the layer (`"layer": id`) converts that +//! layer, whatever is selected. + +use photocraft_doc::LayerId; +use photocraft_engine::Session; +use serde_json::{Value, json}; + +const CMD: &str = "layer.new.backgroundFromLayer"; + +/// A document without a Background: a transparent "Layer 1" plus A and B on top. +fn no_background() -> (Session, [LayerId; 3]) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 12, "background": "transparent"})).unwrap(); + let base = s.active().unwrap().active_layer.unwrap(); + let a = new_layer(&mut s, "A"); + let b = new_layer(&mut s, "B"); + (s, [base, a, b]) +} + +fn new_layer(s: &mut Session, name: &str) -> LayerId { + LayerId(s.execute("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap()) +} + +fn ids(r: &Value) -> LayerId { + LayerId(r["layer"].as_u64().unwrap()) +} + +fn order(s: &Session) -> Vec { + s.active().unwrap().doc.layers.iter().map(|l| l.id).collect() +} + +fn background(s: &Session) -> Option { + s.active().unwrap().doc.background().map(|l| l.id) +} + +#[test] +fn two_selected_layers_grey_the_command_and_a_call_without_layer_is_refused() { + let (mut s, [_, a, b]) = no_background(); + s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap(); + assert_eq!(s.active().unwrap().selected_layers().len(), 2); + assert!(!s.is_enabled(CMD), "greyed with two layers selected"); + let why = s.disabled_reason(CMD).unwrap(); + assert!(why.contains("layers selected"), "the reason names the multi-selection: {why}"); + + let before = order(&s); + let revision = s.active().unwrap().revision; + let err = s.execute(CMD, json!({})).unwrap_err().to_string(); + assert!(err.contains("layers selected"), "the error names the reason: {err}"); + assert_eq!(order(&s), before, "no layer moved"); + assert_eq!(background(&s), None, "no Background made"); + assert_eq!(s.active().unwrap().revision, revision, "the document did not change"); + + // Three layers selected: greyed too. + let (mut s, [base, a, b]) = no_background(); + s.execute("layer.select", json!({"layers": [base.0, a.0, b.0]})).unwrap(); + assert!(!s.is_enabled(CMD)); + assert!(s.execute(CMD, json!({"layer": null})).is_err(), "a null layer is the selection too"); +} + +#[test] +fn an_explicit_layer_converts_that_layer_with_several_selected() { + // The named layer is one of the selected ones. + let (mut s, [_, a, b]) = no_background(); + s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap(); + let r = s.execute(CMD, json!({"layer": a.0})).unwrap(); + assert_eq!(ids(&r), a); + assert_eq!(background(&s), Some(a), "A is the Background"); + assert_eq!(order(&s)[0], a, "at the bottom"); + + // The named layer is outside the selection. + let (mut s, [base, a, b]) = no_background(); + s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap(); + s.execute(CMD, json!({"layer": base.0})).unwrap(); + assert_eq!(background(&s), Some(base)); +} + +#[test] +fn one_selected_layer_keeps_todays_rules() { + let live = |make: &dyn Fn(&mut Session)| { + let (mut s, _) = no_background(); + make(&mut s); + assert_eq!(s.active().unwrap().selected_layers().len(), 1); + s.is_enabled(CMD) + }; + assert!(live(&|_| {}), "pixel layer"); + assert!( + live(&|s| { + s.execute("type.create", json!({"x": 2, "y": 10, "text": "I", "size": 8})).unwrap(); + }), + "type layer" + ); + assert!( + live(&|s| { + s.execute("edit.fill", json!({"contents": "black"})).unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + }), + "smart object" + ); + assert!( + live(&|s| { + s.execute("layer.newFillLayer.solidColor", json!({"color": "#4080c0"})).unwrap(); + }), + "solid colour fill layer" + ); + assert!( + live(&|s| { + s.execute("shape.create", json!({"kind": "rect", "rect": [2, 2, 6, 6]})).unwrap(); + }), + "shape layer" + ); + assert!( + !live(&|s| { + s.execute("layer.new.group", json!({})).unwrap(); + }), + "group" + ); + assert!( + !live(&|s| { + s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + }), + "adjustment layer" + ); + + // And one selected pixel layer converts as before. + let (mut s, [_, a, _]) = no_background(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + s.execute(CMD, json!({})).unwrap(); + assert_eq!(background(&s), Some(a)); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 44bad7c9526..f5205efc178 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -37,6 +37,7 @@ mod atn_video_frames; mod auto_blend_layer_kinds; mod average_smart; mod background_and_mask_depth; +mod background_from_layer_multi_select; mod background_job_target; mod background_layer_consistency; mod brush_smoothing;