Skip to content

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

Save: a script's JPEG or Targa answer survives the changed-on-disk prompt

Higgs32584df933879866a
diff --git a/crates/ui-egui/src/disk_change_prompt.rs b/crates/ui-egui/src/disk_change_prompt.rs
index 4a058016a..9025d2226 100644
--- a/crates/ui-egui/src/disk_change_prompt.rs
+++ b/crates/ui-egui/src/disk_change_prompt.rs
@@ -20,7 +20,7 @@ pub fn owns(fields: &Map<String, Value>) -> bool {
/// Opens the prompt instead of saving `doc` to its own file at `path` when another app changed
/// that file; `None` saves as usual. `flat` is a flat file confirmed in the Save dialog, which
/// is saved (and recorded) the way [`PhotocraftApp::save_dialog_flow`] does it.
-pub(crate) fn intercept(app: &mut PhotocraftApp, doc: DocId, path: &str, flat: bool) -> Option<Result<Value, String>> {
+pub(crate) fn intercept(app: &mut PhotocraftApp, doc: DocId, path: &str, flat: bool, choices: crate::SaveChoices) -> Option<Result<Value, String>> {
if !app.changed_on_disk(doc, path) {
return None;
}
@@ -29,7 +29,9 @@ pub(crate) fn intercept(app: &mut PhotocraftApp, doc: DocId, path: &str, flat: b
}
let name = crate::file_open::display_name(path);
let message = crate::i18n::fmt(tl!("{name} was changed by another app after it was opened or saved here."), &[("name", name.as_str())]);
- let fields = json!({MARK: true, "__label": "Save", "message": message, "document": doc.0, "path": path, "flat": flat});
+ let mut fields = json!({MARK: true, "__label": "Save", "message": message, "document": doc.0, "path": path, "flat": flat});
+ // A script's TIFF, Targa or JPEG answer is kept for the save Replace writes.
+ choices.record(&mut fields);
let id = app.ui.open_dialog(DialogKind::Command, fields.as_object().cloned().unwrap_or_default());
Some(Ok(json!({"dialog": id, "confirmationPending": true})))
}
@@ -45,9 +47,10 @@ pub fn confirm(app: &mut PhotocraftApp, fields: &Map<String, Value>) -> Result<V
let doc = fields.get("document").and_then(Value::as_u64).map(DocId).ok_or(tl!("The document is closed"))?;
let path = fields.get("path").and_then(Value::as_str).unwrap_or_default().to_string();
let flat = fields.get("flat").and_then(Value::as_bool) == Some(true);
+ let choices = crate::SaveChoices::from_step(&Value::Object(fields.clone()), "file.save")?;
app.with_document(doc, |app| {
if flat {
- let r = app.save_to(path.clone(), crate::SaveChoices::default())?;
+ let r = app.save_to(path.clone(), choices)?;
app.journal_as_plain_save(&path);
Ok(r)
} else {
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rs
index 2386f9d43..23a06c2c5 100644
--- a/crates/ui-egui/src/file_dialog/tests.rs
+++ b/crates/ui-egui/src/file_dialog/tests.rs
@@ -1200,3 +1200,33 @@ fn save_of_a_flat_file_asks_after_the_dialog_when_another_app_changed_it() {
assert!(!app.session.active().unwrap().is_dirty());
assert_eq!(app.session.journal.last(), Some(&("file.save".to_string(), json!({}))), "still recorded as a plain Save");
}
+
+/// A script's JPEG Options answer survives the changed-on-disk prompt: Replace writes with it,
+/// without asking JPEG Options.
+#[test]
+fn a_scripted_quality_survives_the_changed_on_disk_prompt() {
+ let ctx = egui::Context::default();
+ let path = "/pics/My image.jpg";
+ let (mut app, open, written) = jpeg_app(None, false);
+ let qualities: Rc<RefCell<Vec<Option<u8>>>> = Rc::default();
+ let q = qualities.clone();
+ app.services.export = Some(Box::new(move |_: &photocraft_doc::Document, _: &str, s: &crate::ExportSettings| {
+ q.borrow_mut().push(s.jpeg_quality);
+ Ok((b"out".to_vec(), Vec::new()))
+ }));
+ let disk = on_disk(&mut app);
+ changed_by_another_app(&disk, path);
+ app.opened_from(path);
+ app.run("image.adjustments.invert", json!({})).unwrap();
+ changed_by_another_app(&disk, path);
+ menus::invoke(&mut app, &ctx, "file.save", json!({"quality": 5})).unwrap();
+ app.poll_file_dialog(&ctx, None);
+ answer(&open, Some(FileDialogAnswer::SaveTo(path.into())));
+ app.poll_file_dialog(&ctx, None);
+ assert!(written.borrow().is_empty(), "the changed file is not replaced before the answer");
+ let id = disk_prompt(&app).expect("Save asks first").id;
+ crate::dialogs::confirm(&mut app, id).unwrap();
+ assert!(app.jpeg_options.is_none(), "the script answered JPEG Options");
+ assert_eq!(*written.borrow(), [path]);
+ assert_eq!(qualities.borrow().last().copied().flatten(), Some(photocraft_engine::file_cmds::jpeg_quality(5.0)));
+}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index d22adffd1..e6b7d1530 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1552,7 +1552,7 @@ impl PhotocraftApp {
// Confirming the document's own file: ask first if another app changed it (#6596).
if plain_save
&& own.as_deref() == Some(path.as_str())
- && let Some(r) = crate::disk_change_prompt::intercept(app, doc, &path, true)
+ && let Some(r) = crate::disk_change_prompt::intercept(app, doc, &path, true, choices)
{
return r;
}
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 090e675d8..dc1ab9012 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -240,7 +240,8 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
Some((true, path, d)) => {
// Another app changed the file since it was opened or saved: ask first (#6596).
let doc = d.doc.id;
- crate::disk_change_prompt::intercept(app, doc, &path, false).unwrap_or_else(|| app.save_in_place(path))
+ crate::disk_change_prompt::intercept(app, doc, &path, false, crate::SaveChoices::default())
+ .unwrap_or_else(|| app.save_in_place(path))
}
Some((false, path, d)) if photocraft_engine::file_cmds::saves_in_place(&path) => {
// Its own file is layered but can't hold what the document has now.