Merge #14836 into the merge queue batch

Layers: Layer from Background makes a hidden Background visible (#14822)
This commit is contained in:
David Mann committed 2026-11-30 00:25:13 +01:00
commit 96b07182b7
4 files changed
+79 -3

No files matched your search

@@ -997,7 +997,7 @@ fn background_from(d: &Descriptor) -> Mapped {
// On an image that opened as a normal layer there is no Background to convert;
// Photoshop stops there, but the step's purpose (an editable layer) is met.
// Photoshop's `set` of another layer doesn't target it: the selected layer stays.
let mut v = vec![("layer.new.layerFromBackground", json!({"ifPresent": true, "select": false}))];
let mut v = vec![("layer.new.layerFromBackground", json!({"ifPresent": true, "select": false, "show": false}))];
let mut props = Map::new();
props.insert("layer".into(), "Layer 0".into());
if let Some(t) = get_obj(d, "T ") {
+1 -1
View File
@@ -1019,7 +1019,7 @@ fn every_mapped_command_exports_and_imports_back() {
("layer.newFillLayer.pattern", json!({"pattern": "b7a3c1d2-0000-4000-8000-000000000001", "patternName": "Twin", "scale": 50})),
("image.mode.bitmap", json!({"method": "threshold"})),
("image.imageSize", json!({"resolution": 1200, "resample": "preserveDetails", "keepPrintSize": true, "scaleStyles": true})),
("layer.new.layerFromBackground", json!({"ifPresent": true, "select": false})),
("layer.new.layerFromBackground", json!({"ifPresent": true, "select": false, "show": false})),
// COPYCAT, AJ_Actions2023 and the Line and Color tutorial set.
("layer.smartFilter.deleteFilterMask", json!({"ifPresent": true})),
("layer.smartFilter.blendingOptions", json!({"index": 1, "opacity": 0.5, "blend": "Darken"})),
+12 -1
View File
@@ -495,6 +495,9 @@ fn layer_from_background(s: &mut Session, p: &Value) -> Result<Value> {
// `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);
// `show: false`: an action's `set` step on the Background replays the conversion and keeps
// its visibility, like the implicit conversions.
let show = p.get("show").and_then(Value::as_bool).unwrap_or(true);
// The reference names the state "Make Layer" however the command runs (#16760).
let id = s.edit("Make Layer", |doc, active| {
let bg = doc.background().ok_or(EngineError::Other("the document has no Background layer".into()))?;
@@ -503,6 +506,11 @@ fn layer_from_background(s: &mut Session, p: &Value) -> Result<Value> {
let l = doc.background_mut().ok_or(EngineError::Other("the document has no Background layer".into()))?;
unlock_background(l);
l.name = name;
// Only this explicit conversion shows a hidden Background, as in the reference
// application; implicit ones through `background_to_layer` keep its visibility.
if show {
l.visible = true;
}
if select || active.is_none() {
*active = Some(id);
}
@@ -1040,7 +1048,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Layer From Background…",
&["Layer", "New"],
None,
r##"{"ifPresent":bool=false (true: a document without a Background is left as it is, as a Photoshop action's step needs on an image that opened as a normal layer),"select":bool=true (false: the targeted layer stays targeted)}"##,
r##"{"ifPresent":bool=false (true: a document without a Background is left as it is, as a Photoshop action's step needs on an image that opened as a normal layer),"select":bool=true (false: the targeted layer stays targeted),"show":bool=true (true: a hidden Background becomes a visible layer, as the menu command does; false: it keeps its visibility, as an action's step does)}"##,
has_background,
layer_from_background
),
@@ -1236,6 +1244,9 @@ pub fn specs() -> Vec<CommandSpec> {
]
}
#[cfg(test)]
#[path = "layer_from_background_visible_tests.rs"]
mod layer_from_background_visible_tests;
#[cfg(test)]
mod tests;
@@ -0,0 +1,65 @@
//! Layer from Background on a hidden Background (#14822): the reference application makes the new
//! layer visible, and one undo brings the hidden Background back. Implicit conversions (an edit the
//! Background can't take) keep its visibility.
use super::*;
/// White Background (hidden when `hidden`) plus one normal layer above it.
fn session(hidden: bool) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 40, "height": 30})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
let bg = background(&s).id;
s.execute("layer.setProps", json!({"layer": bg.0, "visible": !hidden})).unwrap();
s
}
fn background(s: &Session) -> &Layer {
&s.active().unwrap().doc.layers[0]
}
#[test]
fn converting_a_hidden_background_makes_the_layer_visible() {
let mut s = session(true);
assert!(!background(&s).visible);
let h = s.active().unwrap().history.past_len();
s.execute("layer.new.layerFromBackground", json!({})).unwrap();
let l = background(&s);
assert_eq!(l.name, "Layer 0");
assert!(l.visible, "the converted layer is visible");
assert_eq!(s.active().unwrap().history.past_len(), h + 1, "one History step");
assert!(s.undo());
let l = background(&s);
assert_eq!(l.name, "Background");
assert!(!l.visible, "undo brings back the hidden Background");
assert!(l.locks.transparency && l.locks.position, "with its locks");
}
#[test]
fn converting_a_visible_background_keeps_it_visible() {
let mut s = session(false);
s.execute("layer.new.layerFromBackground", json!({})).unwrap();
assert!(background(&s).visible);
}
#[test]
fn an_implicit_conversion_keeps_a_hidden_background_hidden() {
let mut s = session(true);
let bg = background(&s).id;
// A layer mask is something the Background can't take, so it becomes "Layer 0" first.
s.execute("layer.layerMask.revealAll", json!({"layer": bg.0})).unwrap();
let l = background(&s);
assert_eq!(l.name, "Layer 0");
assert!(!l.visible);
}
#[test]
fn an_action_step_keeps_a_hidden_background_hidden() {
// An action's `set` step on the Background plays with `show: false`.
let mut s = session(true);
s.execute("layer.new.layerFromBackground", json!({"ifPresent": true, "select": false, "show": false})).unwrap();
let l = background(&s);
assert_eq!(l.name, "Layer 0");
assert!(!l.visible);
}