Actions: a recorded File > Save of a flat file saves the file it is played on (#3531) (#3537)

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Valentin KleinandClaude Opus 5.5 authored and GitHub committed 2026-10-14 13:28:11 -04:00
1 parent 6a766345d3
commit e51ba6720d
3 files changed
+117 -6

No files matched your search

+75
View File
@@ -1318,6 +1318,81 @@ mod tests {
assert_eq!(app.session.active().unwrap().path.as_deref(), Some("out/result.psd"));
}
/// An app with the real importer/exporter and writer whose Save dialogs answer `answers`.
fn disk_app(answers: Vec<Option<crate::file_dialog::FileDialogAnswer>>) -> PhotocraftApp {
let services = crate::Services {
import: Some(Box::new(|name, bytes, depth| {
photocraft_io::import_with_svg_group_depth(name, bytes, depth).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string())
})),
export: Some(Box::new(|doc, path, _| {
photocraft_io::export(doc, path, &Default::default()).map(|r| (r.bytes, r.warnings)).map_err(|e| e.to_string())
})),
write: Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))),
file_dialog: Some(crate::file_dialog::fake(answers).0),
..Default::default()
};
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);
app.background_jobs = false;
app
}
/// Records Invert + File › Save on the file `a`, then plays it on the file `b` (given a
/// new layer first when `layered`). Returns the played result and whether `b` / `a`
/// changed on disk.
fn record_save_on_a_play_on_b(ext: &str, layered: bool) -> (Value, bool, bool) {
use crate::file_dialog::FileDialogAnswer::SaveTo;
let dir = std::env::temp_dir().join(format!("photocraft-rec-save-{ext}-{layered}-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = |n: &str| dir.join(format!("{n}.{ext}")).to_string_lossy().into_owned();
let (a, b) = (path("a"), path("b"));
// The dialog confirms the suggested name, which is the document's own file.
let mut app = disk_app(vec![Some(SaveTo(a.clone())), Some(SaveTo(a.clone()))]);
let ctx = egui::Context::default();
for p in [&a, &b] {
app.run("file.new", json!({"width": 16, "height": 16})).unwrap();
app.save_as(Some(p.clone())).unwrap();
}
let (before_a, before_b) = (std::fs::read(&a).unwrap(), std::fs::read(&b).unwrap());
app.open_path(&a).unwrap();
app.run("actions.record", json!({})).unwrap();
app.run("image.adjustments.invert", json!({})).unwrap();
crate::menus::invoke(&mut app, &ctx, "file.save", json!({})).unwrap();
app.poll_file_dialog(&ctx, None);
app.run("actions.stop", json!({})).unwrap();
std::fs::write(&a, &before_a).unwrap();
app.open_path(&b).unwrap();
if layered {
app.run("layer.new.layer", json!({})).unwrap();
}
let played = app.run("actions.play", json!({"action": 0})).unwrap();
let r = (played, std::fs::read(&b).unwrap() != before_b, std::fs::read(&a).unwrap() != before_a);
let _ = std::fs::remove_dir_all(&dir);
r
}
#[test]
fn recorded_save_overwrites_the_layered_file_the_action_is_played_on() {
let (played, b_changed, a_changed) = record_save_on_a_play_on_b("psd", false);
assert!(played.get("failed").is_none(), "{played}");
assert!(b_changed && !a_changed);
}
#[test]
fn recorded_save_overwrites_the_flat_file_the_action_is_played_on() {
let (played, b_changed, a_changed) = record_save_on_a_play_on_b("png", false);
assert!(played.get("failed").is_none(), "{played}");
assert!(b_changed, "Save did not overwrite the file the action was played on");
assert!(!a_changed, "Save wrote the file the action was recorded on");
}
#[test]
fn recorded_save_never_flattens_layers_over_a_flat_file() {
// The played-on document has layers its PNG can't keep: the step fails, nothing is written (#3806).
let (played, b_changed, a_changed) = record_save_on_a_play_on_b("png", true);
assert!(played.get("failed").is_some(), "{played}");
assert!(!b_changed && !a_changed);
}
#[test]
fn set_folders_group_actions_and_collapse() {
use egui_kittest::{Harness, kittest::Queryable};
+39 -5
View File
@@ -1319,6 +1319,13 @@ impl PhotocraftApp {
/// [`Self::save_as`], writing a Targa with `tga_bits` per pixel when given (no Targa Options
/// prompt): `file.save` and `file.saveAs` with `tgaBits`.
pub fn save_as_with(&mut self, path: Option<String>, tga_bits: Option<photocraft_io::TgaBits>) -> Result<Value, String> {
self.save_dialog_flow(path, tga_bits, false)
}
/// [`Self::save_as_with`]. `plain_save` marks File › Save that had to ask for a file name
/// (a flat file): confirming the document's own file is recorded as a `file.save` step,
/// which writes whichever file the action is played on, not as a Save As to a fixed path.
pub(crate) fn save_dialog_flow(&mut self, path: Option<String>, tga_bits: Option<photocraft_io::TgaBits>, plain_save: bool) -> Result<Value, String> {
if self.save_options_open() {
return Err("Answer TIFF or Targa Options before starting another save".into());
}
@@ -1358,7 +1365,16 @@ impl PhotocraftApp {
}
};
let doc = st.doc.id;
self.pick_save(&suggested, move |app, path| app.with_document(doc, |app| app.save_to(path, tga_bits)))
self.pick_save(&suggested, move |app, path| {
app.with_document(doc, |app| {
let own = app.session.active().and_then(|st| st.path.clone());
let r = app.save_to(path.clone(), tga_bits)?;
if plain_save && own.as_deref() == Some(path.as_str()) {
app.journal_as_plain_save(&path);
}
Ok(r)
})
})
}
/// [`Self::save_as_with`] once the path is known.
@@ -1405,6 +1421,17 @@ impl PhotocraftApp {
Ok(r)
}
/// Re-label the Save As just journaled for `path` as File › Save (see [`Self::save_dialog_flow`]).
fn journal_as_plain_save(&mut self, path: &str) {
if let Some(last) = self.session.journal.last_mut()
&& last.0 == "file.saveAs"
&& last.1.get("path").and_then(Value::as_str) == Some(path)
&& last.1.as_object().is_some_and(|o| o.len() == 1)
{
*last = ("file.save".into(), Value::Object(Default::default()));
}
}
/// 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<Value, String> {
@@ -1412,11 +1439,18 @@ impl PhotocraftApp {
return Err("Answer TIFF or Targa Options before starting another save".into());
}
if id == "file.save" {
// The file the action is played on: one that holds the whole document, or a flat
// file a recorded Save confirmed in the dialog while the document is still a plain
// raster, so it is never flattened over (#3531, #3806).
let st = self.session.active().ok_or("no document")?;
let path =
st.path.clone().filter(|p| photocraft_engine::file_cmds::holds_at(&st.doc, p)).ok_or(
"Save writes back only to the document's own PSD, PSB, .pcraft or OpenRaster file, and to OpenRaster only when it holds everything",
)?;
let flat_own = |p: &str| {
!photocraft_engine::file_cmds::saves_in_place(p)
&& plain_raster(&st.doc).is_some()
&& photocraft_engine::file_cmds::extension(p).is_some_and(|e| photocraft_codecs::from_extension(&e).is_some_and(|f| f.caps().write))
};
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);
}
let path = params.get("path").and_then(Value::as_str).filter(|p| !p.is_empty()).ok_or("Save As needs a `path`")?;
+3 -1
View File
@@ -325,7 +325,9 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
crate::notices::post(app, title, lines, false, None);
app.save_as_with(None, tga_bits)
}
_ => app.save_as_with(None, tga_bits),
// A flat file asks for its name; confirming its own file records a
// plain Save that writes the file an action is played on (#3531).
_ => app.save_dialog_flow(None, tga_bits, true),
}
}
}