Layers: Layer from Background is unavailable in Indexed, Bitmap and Multichannel documents (#16762)

The enabled check only asked whether the document had a Background, so in
these modes the menu entry, the Background row's double-click and its lock
icon turned the Background into "Layer 0". The check and the command (for
action steps with ifPresent) now refuse there; the Background stays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Xevos117andClaude Opus 5.5 committed 2026-11-25 01:15:04 +00:00
1 parent 928326271c
commit 4b1d81ebd0
2 files changed
+87 -1

No files matched your search

+19 -1
View File
@@ -45,7 +45,20 @@ fn has_clip_and_selection(s: &Session) -> std::result::Result<(), String> {
fn has_background(s: &Session) -> std::result::Result<(), String> {
let d = s.active().ok_or("no document open")?;
d.doc.background().map(|_| ()).ok_or_else(|| "the document has no Background layer".into())
d.doc.background().ok_or("the document has no Background layer")?;
background_convertible_in(d.doc.mode)
}
/// Layer from Background isn't available in Indexed, Bitmap and Multichannel documents, which
/// keep their Background (#16762).
fn background_convertible_in(mode: photocraft_color::ColorMode) -> std::result::Result<(), String> {
use photocraft_color::ColorMode::{Bitmap, Indexed, Multichannel};
match mode {
Indexed => Err("Layer from Background is not available in Indexed Color mode".into()),
Bitmap => Err("Layer from Background is not available in Bitmap mode".into()),
Multichannel => Err("Layer from Background is not available in Multichannel mode".into()),
_ => Ok(()),
}
}
/// The layers a layer-style command acts on: as [`crate::layer_multi_cmds::targets`], but with
@@ -489,6 +502,8 @@ fn layer_from_background(s: &mut Session, p: &Value) -> Result<Value> {
if d.doc.background().is_none() && p.get("ifPresent").and_then(Value::as_bool) == Some(true) {
return Ok(json!({"layer": null}));
}
// `ifPresent` skips the precondition, so the mode is checked here too.
background_convertible_in(d.doc.mode).map_err(EngineError::Other)?;
// `select: false`: the targeted layer stays targeted, as when a Photoshop action's `set`
// names the Background while another layer is selected.
let select = p.get("select").and_then(Value::as_bool).unwrap_or(true);
@@ -1216,5 +1231,8 @@ pub fn specs() -> Vec<CommandSpec> {
]
}
#[cfg(test)]
#[path = "layer_from_background_modes_tests.rs"]
mod layer_from_background_modes_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,68 @@
//! Layer › New › Layer from Background per colour mode (#16762).
//!
//! The reference greys the command in Indexed Color documents, and a scripted conversion fails
//! there and in Bitmap and Multichannel documents; the Background stays. RGB, Grayscale, CMYK and
//! Lab convert it to a normal layer.
use super::*;
use photocraft_color::ColorMode;
const CMD: &str = "layer.new.layerFromBackground";
/// A white document with a Background, of `mode` (as `file.new` names it) at `depth` bits.
fn session(mode: &str, depth: u32) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 8, "height": 8, "mode": mode, "depth": depth, "background": "white"})).unwrap();
assert!(s.active().unwrap().doc.background().is_some(), "{mode} {depth}-bit has a Background");
s
}
/// Greyed, and a scripted run, also as an action step with `ifPresent`, keeps the Background.
fn assert_refused(s: &mut Session, what: &str) {
assert!(!s.is_enabled(CMD), "{what}: Layer from Background is greyed");
for p in [json!({}), json!({"ifPresent": true, "select": false})] {
assert!(s.execute(CMD, p.clone()).is_err(), "{what}: {p} is refused");
assert!(s.active().unwrap().doc.background().is_some(), "{what}: {p} keeps the Background");
}
}
#[test]
fn indexed_keeps_its_background() {
let mut s = session("rgb", 8);
s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": false})).unwrap();
assert_eq!(s.active().unwrap().doc.mode, ColorMode::Indexed);
assert_refused(&mut s, "Indexed");
}
#[test]
fn bitmap_keeps_its_background() {
let mut s = session("gray", 8);
s.execute("image.mode.bitmap", json!({"method": "threshold"})).unwrap();
assert_eq!(s.active().unwrap().doc.mode, ColorMode::Bitmap);
assert_refused(&mut s, "Bitmap");
}
#[test]
fn multichannel_keeps_its_background() {
// Image › Mode › Multichannel leaves no Background here, so the mode is set on a document
// that has one, as a Multichannel file with a Background opens.
let mut s = session("rgb", 8);
s.edit("mode", |doc, _| {
doc.mode = ColorMode::Multichannel;
Ok(())
})
.unwrap();
assert_refused(&mut s, "Multichannel");
}
#[test]
fn rgb_grayscale_cmyk_and_lab_still_convert() {
for (mode, depths) in [("rgb", &[8, 16, 32][..]), ("gray", &[8, 16, 32]), ("cmyk", &[8, 16]), ("lab", &[8, 16])] {
for &depth in depths {
let mut s = session(mode, depth);
assert!(s.is_enabled(CMD), "{mode} {depth}-bit");
s.execute(CMD, json!({})).unwrap();
assert!(s.active().unwrap().doc.background().is_none(), "{mode} {depth}-bit: converted");
}
}
}