From 5d8cb2a39ad70e31260f1f3fc2010ca132daff65 Mon Sep 17 00:00:00 2001 From: kotulsky <7805437+kotulsky@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:25:25 +0200 Subject: [PATCH] fix(actions): resolve recorded visibility targets by layer name Record layer-eye targets by name and duplicate-name ordinal rather than document-local ids. Resolve them identically in engine and shell playback, fail visibly for missing targets, and allow explicit visibility edits without an active layer. Add regressions for another document, duplicate layer names, deselected layers, document Undo and shell playback. Existing recordings are preserved. --- crates/engine/src/actions_cmds.rs | 64 ++++++++++++++++++++++++++++++- crates/engine/src/jobs.rs | 1 + crates/engine/src/lib.rs | 4 +- crates/ui-egui/src/actions.rs | 19 +++++++++ docs/control-protocol.md | 4 ++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs index 348011f75d..4ae0a79e0e 100644 --- a/crates/engine/src/actions_cmds.rs +++ b/crates/engine/src/actions_cmds.rs @@ -77,6 +77,47 @@ pub struct ActionState { pub rev: u64, } +/// Visibility gestures name a layer for replay, since document-local ids change on another image. +pub(crate) fn recorded_params(s: &Session, id: &str, mut p: Value) -> Value { + if s.actions.recording.is_none() || id != "layer.setProps" || p.get("visible").is_none() { + return p; + } + let Some(st) = s.active() else { return p }; + let Some(layer) = p.get("layer").and_then(Value::as_u64).map(photocraft_doc::LayerId).or(st.active_layer).and_then(|id| st.doc.layer(id)) else { return p }; + let ordinal = st.doc.walk().into_iter().filter(|(_, _, l)| l.name == layer.name).position(|(_, _, l)| l.id == layer.id).unwrap_or(0); + if let Some(object) = p.as_object_mut() { + object.remove("layer"); + object.remove("coalesce"); + object.insert("_actionLayer".into(), json!({"name": layer.name, "ordinal": ordinal})); + } + p +} + +/// Resolve the recorded visibility target identically in engine and shell playback. +pub fn playback_params(s: &Session, id: &str, p: &Value) -> Result { + let mut p = p.clone(); + if id == "layer.setProps" + && let Some(target) = p.get("_actionLayer") + { + let name = target.get("name").and_then(Value::as_str).ok_or_else(|| bad(id, "missing recorded layer name"))?; + let ordinal = + target.get("ordinal").and_then(Value::as_u64).and_then(|v| usize::try_from(v).ok()).ok_or_else(|| bad(id, "invalid recorded layer ordinal"))?; + let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; + let layer = doc + .walk() + .into_iter() + .filter(|(_, _, l)| l.name == name) + .nth(ordinal) + .map(|(_, _, l)| l.id) + .ok_or_else(|| bad(id, format!("recorded layer `{name}` does not exist")))?; + if let Some(object) = p.as_object_mut() { + object.remove("_actionLayer"); + object.insert("layer".into(), json!(layer.0)); + } + } + Ok(p) +} + impl ActionState { pub(crate) fn touch(&mut self) { self.rev = self.rev.saturating_add(1); @@ -292,7 +333,7 @@ fn run_recorded(s: &mut Session, action: &Action, from: usize) -> (u64, Option { if let Some(error) = nested_failure(id, &value) { failed = Some(json!({"step": i, "id": id, "error": error})); @@ -673,6 +714,27 @@ mod tests { assert_eq!(s.actions.list[0].disabled.iter().copied().collect::>(), [1], "rejected toggles change nothing"); } + #[test] + fn recorded_eye_visibility_replays_on_another_document_and_duplicate_names() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Same"})).unwrap(); + let hidden = s.execute("layer.new.layer", json!({"name": "Same"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("actions.record", json!({"name": "Hide"})).unwrap(); + s.execute("layer.setProps", json!({"layer": hidden, "visible": false, "coalesce": "eye-sweep"})).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let keep = s.execute("layer.new.layer", json!({"name": "Same"})).unwrap()["layer"].as_u64().unwrap(); + let target = s.execute("layer.new.layer", json!({"name": "Same"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("select.deselectLayers", json!({})).unwrap(); + let result = s.execute("actions.play", json!({"action": "Hide"})).unwrap(); + assert!(result.get("failed").is_none(), "{result}"); + assert!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(keep)).unwrap().visible); + assert!(!s.active().unwrap().doc.layer(photocraft_doc::LayerId(target)).unwrap().visible); + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(target)).unwrap().visible); + } + #[test] fn rename_keeps_calls_and_function_key_and_rejects_bad_names() { let mut s = Session::new(); diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index 9b612e3c4c..a2c11c39f8 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -678,6 +678,7 @@ impl Session { crate::automate_cmds::after_command(self, id); self.sync_preset_store(); if journal && !crate::brush_cmds::coalesce_journal(self, id, ¶ms) { + let params = crate::actions_cmds::recorded_params(self, id, params); self.journal.push((id.to_string(), params)); } } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index a20d02bf8c..037f3f7ecf 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -531,7 +531,9 @@ impl Session { /// Whether `id` is a layer command whose `"layer"` param names an existing layer, so it needs no /// active layer (#2457). Matches how `layer_param` resolves the layer when the command runs. fn names_existing_layer(&self, id: &str, params: &Value) -> bool { - matches!(id, "layer.hideLayers" | "layer.showLayers" | "layer.renameLayer") + (matches!(id, "layer.hideLayers" | "layer.showLayers" | "layer.renameLayer") + || (id == "layer.setProps" + && params.as_object().is_some_and(|o| o.contains_key("visible") && o.keys().all(|k| matches!(k.as_str(), "layer" | "visible" | "coalesce"))))) && params.get("layer").and_then(Value::as_u64).is_some_and(|l| self.active().is_some_and(|st| st.doc.layer(LayerId(l)).is_some())) } diff --git a/crates/ui-egui/src/actions.rs b/crates/ui-egui/src/actions.rs index cf06940224..2a9c92a099 100644 --- a/crates/ui-egui/src/actions.rs +++ b/crates/ui-egui/src/actions.rs @@ -94,6 +94,8 @@ pub(crate) fn play(app: &mut PhotocraftApp, params: &Value) -> Result