Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(actions): strict .atn export keeps Apply Image's flat source fields (#7197)

VishnuByrrajub65e0af1979a
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index 9e8eb6cb7..811d1dbc5 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -1736,7 +1736,13 @@ fn apply_image_from(d: &Descriptor) -> Mapped {
}
fn apply_image_to(_: &str, p: &Value) -> Option<Descriptor> {
- let source = p.get("source").cloned().unwrap_or(json!({}));
+ // The Apply Image dialog sends flat `sourceLayer`/`sourceChannel`/`sourceInvert` fields; read
+ // them as the command does, or the export would quietly reset them to the merged composite.
+ let source = crate::channel_cmds::source_spec(p, "source", "source").unwrap_or(json!({}));
+ // A source from another open document has no event form here: skip the step, don't rewrite it.
+ if source.get("document").is_some_and(|d| !d.is_null()) {
+ return None;
+ }
let channel = jstr(&source, "channel").unwrap_or("composite");
let chan = lookup_back(SOURCE_CHANNELS, channel).or_else(|| lookup_back(CHANNELS, channel))?;
let mut items = vec![ReferenceItem::Enumerated { class: class("Chnl"), type_id: code("Chnl"), value: code(chan) }];
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 26865d406..bbcef0ad5 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -1387,3 +1387,23 @@ fn function_keys_keep_shift_and_command() {
}
assert_eq!(super::function_key_name(7, false, false), "F7");
}
+
+/// The Apply Image dialog's flat source fields keep their meaning through a strict `.atn` export
+/// (#7197): they used to come back as the merged composite.
+#[test]
+fn apply_image_flat_source_fields_survive_a_strict_export() {
+ let flat = json!({"sourceLayer": "Pixels", "sourceChannel": "green", "sourceInvert": true, "blending": "normal", "opacity": 75});
+ let item = export_item("image.applyImage", &flat, "S").expect("the flat form exports");
+ let event = event_for(&item.event).expect("a known event");
+ let Mapped::Steps(steps) = (event.from)(item.descriptor.as_ref().unwrap()) else { panic!("does not import back") };
+ assert_eq!(steps.len(), 1);
+ assert_eq!(steps[0].1["source"], json!({"layer": "Pixels", "channel": "green", "invert": true}));
+ // The nested form is unchanged, and the "merged" layer of a flat form stays merged.
+ let merged = json!({"sourceLayer": "merged", "sourceChannel": "red"});
+ let item = export_item("image.applyImage", &merged, "S").expect("exports");
+ let Mapped::Steps(steps) = (event.from)(item.descriptor.as_ref().unwrap()) else { panic!("does not import back") };
+ assert_eq!(steps[0].1["source"], json!({"layer": "merged", "channel": "red"}));
+ // A source in another document cannot be written: the step is skipped, not rewritten.
+ assert!(export_item("image.applyImage", &json!({"source": {"document": "Other", "channel": "red"}}), "S").is_none());
+ assert!(export_item("image.applyImage", &json!({"sourceDocument": "Other"}), "S").is_none());
+}
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 991f40dc0..91dc94c7e 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -569,7 +569,7 @@ impl Blending {
/// A source spec from `p[key]`, or from flat `<prefix>Document` / `<prefix>Layer` /
/// `<prefix>Channel` / `<prefix>Invert` keys (what the generic dialog form sends).
-fn source_spec(p: &Value, key: &str, prefix: &str) -> Option<Value> {
+pub(crate) fn source_spec(p: &Value, key: &str, prefix: &str) -> Option<Value> {
if let Some(v) = p.get(key) {
return Some(v.clone());
}