diff --git a/crates/engine-core/src/cmd/atn_cmds/steps_make.rs b/crates/engine-core/src/cmd/atn_cmds/steps_make.rs index 995a7120e5..75d22f0794 100644 --- a/crates/engine-core/src/cmd/atn_cmds/steps_make.rs +++ b/crates/engine-core/src/cmd/atn_cmds/steps_make.rs @@ -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 ") { diff --git a/crates/engine-core/src/cmd/atn_cmds/tests.rs b/crates/engine-core/src/cmd/atn_cmds/tests.rs index 12c04319ac..b9786c8404 100644 --- a/crates/engine-core/src/cmd/atn_cmds/tests.rs +++ b/crates/engine-core/src/cmd/atn_cmds/tests.rs @@ -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"})), diff --git a/crates/engine-core/src/cmd/extra_cmds.rs b/crates/engine-core/src/cmd/extra_cmds.rs index bbec2307c1..27e1854184 100644 --- a/crates/engine-core/src/cmd/extra_cmds.rs +++ b/crates/engine-core/src/cmd/extra_cmds.rs @@ -495,6 +495,9 @@ fn layer_from_background(s: &mut Session, p: &Value) -> Result { // `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 { 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 { "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 { ] } +#[cfg(test)] +#[path = "layer_from_background_visible_tests.rs"] +mod layer_from_background_visible_tests; #[cfg(test)] mod tests; diff --git a/crates/engine-core/src/cmd/layer_from_background_visible_tests.rs b/crates/engine-core/src/cmd/layer_from_background_visible_tests.rs new file mode 100644 index 0000000000..3caece2c30 --- /dev/null +++ b/crates/engine-core/src/cmd/layer_from_background_visible_tests.rs @@ -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); +}