PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge remote-tracking branch 'upstream/main' into fix/atn-numeric-layer-ref-7248
seotarek55dbbdf0604d
diff --cc crates/engine/src/atn_cmds/tests.rsindex a97706772,26865d406..993906c9a--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@@ -1069,108 -1303,25 +1303,127 @@@ fn fractional_translation_only_transforassert_eq!(steps[0].1.get("interpolation"), Some(&json!("bilinear")));}+#[test]+fn numeric_layer_references_refuse_silent_fallback_to_current_selection() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();++ let parse_first_desc = |v: &Value| -> Descriptor {+ let bytes = photocraft_paint::tile::b64_decode(v["data"].as_str().unwrap()).unwrap();+ let file = atn::parse(&bytes).unwrap();+ file.actions[0].items[0].descriptor.clone().unwrap()+ };++ // Named layer reference exports successfully with Name reference to "A"+ s.actions.list.push(Action::new("DeleteNamed", vec![("layer.delete".into(), json!({"layer": "A"}))]));+ let saved_named = s.execute("actions.save", json!({"action": "DeleteNamed"})).unwrap();+ assert_eq!(saved_named["steps"], 1);+ let named_desc = parse_first_desc(&saved_named);+ match get_ref(&named_desc, "null").unwrap() {+ [ReferenceItem::Name { name, .. }] => assert_eq!(name.to_string_lossy(), "A"),+ other => panic!("expected Name reference to A, got {other:?}"),+ }++ // Omitted layer reference (target selection) exports successfully with Trgt enumerated reference+ s.actions.list.push(Action::new("DeleteTarget", vec![("layer.delete".into(), json!({}))]));+ let saved_target = s.execute("actions.save", json!({"action": "DeleteTarget"})).unwrap();+ assert_eq!(saved_target["steps"], 1);+ let target_desc = parse_first_desc(&saved_target);+ match get_ref(&target_desc, "null").unwrap() {+ [ReferenceItem::Enumerated { value, .. }] => assert!(value.is("Trgt")),+ other => panic!("expected Trgt enumerated reference, got {other:?}"),+ }++ // Explicit null layer reference also exports successfully as target selection with Trgt enumerated reference+ s.actions.list.push(Action::new("DeleteNull", vec![("layer.delete".into(), json!({"layer": null}))]));+ let saved_null = s.execute("actions.save", json!({"action": "DeleteNull"})).unwrap();+ assert_eq!(saved_null["steps"], 1);+ let null_desc = parse_first_desc(&saved_null);+ match get_ref(&null_desc, "null").unwrap() {+ [ReferenceItem::Enumerated { value, .. }] => assert!(value.is("Trgt")),+ other => panic!("expected Trgt enumerated reference, got {other:?}"),+ }++ // Explicit numeric ID reference cannot be silently converted to current target in strict export+ s.actions.list.push(Action::new("DeleteNumeric", vec![("layer.delete".into(), json!({"layer": 42}))]));+ let before_actions = s.actions.list.clone();+ let before_rev = s.active().unwrap().revision;+ let err_delete = s.execute("actions.save", json!({"action": "DeleteNumeric"}));+ assert!(err_delete.is_err(), "strict export must reject numeric layer ID for layer.delete rather than falling back to current target");+ assert_eq!(s.actions.list, before_actions, "session actions must remain unchanged when save fails");+ assert_eq!(s.active().unwrap().revision, before_rev, "document revision must remain unchanged when save fails");+ let err_msg = err_delete.unwrap_err().to_string();+ assert!(err_msg.contains("(`layer.delete` with numeric layer 42)"), "error message should name the numeric layer: {err_msg}");++ // Same rejection and state invariance for remaining commands with numeric layer reference+ for (name, id, params) in [+ ("DuplicateNumeric", "layer.duplicate", json!({"layer": 42, "name": "Copy"})),+ ("DuplicateNewDocNumeric", "layer.duplicate", json!({"layer": 42, "document": "new"})),+ ("TranslateNumeric", "layer.translate", json!({"layer": 42, "dx": 5, "dy": 5})),+ ("SetPropsNumeric", "layer.setProps", json!({"layer": 42, "opacity": 0.5})),+ ("HideNumeric", "layer.hideLayers", json!({"layer": 42})),+ ("TransformNumeric", "edit.transform", json!({"layer": 42, "dx": 5, "dy": 5})),+ ("RenameNumeric", "layer.renameLayer", json!({"layer": 42, "name": "Renamed"})),+ ("LabelNumeric", "layer.setLabelColor", json!({"layer": 42, "color": "red"})),+ ("ShowNumeric", "layer.showLayers", json!({"layer": 42})),+ ("SelectNumeric", "layer.select", json!({"layer": 42})),+ ("BringToFrontNumeric", "layer.arrange.bringToFront", json!({"layer": 42})),+ ("SendToBackNumeric", "layer.arrange.sendToBack", json!({"layer": 42})),+ ("BringForwardNumeric", "layer.arrange.bringForward", json!({"layer": 42})),+ ("SendBackwardNumeric", "layer.arrange.sendBackward", json!({"layer": 42})),+ ] {+ s.actions.list.push(Action::new(name, vec![(id.into(), params)]));+ let before_actions = s.actions.list.clone();+ let before_rev = s.active().unwrap().revision;+ assert!(s.execute("actions.save", json!({"action": name})).is_err());+ assert_eq!(s.actions.list, before_actions, "session actions must remain unchanged when save fails");+ assert_eq!(s.active().unwrap().revision, before_rev, "document revision must remain unchanged when save fails");+ }++ // With skipUnsupported: true, numeric layer references are skipped+ let skipped = s.execute("actions.save", json!({"action": "DeleteNumeric", "skipUnsupported": true})).unwrap();+ assert_eq!(skipped["steps"], 0);+ assert_eq!(skipped["skipped"][0]["id"], "layer.delete");++ // Blend-mode shortcut records layer.setProps with active layer ID, which normalises to the target form+ let active_id = s.active().unwrap().active_layer.unwrap().0;+ s.execute("actions.record", json!({"name": "RecordBlend"})).unwrap();+ s.execute("layer.setProps", json!({"layer": active_id, "blend": "multiply"})).unwrap();+ s.execute("actions.stop", json!({})).unwrap();+ 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);+}++ /// Arbitrary canvas rotation must keep its interpolation through a strict export: dropping it+ /// silently replays the rotation with the default bicubic resampling.+ #[test]+ fn arbitrary_rotation_keeps_interpolation_through_atn() {+ for params in [+ json!({"angle": 17, "direction": "ccw", "interpolation": "nearest"}),+ json!({"angle": 17, "direction": "ccw", "interpolation": "bilinear"}),+ json!({"angle": 17, "direction": "ccw", "interpolation": "bicubic"}),+ json!({"angle": 17, "direction": "ccw"}),+ ] {+ let a = Action { name: "Rot".into(), steps: vec![("image.rotation.arbitrary".into(), params.clone())], ..Default::default() };+ let (bytes, skipped) = export("Rot", &[&a], &[], false).unwrap();+ assert!(skipped.is_empty());+ let imported = import(&bytes).unwrap();+ assert!(imported.unsupported.is_empty());+ assert_eq!(imported.actions[0].steps, vec![("image.rotation.arbitrary".to_string(), params)]);+ }+ }+/// A filter recorded against a layer mask must not export as a plain filter that edits the/// layer's pixels on replay: Photoshop's descriptor has no field for PhotoCraft's `target`.#[test]diff --cc crates/engine/src/jobs.rsindex 4ae2b4b1e,0ee057317..c98266205--- a/crates/engine/src/jobs.rs+++ b/crates/engine/src/jobs.rs@@@ -763,7 -785,10 +789,10 @@@ impl SessionOk(Started::Job(jid))}(Ok(v), None) => {+ if let Some(before) = before {+ self.add_relayout_damage(&before);+ }- 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))}}@@@ -803,7 -841,12 +845,11 @@@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, pre_selected) } 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,++ _ => crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected),+ };self.journal.push((id.to_string(), params));}}@@@ -823,9 -866,9 +869,8 @@@};self.end_job(&job, outcome);}--fn apply_job(&mut self, job: &Running, apply: Option<Apply>, payload: Payload) -> Result<Value> {- let Running { label, doc, snapshot, coalesce, color_restrict, command, params, journal, pre_selected, .. } = job;- let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, .. } = job;++ let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, pre_selected, .. } = job;let prev_active = self.active;let target = match doc {Some(d) => {@@@ -860,8 -916,17 +918,17 @@@// 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), pre_selected.as_deref());}+ // Give the user back the layers they selected while the job ran, unless the job chose a+ // selection itself (a new layer, say) or one of their layers is gone.+ if let (Some(started), Some(user), Some(st)) = (layers, user_layers, target.and_then(|i| self.docs.get_mut(i))) {+ let kept = LayerSelection::of(st) == *started;+ let exist = user.active.iter().chain(&user.selected).all(|id| st.doc.layer(*id).is_some());+ if (r.is_err() || kept) && exist {+ user.set(st);+ }+ }// 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.if target.is_some() && self.active == target {