PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Actions: Apply Layer Comp is recorded by the comp's name, so it plays in any document (#7181) (#7790)
Korg1ch990483a95e80
diff --git a/crates/engine/src/comps_cmds.rs b/crates/engine/src/comps_cmds.rsindex fe5708387..43a8ec12f 100644--- a/crates/engine/src/comps_cmds.rs+++ b/crates/engine/src/comps_cmds.rs@@ -208,6 +208,23 @@ fn apply_cmd(s: &mut Session, p: &Value) -> Result<Value> {apply(s, id, "Apply Layer Comp")}+/// How an action records Apply Layer Comp: by the comp's name, as Photoshop does. Comp ids are+/// local to a document, so a recorded id would apply whichever comp of another document has it+/// (#7181); by name the step finds that document's comp of the name, or stops with an error.+/// The comp is the one just applied, so `{}` and `null` (the last applied) record its name too.+pub(crate) fn recorded_apply(s: &Session, mut p: Value) -> Value {+ let Some(d) = s.active() else { return p };+ let Some(comp) = d.doc.last_applied_comp.and_then(|id| d.doc.comp(id)) else { return p };+ // `null` params run as `{}` (the last applied comp), so they are recorded by name too.+ if p.is_null() {+ p = json!({});+ }+ if let Value::Object(m) = &mut p {+ m.insert("comp".into(), json!(comp.name));+ }+ p+}+fn step(s: &mut Session, delta: i64) -> Result<Value> {let d = s.active().ok_or(EngineError::NoDocument)?;let n = d.doc.layer_comps.len() as i64;diff --git a/crates/engine/src/comps_cmds/tests.rs b/crates/engine/src/comps_cmds/tests.rsindex 5458523bc..d4857ad02 100644--- a/crates/engine/src/comps_cmds/tests.rs+++ b/crates/engine/src/comps_cmds/tests.rs@@ -347,3 +347,56 @@ fn clearing_warnings_of_an_unknown_comp_is_an_error() {assert_eq!(doc(&s).layer_comps, before);assert_eq!(s.execute("layerComp.updateWarnings", json!({})).unwrap()["count"], 1);}++/// Apply Layer Comp is recorded by the comp's name, not its document-local id: played in another+/// document it applies that document's comp of the name, or stops when there is none (#7181).+#[test]+fn a_recorded_apply_layer_comp_finds_the_comp_by_name() {+ let (mut s, _, _) = session(8);+ s.execute("layerComp.new", json!({"name": "Wanted"})).unwrap();+ let wanted = doc(&s).layer_comps[0].id;+ s.execute("actions.record", json!({"name": "ApplyWanted"})).unwrap();+ s.execute("layerComp.apply", json!({"comp": wanted})).unwrap();+ s.execute("actions.stop", json!({})).unwrap();+ // `{}` and `null` apply the last applied comp; they are recorded by name too.+ s.execute("actions.record", json!({"name": "ApplyLast"})).unwrap();+ s.execute("layerComp.apply", json!({})).unwrap();+ s.execute("actions.stop", json!({})).unwrap();+ s.execute("actions.record", json!({"name": "ApplyNull"})).unwrap();+ s.execute("layerComp.apply", Value::Null).unwrap();+ s.execute("actions.stop", json!({})).unwrap();+ for action in ["ApplyWanted", "ApplyLast", "ApplyNull"] {+ let steps = s.execute("actions.get", json!({"action": action})).unwrap()["steps"].clone();+ assert_eq!(steps, json!([["layerComp.apply", {"comp": "Wanted"}]]), "{action}");+ }++ // A second document whose comp with the recorded id is another one.+ let (mut other, _, ob) = session(8);+ other.actions = s.actions.clone();+ let show = |s: &mut Session, visible: bool| {+ s.edit("t", |d, _| {+ d.layer_mut(ob).unwrap().visible = visible;+ Ok(())+ })+ .unwrap();+ };+ show(&mut other, false);+ other.execute("layerComp.new", json!({"name": "Different"})).unwrap();+ show(&mut other, true);+ other.execute("layerComp.new", json!({"name": "Wanted"})).unwrap();+ assert_eq!(doc(&other).layer_comps[0].id, wanted, "the recorded id names `Different` here");+ show(&mut other, false);+ other.execute("actions.play", json!({"action": "ApplyWanted"})).unwrap();+ let applied = doc(&other).last_applied_comp.and_then(|id| doc(&other).comp(id)).map(|c| c.name.clone());+ assert_eq!(applied.as_deref(), Some("Wanted"));+ assert!(doc(&other).layer(ob).unwrap().visible, "Wanted shows the layer");++ // A document without a comp of that name stops the action instead of applying another one.+ other.execute("layerComp.delete", json!({"comp": "Wanted"})).unwrap();+ show(&mut other, false);+ let before = (doc(&other).last_applied_comp, doc(&other).layer(ob).unwrap().visible);+ let played = other.execute("actions.play", json!({"action": "ApplyWanted"})).unwrap();+ assert_eq!(played["ran"], 0, "{played}");+ assert!(played["failed"]["error"].as_str().unwrap().contains("no layer comp named \"Wanted\""), "{played}");+ assert_eq!((doc(&other).last_applied_comp, doc(&other).layer(ob).unwrap().visible), before, "the document is unchanged");+}diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rsindex 5098b958b..583465b46 100644--- a/crates/engine/src/jobs.rs+++ b/crates/engine/src/jobs.rs@@ -816,8 +816,12 @@ impl Session {self.journal.truncate(from);}if !crate::brush_cmds::coalesce_journal(self, id, ¶ms) {- let params =- if id == "layer.select" && self.actions.recording.is_some() { crate::layer_multi_cmds::recorded_selection(self, params) } else { params };+ let params = match id {+ _ if self.actions.recording.is_none() => params,+ "layer.select" => crate::layer_multi_cmds::recorded_selection(self, params),+ "layerComp.apply" => crate::comps_cmds::recorded_apply(self, params),+ _ => params,+ };self.journal.push((id.to_string(), params));}}