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.
This commit is contained in:
kotulsky committed 2026-10-11 14:32:47 +02:00
1 parent 614d06892c
commit 5d8cb2a39a
5 files changed
+90 -2

No files matched your search

+63 -1
View File
@@ -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<Value> {
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<V
failed = Some(json!({"step": i, "id": id, "error": e.to_string()}));
break;
}
match s.execute(id, params.clone()) {
match playback_params(s, id, params).and_then(|p| s.execute(id, p)) {
Ok(value) => {
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::<Vec<_>>(), [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();
+1
View File
@@ -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, &params) {
let params = crate::actions_cmds::recorded_params(self, id, params);
self.journal.push((id.to_string(), params));
}
}
+3 -1
View File
@@ -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()))
}
+19
View File
@@ -94,6 +94,8 @@ pub(crate) fn play(app: &mut PhotocraftApp, params: &Value) -> Result<Value, Str
let mut failed = None;
for (i, (id, p)) in action.enabled_steps().skip_while(|(i, _)| *i < from) {
let result = (|| {
let resolved = actions_cmds::playback_params(&app.session, id, p).map_err(|e| e.to_string())?;
let p = &resolved;
if let Some(auth) = app.session.authorize {
auth(id, p).map_err(|e| e.to_string())?;
}
@@ -541,6 +543,23 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
mod tests {
use super::*;
#[test]
fn shell_playback_resolves_recorded_eye_target_on_new_document() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 8, "height": 8})).unwrap();
app.run("layer.new.layer", json!({"name": "Target"})).unwrap();
app.run("actions.record", json!({"name": "Hide"})).unwrap();
let id = app.session.active().unwrap().active_layer.unwrap();
app.run("layer.setProps", json!({"layer": id.0, "visible": false})).unwrap();
app.run("actions.stop", json!({})).unwrap();
app.run("file.new", json!({"width": 8, "height": 8})).unwrap();
app.run("layer.new.layer", json!({"name": "Target"})).unwrap();
let id = app.session.active().unwrap().active_layer.unwrap();
let result = play(&mut app, &json!({"action": "Hide"})).unwrap();
assert!(result.get("failed").is_none(), "{result}");
assert!(!app.session.active().unwrap().doc.layer(id).unwrap().visible);
}
#[test]
fn double_clicking_an_action_name_renames_it_in_place() {
use egui_kittest::{Harness, kittest::Queryable};
+4
View File
@@ -117,6 +117,10 @@ store expose and persist the same setting; scripts keep using canonical command
| `actions.toggle` | `{"action": name or index, "step": index?, "enabled": bool?}` → `{action, disabled}`. Switches one 0-based step (pending recorded steps included) on or off; omitting `enabled` flips it. Without `step`, switches every step: on unless all already are. Playback, Batch and droplets skip steps that are off. In the desktop panel, click a step's check box |
| `actions.rename` | `{"action": name or index, "name": str}` → `{action, index}`. Empty, whitespace-only or duplicate names are rejected and keep the old name. Nested `actions.play` steps that call the action by name and its function key follow the new name. In the desktop panel, double-click an action's name to rename it in place (Enter commits, Esc cancels) |
Newly recorded Layers eye gestures resolve their targets by layer name and duplicate-name
ordinal during playback rather than retaining a document-local layer id. A missing target
reports a failed step instead of changing another layer.
The Actions panel shows replayable steps immediately while recording. Click an individual
step to select it, then use the trash button to remove that step; selecting the action
heading instead targets the whole action. Step deletion also works during recording