Address CodeRabbit review: never save Apply Data Set's Preview

File > Save and the unsaved-changes prompt's Save write through
invoke_unguarded without run_command, so they skipped end_preview and
could write the previewed data set into the file. invoke_unguarded now
ends the preview first; the dialog shows it again on the next frame.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-16 01:31:42 +01:00
1 parent 1d4c4e2d6b
commit c7544cd080
2 files changed
+53

No files matched your search

+3
View File
@@ -398,6 +398,9 @@ fn invoke_file_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str,
/// Dispatch UI actions and dialogs, falling back to the engine for other commands.
pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
// File › Save and the unsaved-changes prompt's Save write from here without `run`: they must
// not write Apply Data Set's Preview either. The dialog shows it again on the next frame.
crate::panels::apply_data_set_ui::end_preview(app);
if let Some(result) = crate::smart_svg_ui::intercept(app, id, &params) {
return result;
}
@@ -109,6 +109,7 @@ fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
return;
}
photocraft_ui_egui::dialogs::show(app, &ctx);
photocraft_ui_egui::discard_ui::show(app, &ctx);
},
app,
);
@@ -418,3 +419,52 @@ fn autosave_waits_while_previewing() {
assert_eq!(saved.len(), 1);
assert_eq!(*saved[0], *before, "the document itself");
}
/// Services that keep every document they are asked to encode, and a document with unsaved work
/// and its own file, so File › Save writes it back in place.
fn saving_app() -> (PhotocraftApp, Layers, Arc<std::sync::Mutex<Vec<Document>>>) {
let encoded: Arc<std::sync::Mutex<Vec<Document>>> = Default::default();
let log = encoded.clone();
let services = photocraft_ui_egui::Services {
export: Some(Box::new(move |d: &Document, _path: &str, _settings: &photocraft_ui_egui::ExportSettings| {
log.lock().unwrap().push(d.clone());
Ok((Vec::new(), Vec::new()))
})),
write: Some(Box::new(|_path: &str, _bytes: &[u8]| Ok(()))),
..Default::default()
};
let (mut a, l) = app_with(services);
let dir = std::env::temp_dir().join(format!("pc-apply-data-set-preview-{}-save", std::process::id()));
a.session.active_mut().unwrap().path = Some(dir.join("own.psd").to_string_lossy().into_owned());
a.run("layer.new.layer", json!({"name": "Work"})).unwrap();
(a, l, encoded)
}
/// Save on the unsaved-changes prompt (closing the document or quitting while the dialog shows a
/// set) and File › Save run from the menu or the control channel write the document itself,
/// never the preview.
#[test]
fn saving_while_previewing_writes_the_document_itself() {
for via_prompt in [true, false] {
let (a, l, encoded) = saving_app();
let mut h = harness(a);
let before = doc(&h);
open(&mut h);
click(&mut h, "Preview");
assert_eq!(shown(&h, &l).1, "Alpha");
let ctx = h.ctx.clone();
if via_prompt {
photocraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "file.close", json!({})).unwrap();
h.run_steps(3);
assert!(photocraft_ui_egui::discard_ui::inspect(h.state()).is_some(), "closing unsaved work asks first");
// Save: Yes on Windows and Linux, S on macOS; the test context is not macOS.
h.key_press(egui::Key::Y);
h.run_steps(3);
} else {
photocraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "file.save", json!({})).unwrap();
}
let encoded = encoded.lock().unwrap();
assert_eq!(encoded.len(), 1, "via_prompt={via_prompt}");
assert_eq!(encoded[0], *before, "via_prompt={via_prompt}: the document itself, nothing of Zed");
}
}