PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(actions): record File › Export › Export As and replay it with its destination and settings (#7207)
Korg1chd93458b72b21
diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rsindex 2cc9c1f29..69f8d3f4e 100644--- a/crates/engine/src/actions_cmds.rs+++ b/crates/engine/src/actions_cmds.rs@@ -121,11 +121,11 @@ pub fn replayable(id: &str) -> bool {&& (shell_view_command(id) || shell_save_command(id) || crate::commands::find(id).is_some_and(|c| c.journal)))}-/// File › Save, Save As and Save a Copy, recorded by the desktop shell once the save has a path-/// (#2032, #4373). The shell replays them; batch and droplets write to their destination folder-/// instead.+/// File › Save, Save As and Save a Copy, and File › Export › Export As, recorded by the desktop+/// shell once the file has a path (#2032, #4373, #7207). The shell replays them; batch and+/// droplets write to their destination folder instead.pub fn shell_save_command(id: &str) -> bool {- matches!(id, "file.save" | "file.saveAs" | "file.saveACopy")+ matches!(id, "file.save" | "file.saveAs" | "file.saveACopy" | "file.export.exportAs")}/// View-only menu commands recorded by the desktop shell. Headless playback reportsdiff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rsindex 103b36a06..2f9bdd2a9 100644--- a/crates/ui-egui/src/export_dialog.rs+++ b/crates/ui-egui/src/export_dialog.rs@@ -22,71 +22,68 @@ const FORMATS: [(&str, &str); 6] = [("png", "PNG"), ("jpg", "JPG"), ("webp", "We/// filter dialogs do.pub(crate) const COMMAND: &str = "file.export.exportAs";+/// The dialog's own settings: what Export As remembers for next time and what an action records+/// with the destination (#7207).+const SETTINGS: [&str; 14] = [+ "format",+ "quality",+ "lossless",+ "transparency",+ "scale",+ "resample",+ "metadata",+ "tgaBits",+ "sizing",+ "width",+ "height",+ "lockAspect",+ "profile",+ "embedProfile",+];++fn settings_of(f: &Map<String, Value>) -> Map<String, Value> {+ SETTINGS.iter().filter_map(|k| f.get(*k).map(|v| ((*k).to_string(), v.clone()))).collect()+}++/// Whether `v` is a value the dialog's `key` control can hold: what a remembered setting is+/// seeded from and what a recorded Export As step replays with (#7207).+fn valid_setting(key: &str, v: &Value) -> bool {+ let num = |lo: f64, hi: f64| v.as_f64().is_some_and(|x| x.is_finite() && (lo..=hi).contains(&x));+ let one_of = |options: &[&str]| v.as_str().is_some_and(|s| options.contains(&s));+ match key {+ "format" => v.as_str().is_some_and(|s| FORMATS.iter().any(|(k, _)| *k == s)),+ "quality" => num(1.0, 100.0),+ "scale" => num(1.0, 1000.0),+ "width" | "height" => num(1.0, MAX_PX),+ "lossless" | "transparency" | "lockAspect" | "embedProfile" => v.is_boolean(),+ "resample" => v.as_str().is_some_and(|s| export_resample(s).is_ok()),+ "metadata" => one_of(&["none", "exif", "all"]),+ "tgaBits" => one_of(&["auto", "24", "32"]),+ "sizing" => one_of(&["scale", "pixels"]),+ "profile" => one_of(&["document", "srgb"]),+ _ => false,+ }+}+/// The dialog is seeded from the settings it was used with last (see [`COMMAND`]); a value that/// no longer matches a control (a hand-edited preferences file) is left at its default.fn seed_remembered(app: &PhotocraftApp, f: &mut Map<String, Value>) {let Some(Value::Object(saved)) = app.session.prefs().dialogs.get(COMMAND) else { return };- if let Some(v) = saved.get("format").and_then(Value::as_str).filter(|v| FORMATS.iter().any(|(k, _)| k == v)) {- f.insert("format".into(), json!(v));- }- if let Some(v) = saved.get("quality").and_then(Value::as_f64).filter(|v| (1.0..=100.0).contains(v)) {- f.insert("quality".into(), json!(v));- }- if let Some(v) = saved.get("scale").and_then(Value::as_f64).filter(|v| (1.0..=1000.0).contains(v)) {- f.insert("scale".into(), json!(v));- }- for key in ["lossless", "transparency"] {- if let Some(v) = saved.get(key).and_then(Value::as_bool) {- f.insert(key.into(), json!(v));- }- }- if let Some(v) = saved.get("metadata").and_then(Value::as_str).filter(|v| ["none", "exif", "all"].contains(v)) {- f.insert("metadata".into(), json!(v));- }- if let Some(v) = saved.get("resample").and_then(Value::as_str).filter(|v| export_resample(v).is_ok()) {- f.insert("resample".into(), json!(v));- }- if let Some(v) = saved.get("tgaBits").and_then(Value::as_str).filter(|v| ["auto", "24", "32"].contains(v)) {- f.insert("tgaBits".into(), json!(v));- }- if let Some(v) = saved.get("sizing").and_then(Value::as_str).filter(|v| ["scale", "pixels"].contains(v)) {- f.insert("sizing".into(), json!(v));- }- if let Some(v) = saved.get("lockAspect").and_then(Value::as_bool) {- f.insert("lockAspect".into(), json!(v));- }- if let Some(v) = saved.get("embedProfile").and_then(Value::as_bool) {- f.insert("embedProfile".into(), json!(v));- }- if let Some(v) = saved.get("profile").and_then(Value::as_str).filter(|v| ["document", "srgb"].contains(v)) {- f.insert("profile".into(), json!(v));- }- for key in ["width", "height"] {- if let Some(v) = saved.get(key).and_then(Value::as_f64).filter(|v| v.is_finite() && (1.0..=MAX_PX).contains(v)) {- f.insert(key.into(), json!(v.round()));- }+ for key in SETTINGS {+ let Some(v) = saved.get(key).filter(|v| valid_setting(key, v)) else { continue };+ // Numbers as the dialog's fields hold them; pixel sizes are whole pixels.+ let v = match (key, v.as_f64()) {+ ("width" | "height", Some(x)) => json!(x.round()),+ (_, Some(x)) => json!(x),+ _ => v.clone(),+ };+ f.insert(key.into(), v);}}/// Record the settings for the next Export As (see [`seed_remembered`]).fn remember(app: &mut PhotocraftApp, f: &Map<String, Value>) {- const KEYS: [&str; 14] = [- "format",- "quality",- "lossless",- "transparency",- "scale",- "resample",- "metadata",- "tgaBits",- "sizing",- "width",- "height",- "lockAspect",- "profile",- "embedProfile",- ];- let saved: Map<String, Value> = KEYS.iter().filter_map(|k| f.get(*k).map(|v| ((*k).to_string(), v.clone()))).collect();+ let saved = settings_of(f);app.session.prefs.edit(|p| {p.dialogs.insert(COMMAND.into(), Value::Object(saved));});@@ -921,23 +918,53 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value,let stem = doc.name.rsplit_once('.').map_or(doc.name.as_str(), |(a, _)| a).to_string();let ext = s_fmt(f);let suggested = format!("{stem}.{ext}");- let (f, settings) = (f.clone(), settings(f));+ let f = f.clone();app.pick_save(&suggested, move |app, path| {- let out = export_document(&doc, &f, None)?;- let export = app.services.export.as_ref().ok_or("no exporter configured")?;- let (bytes, warnings) = export(&out, &path, &settings)?;- let write = app.services.write.as_mut().ok_or("no writer configured")?;- write(&path, &bytes)?;- let template = "Exported {path} ({size})";- app.ui.status = crate::i18n::fmt(tl!(template), &[("path", &path), ("size", &crate::sizing::human_bytes(bytes.len() as f64))]);- app.ui.status_error = false;- let template = "Exported {path}";- let what = crate::i18n::fmt(tl!(template), &[("path", &crate::file_open::display_name(&path))]);- crate::notices::io_warnings(app, &what, &warnings);- Ok(json!({"path": path, "bytes": bytes.len(), "warnings": warnings}))+ let r = write_export(app, &doc, &f, &path)?;+ // An action being recorded keeps a whole-document export with its destination and+ // settings (#7207). A layer's export isn't recorded: its layer is this document's.+ if !f.contains_key("__layer") {+ let mut step = settings_of(&f);+ step.insert("path".into(), json!(path));+ app.session.journal.push((COMMAND.into(), Value::Object(step)));+ }+ Ok(r)})}+/// Exports `doc` to `path` with the dialog settings `f` (Export As, and its replayed action step).+fn write_export(app: &mut PhotocraftApp, doc: &Document, f: &Map<String, Value>, path: &str) -> Result<Value, String> {+ let out = export_document(doc, f, None)?;+ let export = app.services.export.as_ref().ok_or("no exporter configured")?;+ let (bytes, warnings) = export(&out, path, &settings(f))?;+ let write = app.services.write.as_mut().ok_or("no writer configured")?;+ write(path, &bytes)?;+ let template = "Exported {path} ({size})";+ app.ui.status = crate::i18n::fmt(tl!(template), &[("path", path), ("size", &crate::sizing::human_bytes(bytes.len() as f64))]);+ app.ui.status_error = false;+ let template = "Exported {path}";+ let what = crate::i18n::fmt(tl!(template), &[("path", &crate::file_open::display_name(path))]);+ crate::notices::io_warnings(app, &what, &warnings);+ Ok(json!({"path": path, "bytes": bytes.len(), "warnings": warnings}))+}++/// Replays a recorded Export As step (#7207): the active document exported to the recorded `path`+/// with the recorded settings, without the dialog. The remembered dialog settings are untouched.+pub(crate) fn replay(app: &mut PhotocraftApp, params: &Value) -> Result<Value, String> {+ let path = params.get("path").and_then(Value::as_str).filter(|p| !p.is_empty()).ok_or("Export As needs a `path`")?;+ let f = params.as_object().map(settings_of).unwrap_or_default();+ if let Some(format) = f.get("format").filter(|v| !valid_setting("format", v)) {+ return Err(format!("Export As can't write the format {format}"));+ }+ // A setting out of the dialog's range or of the wrong type is an error, not quietly its+ // default: the step would otherwise write something other than what was recorded.+ if let Some((key, v)) = f.iter().find(|(k, v)| !valid_setting(k, v)) {+ return Err(format!("Export As: `{key}` can't be {v}"));+ }+ let doc = app.session.active().ok_or("no document")?.doc.clone();+ write_export(app, &doc, &f, path)+}+/// [`confirm`] for several layers: one destination prompt, then each layer exported with the/// dialog's settings and named as Quick Export names a selection's files. The layers are captured/// when asked, so changing the selection or document meanwhile can't change what is exported.diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 8784800dd..6e4e0c75e 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1666,8 +1666,9 @@ impl PhotocraftApp {}}- /// Replays a recorded `file.save` / `file.saveAs` / `file.saveACopy` step: no dialog or prompt, and the- /// recorded TIFF, Targa or DDS answer (if any) stands in for the TIFF, Targa or DDS Options prompt.+ /// Replays a recorded `file.save` / `file.saveAs` / `file.saveACopy` / `file.export.exportAs`+ /// step: no dialog or prompt, and the recorded TIFF, Targa or DDS answer (if any) stands in for+ /// the TIFF, Targa or DDS Options prompt.pub(crate) fn replay_save(&mut self, id: &str, params: &Value) -> Result<Value, String> {if self.save_options_open() {return Err("Answer the TIFF, Targa or DDS Options before starting another save".into());@@ -1689,6 +1690,10 @@ impl PhotocraftApp {)?;return self.save_in_place(path, choices);}+ if id == crate::export_dialog::COMMAND {+ // Export As has its own settings, its TGA bits included (#7207).+ return crate::export_dialog::replay(self, params);+ }let path = params.get("path").and_then(Value::as_str).filter(|p| !p.is_empty()).ok_or("Save As needs a `path`")?;let choices = FormatChoices::from_params(params, id)?;if id == "file.saveACopy" {