PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(atn): capture pre-command selection so recorded sole layer delete omits numeric id (#7248)
seotarekd90346d148af
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rsindex 667285c0b..fe8ba0270 100644--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@ -1022,4 +1022,14 @@ fn numeric_layer_references_refuse_silent_fallback_to_current_selection() {let saved_blend = s.execute("actions.save", json!({"action": "RecordBlend"})).unwrap();assert_eq!(saved_blend["steps"], 1, "recorded blend shortcut step with active layer id must export successfully as target layer");assert_eq!(saved_blend["skipped"].as_array().unwrap().len(), 0);++ // Record explicit layer.delete of the sole selected layer in a two-layer document+ s.execute("layer.new.layer", json!({"name": "Second"})).unwrap();+ let second_id = s.active().unwrap().active_layer.unwrap().0;+ s.execute("actions.record", json!({"name": "RecordDelete"})).unwrap();+ s.execute("layer.delete", json!({"layer": second_id})).unwrap();+ s.execute("actions.stop", json!({})).unwrap();+ let saved_delete = s.execute("actions.save", json!({"action": "RecordDelete"})).unwrap();+ assert_eq!(saved_delete["steps"], 1, "recorded explicit delete of sole selected layer must export successfully as target delete");+ assert_eq!(saved_delete["skipped"].as_array().unwrap().len(), 0);}diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rsindex 2b3158350..534e6941d 100644--- a/crates/engine/src/jobs.rs+++ b/crates/engine/src/jobs.rs@@ -726,6 +726,7 @@ impl Session {self.jobs.spawn = background;// Commands this one runs on its own behalf journal after this mark (#3067).let nested_from = self.journal.len();+ let pre_selected = if self.actions.recording.is_some() { self.active().map(|st| st.selected_layers()) } else { None };// A replayable command is recorded as one step, so recording pauses while it runs: an// action it plays (a script step) must not flush the steps it ran so far into the action// being recorded, nor move the recording mark past the entries it is about to replace.@@ -757,7 +758,7 @@ impl Session {Ok(Started::Job(jid))}(Ok(v), None) => {- self.after_command(id, params, spec.journal, Some(nested_from));+ self.after_command(id, params, spec.journal, Some(nested_from), pre_selected.as_deref());Ok(Started::Done(v))}}@@ -782,7 +783,7 @@ impl Session {/// dropped, so a recorded action replays the command once, not its steps as well (#3067)./// Edit › Transform › Again (and Again on a Copy) is not replayable and keeps the concrete/// steps it ran instead.- fn after_command(&mut self, id: &str, params: Value, journal: bool, nested_from: Option<usize>) {+ fn after_command(&mut self, id: &str, params: Value, journal: bool, nested_from: Option<usize>, pre_selected: Option<&[LayerId]>) {// A layer-mask view ends when another layer becomes active (#196).if let Some(st) = self.active_mut() {crate::mask_view_cmds::fix(st);@@ -797,7 +798,7 @@ impl Session {self.journal.truncate(from);}if !crate::brush_cmds::coalesce_journal(self, id, ¶ms) {- let params = if self.actions.recording.is_some() { crate::layer_multi_cmds::recorded_step(self, id, params) } else { params };+ let params = if self.actions.recording.is_some() { crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected) } else { params };self.journal.push((id.to_string(), params));}}@@ -854,7 +855,7 @@ impl Session {// Restoring the viewed document first would attribute Fade and slice updates// to that document instead, when the user switched tabs mid-job.if r.is_ok() {- self.after_command(command, params.clone(), *journal, Some(nested_from));+ self.after_command(command, params.clone(), *journal, Some(nested_from), None);}// Keep the user's active document unless the job opened a new one.// This also restores the selection when the apply step returned an error.diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex f5c3e5ce8..bfe43bc47 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -124,13 +124,16 @@ pub(crate) fn recorded_selection(s: &Session, mut p: Value) -> Value {/// parameter references the document's sole selected layer, normalises it to the target form/// (omitting `layer`) so that the recorded step applies to whatever layer is targeted when/// replayed or exported to `.atn` (#7248).-pub(crate) fn recorded_step(s: &Session, id: &str, p: Value) -> Value {+pub(crate) fn recorded_step(s: &Session, id: &str, p: Value, pre_selected: Option<&[LayerId]>) -> Value {if id == "layer.select" {return recorded_selection(s, p);}let Some(layer_id) = p.get("layer").and_then(Value::as_u64).map(LayerId) else { return p };- let Some(st) = s.active() else { return p };- if st.selected_layers().as_slice() == [layer_id] {+ let is_sole_selected = match pre_selected {+ Some(sel) => sel == [layer_id],+ None => s.active().is_some_and(|st| st.selected_layers().as_slice() == [layer_id]),+ };+ if is_sole_selected {let mut p = p;if let Some(fields) = p.as_object_mut() {fields.remove("layer");