PhotoCraft: An open-source, photo editor written by scratch in pure Rust
JPEG Options: a plain Save confirmed in the dialog records its answer, and replays it
Higgs325847a2068c6245d
diff --git a/crates/ui-egui/src/file_dialog/tests.rs b/crates/ui-egui/src/file_dialog/tests.rsindex 8a53e739e..2386f9d43 100644--- a/crates/ui-egui/src/file_dialog/tests.rs+++ b/crates/ui-egui/src/file_dialog/tests.rs@@ -187,6 +187,42 @@ fn plain_save_of_a_flat_jpeg_asks_jpeg_options_and_records_a_plain_save() {assert_eq!(app.session.actions.list[0].steps, [("file.save".to_string(), json!({}))]);}+/// A plain Save confirmed in the dialog keeps a changed JPEG Options answer in its recorded step,+/// and playing it writes the active flat JPEG with that answer, without asking.+#[test]+fn plain_save_of_a_flat_jpeg_records_and_replays_a_changed_quality() {+ let ctx = egui::Context::default();+ let (mut app, open, written) = jpeg_app(Some("/pics/photo.jpg"), 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()))+ }));+ app.run("actions.record", json!({})).unwrap();+ menus::invoke(&mut app, &ctx, "file.save", json!({})).unwrap();+ app.poll_file_dialog(&ctx, None);+ answer(&open, Some(FileDialogAnswer::SaveTo("/pics/photo.jpg".into())));+ app.poll_file_dialog(&ctx, None);+ let prompt = app.jpeg_options.as_mut().expect("JPEG Options");+ prompt.quality = 3;+ prompt.format = photocraft_codecs::JpegFormat::Progressive;+ crate::jpeg_options_ui::confirm(&mut app, &ctx);+ app.run("actions.stop", json!({})).unwrap();+ assert_eq!(app.session.actions.list[0].steps, [("file.save".to_string(), json!({"quality": 3, "jpegFormat": "progressive"}))]);+ // Played on another flat JPEG: written over itself at quality 3, with no dialog or prompt.+ let bytes = photocraft_io::export(&app.session.active().unwrap().doc, "other.jpg", &Default::default()).unwrap().bytes;+ let index = app.session.add_document(photocraft_io::import("other.jpg", &bytes).unwrap().document, Some("/pics/other.jpg".into()));+ app.session.set_active(index);+ written.borrow_mut().clear();+ let played = app.run("actions.play", json!({"action": 0})).unwrap();+ assert!(played.get("failed").is_none(), "{played}");+ assert!(app.jpeg_options.is_none(), "playback does not ask");+ assert!(open.borrow().is_empty(), "no Save dialog");+ assert_eq!(*written.borrow(), ["/pics/other.jpg"]);+ assert_eq!(qualities.borrow().last().copied().flatten(), Some(photocraft_engine::file_cmds::jpeg_quality(3.0)));+}+/// The user answers the open dialog, from the dialog's own thread.fn answer(open: &Open, answer: Option<FileDialogAnswer>) {let (_, reply) = open.borrow_mut().pop().expect("a dialog is open");diff --git a/crates/ui-egui/src/jpeg_options_ui.rs b/crates/ui-egui/src/jpeg_options_ui.rsindex 107f478a7..a6bd37fb4 100644--- a/crates/ui-egui/src/jpeg_options_ui.rs+++ b/crates/ui-egui/src/jpeg_options_ui.rs@@ -12,8 +12,9 @@//! Any save can ask: check [`wants_prompt`], then [`park`] (which starts where//! [`Start::for_active`] says) or [`park_at`] with a [`Start`] of its own, and return. The answer//! is written by [`confirm`] and journalled as the `record` command passed to `park`: with its-//! path, `quality` (0–12, left out when the file's own quality was kept) and `jpegFormat`, or with-//! no params for a plain `file.save`, which writes whichever file an action is played on.+//! path, `quality` (0–12, left out when the file's own quality was kept) and `jpegFormat`, or+//! without the path for a plain `file.save`, which writes whichever file an action is played on+//! ([`plain_save_step`]).use photocraft_codecs::JpegFormat;use serde_json::Value;@@ -68,6 +69,23 @@ pub fn choice_param(params: &Value, cmd: &str) -> Result<Option<JpegChoice>, StrOk((quality.is_some() || format.is_some()).then(|| JpegChoice { quality, format: format.unwrap_or_default() }))}+/// The params of a recorded plain `file.save` (which writes whichever file an action is played+/// on): none, unless JPEG Options changed something, so that a replay writes it too without+/// asking. `quality` is left out when the file's own quality was kept, and `jpegFormat` when it+/// is Baseline, the default.+pub fn plain_save_step(choice: Option<JpegChoice>) -> Value {+ let mut step = Value::Object(Default::default());+ if let Some(c) = choice {+ if let Some(q) = c.quality {+ step["quality"] = q.into();+ }+ if c.format != JpegFormat::Baseline {+ step["jpegFormat"] = c.format.name().into();+ }+ }+ step+}+/// The JPEG Options in a recorded save step, which replays without asking. A `file.saveACopy`/// step from the engine command keeps its params as they were given, and the engine clamps a/// `quality` to 0–12 and ignores one that isn't a number, so the replay reads it the same way. A@@ -231,8 +249,8 @@ pub fn confirm(app: &mut PhotocraftApp, ctx: &egui::Context) {let own = p.own_quality.filter(|&q| quality_from_percent(u32::from(q)) == p.quality);let choice = JpegChoice { quality: own.is_none().then_some(p.quality), format: p.format };let step = if p.record == "file.save" {- // A plain Save writes whichever file the action is played on (#3531).- Value::Object(Default::default())+ // A plain Save writes whichever file the action is played on (#3531), with the answer.+ plain_save_step(Some(choice))} else {let mut step = serde_json::json!({"path": p.path});choice.record(&mut step);diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 00950a5de..d22adffd1 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1591,11 +1591,17 @@ impl PhotocraftApp {/// File › Save back to the document's own layered file, recorded as a `file.save` step.pub(crate) fn save_in_place(&mut self, path: String) -> Result<Value, String> {+ self.save_in_place_with(path, SaveChoices::default())+ }++ /// [`Self::save_in_place`] with JPEG Options already answered (a replayed or scripted Save),+ /// recorded with them ([`jpeg_options_ui::plain_save_step`]).+ pub(crate) fn save_in_place_with(&mut self, path: String, choices: SaveChoices) -> Result<Value, String> {if self.save_options_open() {return Err(SAVE_OPTIONS_OPEN.into());}- let r = self.write_save(path, None, SaveChoices::default())?;- self.session.journal.push(("file.save".into(), Value::Object(Default::default())));+ let r = self.write_save(path, None, choices)?;+ self.session.journal.push(("file.save".into(), jpeg_options_ui::plain_save_step(choices.jpeg)));Ok(r)}@@ -1649,7 +1655,8 @@ impl PhotocraftApp {let path = st.path.clone().filter(|p| photocraft_engine::file_cmds::holds_at(&st.doc, p) || flat_own(p)).ok_or("Save writes back only to the document's own PSD, PSB, .pcraft or OpenRaster file (OpenRaster only when it holds everything), or to its own flat file while the document is a single plain layer",)?;- return self.save_in_place(path);+ // A recorded JPEG Options answer writes the same way, without asking.+ return self.save_in_place_with(path, SaveChoices::from_step(params, id)?);}let path = params.get("path").and_then(Value::as_str).filter(|p| !p.is_empty()).ok_or("Save As needs a `path`")?;let choices = SaveChoices::from_step(params, id)?;