diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs index 3ee242d7d6..3b3ffd1f01 100644 --- a/crates/engine/src/actions_cmds.rs +++ b/crates/engine/src/actions_cmds.rs @@ -115,10 +115,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 and Save As, recorded by the desktop shell once the save has a path (#2032). -/// The shell replays them; batch and droplets write to their destination folder instead. +/// 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. pub fn shell_save_command(id: &str) -> bool { - matches!(id, "file.save" | "file.saveAs") + matches!(id, "file.save" | "file.saveAs" | "file.saveACopy") } /// View-only menu commands recorded by the desktop shell. Headless playback reports diff --git a/crates/ui-egui/src/actions.rs b/crates/ui-egui/src/actions.rs index 7902a39bba..e0f3713f2e 100644 --- a/crates/ui-egui/src/actions.rs +++ b/crates/ui-egui/src/actions.rs @@ -37,10 +37,12 @@ struct RenameAction { focused: bool, } -fn label_of(id: &str) -> String { +pub(crate) fn label_of(id: &str) -> String { photocraft_engine::commands::find(id) .map(|c| c.label) .or_else(|| crate::menus::UI_COMMANDS.iter().find(|c| c.0 == id).map(|c| c.1)) + // Shell menu items recorded as steps, like Save a Copy (#4373). + .or_else(|| crate::menu_catalog::CATALOG.iter().find(|c| c.3 == id).map(|c| c.1)) .map(|label| label.trim_end_matches('…').to_string()) .unwrap_or_else(|| id.to_string()) } diff --git a/crates/ui-egui/src/discard_ui/tests/targa_tests.rs b/crates/ui-egui/src/discard_ui/tests/targa_tests.rs index e4c83bda64..565fe01037 100644 --- a/crates/ui-egui/src/discard_ui/tests/targa_tests.rs +++ b/crates/ui-egui/src/discard_ui/tests/targa_tests.rs @@ -113,7 +113,7 @@ fn save_a_copy_to_a_targa_asks_and_keeps_the_document_unsaved() { crate::menus::invoke(h.state_mut(), &ctx, "file.saveACopy", json!({})).unwrap(); h.run_steps(3); assert_eq!(h.state().targa_options.as_ref().map(|p| (p.bits, p.copy)), Some((TgaBits::Bits32, true))); - assert!(targa_options_ui::park(h.state_mut(), "other.tga".into(), false, true).is_err(), "the waiting prompt is kept"); + assert!(targa_options_ui::park(h.state_mut(), "other.tga".into(), false, "file.saveAs").is_err(), "the waiting prompt is kept"); assert_eq!(h.state().targa_options.as_ref().map(|p| p.path.as_str()), Some("copy.tga")); h.key_press(Key::Enter); h.run_steps(3); @@ -140,8 +140,8 @@ fn targa_options_keeps_a_layered_save_as_a_copy() { assert_eq!(st.path, None); } -/// An action being recorded keeps a Targa Save As with its bits, answered in Targa Options or -/// scripted (#2032), and plays it back without asking; the dialog's Save a Copy isn't recorded. +/// An action being recorded keeps a Targa Save As or Save a Copy with its bits, answered in Targa +/// Options or scripted (#2032, #4373), and plays them back without asking. #[test] fn recorded_targa_save_as_keeps_its_bits_and_replays_without_asking() { let (mut h, writes) = targa_app(true, "copy.tga"); @@ -164,12 +164,22 @@ fn recorded_targa_save_as_keeps_its_bits_and_replays_without_asking() { &[("t.tga".to_string(), asked(Some(24))), ("copy.tga".to_string(), asked(Some(32))), ("s.tga".to_string(), asked(Some(32)))] ); let steps = &h.state().session.actions.list[0].steps; - assert_eq!(steps, &[("file.saveAs".into(), json!({"path": "t.tga", "tgaBits": 24})), ("file.saveAs".into(), json!({"path": "s.tga", "tgaBits": 32}))]); + assert_eq!( + steps, + &[ + ("file.saveAs".into(), json!({"path": "t.tga", "tgaBits": 24})), + ("file.saveACopy".into(), json!({"path": "copy.tga", "tgaBits": 32})), + ("file.saveAs".into(), json!({"path": "s.tga", "tgaBits": 32})) + ] + ); writes.borrow_mut().clear(); let played = h.state_mut().run("actions.play", json!({"action": 0})).unwrap(); assert!(played.get("failed").is_none(), "{played}"); assert!(h.state().targa_options.is_none(), "playback doesn't ask"); - assert_eq!(writes.borrow().as_slice(), &[("t.tga".to_string(), asked(Some(24))), ("s.tga".to_string(), asked(Some(32)))]); + assert_eq!( + writes.borrow().as_slice(), + &[("t.tga".to_string(), asked(Some(24))), ("copy.tga".to_string(), asked(Some(32))), ("s.tga".to_string(), asked(Some(32)))] + ); } /// Closing an unsaved document whose save goes to a Targa waits behind Targa Options, then closes. diff --git a/crates/ui-egui/src/file_open/tests.rs b/crates/ui-egui/src/file_open/tests.rs index 9a833b7ba1..67887877e1 100644 --- a/crates/ui-egui/src/file_open/tests.rs +++ b/crates/ui-egui/src/file_open/tests.rs @@ -777,6 +777,30 @@ fn a_set_of_background_opens_keeps_its_order_however_they_finish() { assert_eq!(doc_names(&app), ["dropped.psd", "existing.psd", "f0.psd", "f1.psd", "f2.psd", "f3.psd", "late.psd"]); } +/// #4373: Save a Copy is recorded in an action, like Save and Save As, and played back as a copy: +/// the document keeps its own file and unsaved changes. +#[test] +fn save_a_copy_is_recorded_and_replayed_as_a_copy() { + let (mut app, written) = app_with(vec![Some(FileDialogAnswer::SaveTo("/pics/copy.png".into()))]); + let ctx = egui::Context::default(); + app.open_file("/pics/work.pcraft", b"x").unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("actions.record", json!({})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + menus::invoke(&mut app, &ctx, "file.saveACopy", json!({})).unwrap(); + answer(&mut app); + app.run("actions.stop", json!({})).unwrap(); + assert_eq!(app.session.actions.list[0].steps.last(), Some(&("file.saveACopy".to_string(), json!({"path": "/pics/copy.png"})))); + assert_eq!(crate::actions::label_of("file.saveACopy"), "Save a Copy"); + written.borrow_mut().clear(); + let played = app.run("actions.play", json!({"action": 0})).unwrap(); + assert!(played.get("failed").is_none(), "{played}"); + assert_eq!(written.borrow().iter().map(|(p, _)| p.as_str()).collect::>(), ["/pics/copy.png"]); + let st = app.session.active().unwrap(); + assert_eq!(st.path.as_deref(), Some("/pics/work.pcraft"), "the document keeps its file"); + assert!(st.is_dirty(), "and its unsaved changes"); +} + /// #4400: a new document saved under a name is listed in Open Recent (and on the Home screen), /// as opening it is; a Save a Copy isn't, since the document doesn't live there. #[test] diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e599845669..96158c024e 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1288,7 +1288,7 @@ impl PhotocraftApp { // which records the step once answered. if tga_bits.is_none() && targa_options_ui::wants_prompt(self, &path) { let copy = self.saves_copy(&path, None)?; - targa_options_ui::park(self, path.clone(), copy, true)?; + targa_options_ui::park(self, path.clone(), copy, "file.saveAs")?; return Ok(serde_json::json!({"path": path, "warnings": []})); } // A layered TIFF asks about its layers first (Preferences › File Handling); the save @@ -1325,7 +1325,7 @@ impl PhotocraftApp { Ok(r) } - /// Replays a recorded `file.save` / `file.saveAs` step: no dialog or prompt, and the + /// Replays a recorded `file.save` / `file.saveAs` / `file.saveACopy` step: no dialog or prompt, and the /// recorded TIFF or Targa answer (if any) stands in for the TIFF or Targa Options prompt. pub(crate) fn replay_save(&mut self, id: &str, params: &Value) -> Result { if self.save_options_open() { @@ -1344,6 +1344,11 @@ impl PhotocraftApp { } let path = params.get("path").and_then(Value::as_str).filter(|p| !p.is_empty()).ok_or("Save As needs a `path`")?; let tga_bits = targa_options_ui::bits_param(params, id)?; + if id == "file.saveACopy" { + // A copy: the document keeps its own file and unsaved state, as when recorded (#4373). + let doc = self.session.active().ok_or("no document")?.doc.clone(); + return crate::view_cmds::write_copy(self, &doc, path, tga_bits); + } self.save_as_step(path.to_string(), params.get("tiffLayers").and_then(Value::as_bool), tga_bits) } diff --git a/crates/ui-egui/src/targa_options_ui.rs b/crates/ui-egui/src/targa_options_ui.rs index afc7f60ba2..c2161a0c9b 100644 --- a/crates/ui-egui/src/targa_options_ui.rs +++ b/crates/ui-egui/src/targa_options_ui.rs @@ -20,9 +20,10 @@ pub struct Prompt { pub bits: TgaBits, /// Save a Copy: the document keeps its path and unsaved changes. pub copy: bool, - /// File › Save As: the answered save is recorded as a `file.saveAs` step, so an action being - /// recorded keeps it (#2032). The dialog's Save a Copy isn't recorded. - pub record: bool, + /// The step the answered save is recorded as, so an action being recorded keeps it: + /// `file.saveAs` for Save As (#2032, even when a flat Targa is written as a copy) and + /// `file.saveACopy` for Save a Copy (#4373). + pub record: &'static str, } /// `true` when `path` names a Targa (`.tga`, and the rarer `.icb`, `.vda`, `.vst`). @@ -43,7 +44,7 @@ pub fn bits_param(params: &serde_json::Value, cmd: &str) -> Result Result<(), String> { +pub fn park(app: &mut PhotocraftApp, path: String, copy: bool, record: &'static str) -> Result<(), String> { if app.save_options_open() { return Err("Answer TIFF or Targa Options before starting another save".into()); } @@ -80,9 +81,9 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) { let settings = ExportSettings { tga_bits: Some(p.bits), ..app.save_settings() }; let step = serde_json::json!({"path": p.path, "tgaBits": p.bits.bits()}); let written = app.with_document(p.doc, |app| app.write_document(p.path, &settings, p.copy)); - if written.is_ok() && p.record { - // An action being recorded keeps the Save As with this answer (#2032). - app.session.journal.push(("file.saveAs".into(), step)); + if written.is_ok() { + // An action being recorded keeps the Save As or Save a Copy with this answer (#2032). + app.session.journal.push((p.record.into(), step)); } match written { Err(e) => { diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rs index 585e5dd074..09d759ed45 100644 --- a/crates/ui-egui/src/view_cmds.rs +++ b/crates/ui-egui/src/view_cmds.rs @@ -1387,7 +1387,8 @@ fn front(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option Result { let doc = app.session.active().ok_or("no document")?.doc.clone(); let stem = doc.name.rsplit_once('.').map_or(doc.name.as_str(), |(a, _)| a); @@ -1398,27 +1399,40 @@ fn save_a_copy(app: &mut PhotocraftApp) -> Result { let parked = app.with_document(doc.id, |app| { let ask = crate::targa_options_ui::wants_prompt(app, &path); if ask { - crate::targa_options_ui::park(app, path.clone(), true, false)?; + crate::targa_options_ui::park(app, path.clone(), true, "file.saveACopy")?; } Ok(ask) })?; if parked { return Ok(json!({"path": path, "warnings": []})); } - let settings = app.save_settings(); - let export = app.services.export.as_ref().ok_or("no exporter configured")?; - let (bytes, warnings) = export(&doc, &path, &settings)?; - let write = app.services.write.as_mut().ok_or("no writer configured")?; - write(&path, &bytes)?; - let template = "Saved a copy as {path}"; - app.ui.status = crate::i18n::fmt(tl!(template), &[("path", &path)]); - app.ui.status_error = false; - 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, "warnings": warnings})) + let r = write_copy(app, &doc, &path, None)?; + app.session.journal.push(("file.saveACopy".into(), json!({"path": path}))); + Ok(r) }) } +/// Writes `doc` to `path` as a copy (Save a Copy, and its replayed action step): the document's +/// path and saved state are untouched. `tga_bits` is the Targa answer, when there is one. +pub(crate) fn write_copy( + app: &mut PhotocraftApp, + doc: &photocraft_doc::Document, + path: &str, + tga_bits: Option, +) -> Result { + let settings = crate::ExportSettings { tga_bits, ..app.save_settings() }; + let export = app.services.export.as_ref().ok_or("no exporter configured")?; + let (bytes, warnings) = export(doc, path, &settings)?; + let write = app.services.write.as_mut().ok_or("no writer configured")?; + write(path, &bytes)?; + let template = "Saved a copy as {path}"; + app.ui.status = crate::i18n::fmt(tl!(template), &[("path", path)]); + app.ui.status_error = false; + 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, "warnings": warnings})) +} + /// Layer › Smart Objects › Export Contents: saves the contents file under its own name by default. fn export_contents(app: &mut PhotocraftApp) -> Result { let st = app.session.active().ok_or("no document")?;