PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix/1350 preserve active on save (#1416)
shmuelbiller11656961cc577
diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rsindex e3bd8c74b..684cb316b 100644--- a/crates/automation/src/headless.rs+++ b/crates/automation/src/headless.rs@@ -146,11 +146,16 @@ impl Headless {.map(|f| f.trim_start_matches('.').eq_ignore_ascii_case("pcraft")).unwrap_or_else(|| target.extension().is_some_and(|e| e.eq_ignore_ascii_case("pcraft")));if is_native {+ // Saving by index must not retarget the next automation command.+ let previously_active = self.session.active_index();self.session.set_active(i);if let Some(st) = self.session.active_mut() {st.saved_revision = st.revision;st.path = Some(target.to_string_lossy().into_owned());}+ if let Some(active) = previously_active {+ self.session.set_active(active);+ }}Ok(json!({ "path": target.to_string_lossy(), "warnings": warnings }))}diff --git a/crates/automation/tests/inactive_save.rs b/crates/automation/tests/inactive_save.rsnew file mode 100644index 000000000..7f7aaefc8--- /dev/null+++ b/crates/automation/tests/inactive_save.rs@@ -0,0 +1,36 @@+//! Regression for #1350: saving an inactive .pcraft file must not select it.++use photocraft_automation::Headless;+use serde_json::json;++#[test]+fn indexed_native_save_preserves_active_document_and_future_command_target() {+ let dir = std::env::temp_dir().join(format!(+ "pc-inactive-save-{}-{}",+ std::process::id(),+ std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()+ ));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("first.pcraft");+ let mut headless = Headless::trusted_local();++ headless.handle("doc.new", json!({"width": 2, "height": 2, "background": "white", "name": "first"})).unwrap();+ headless.handle("doc.new", json!({"width": 2, "height": 2, "background": "white", "name": "second"})).unwrap();+ assert_eq!(headless.session.active_index(), Some(1));+ let first_revision = headless.session.documents()[0].revision;+ let second_revision = headless.session.documents()[1].revision;++ headless.handle("doc.save", json!({"index": 0, "path": path.to_str().unwrap()})).unwrap();+ assert!(path.is_file(), "inactive document was saved");+ assert_eq!(headless.session.active_index(), Some(1), "save must not select document 0");+ let first = &headless.session.documents()[0];+ assert_eq!(first.path.as_deref(), path.to_str());+ assert_eq!(first.revision, first.saved_revision, "saved document is clean");++ headless.handle("engine.execute", json!({"command": "image.adjustments.invert", "params": {}})).unwrap();+ assert_eq!(headless.session.active_index(), Some(1));+ assert_eq!(headless.session.documents()[0].revision, first_revision);+ assert!(headless.session.documents()[1].revision > second_revision);+ drop(headless);+ std::fs::remove_dir_all(dir).unwrap();+}