mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(ui): guard Image Processor document sources during transforms
This commit is contained in:
1 parent
36bfc50fd3
commit
12a38e5fd5
2 files changed
+102
No files matched your search
@@ -93,6 +93,18 @@ impl PhotocraftApp {
|
||||
if let Some(result) = smart_svg_ui::intercept(self, id, ¶ms) {
|
||||
return result;
|
||||
}
|
||||
// Document exports read committed pixels, not the pending Free Transform preview.
|
||||
// Check on execution (also dialog OK and automation), leaving Folder processing usable.
|
||||
if id == "file.scripts.imageProcessor"
|
||||
&& self.ui.transform.is_some()
|
||||
&& match params.get("source").and_then(Value::as_str) {
|
||||
Some("open") => true,
|
||||
Some("current") => self.transform_preview.as_ref().is_none_or(|preview| self.session.active().is_some_and(|st| st.doc.id == preview.doc.id)),
|
||||
_ => false,
|
||||
}
|
||||
{
|
||||
return Err("Commit or cancel the transform first".into());
|
||||
}
|
||||
if let Some(r) = transform_tool::intercept(self, id) {
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -1132,6 +1132,96 @@ fn a_refused_commit_keeps_the_box_open() {
|
||||
assert!(app.ui.transform.is_none() && app.transform_preview.is_none(), "another tool ends it");
|
||||
}
|
||||
|
||||
/// Image Processor must not silently export the pixels from before the visible transform.
|
||||
/// Both direct commands and dialog OK preserve the box and write nothing until it is settled.
|
||||
#[test]
|
||||
fn image_processor_document_sources_wait_for_the_transform() {
|
||||
for source in ["current", "open"] {
|
||||
for through_dialog in [false, true] {
|
||||
for apply in [false, true] {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
|
||||
let output = tempfile::tempdir().unwrap();
|
||||
begin(&mut app, &ctx).unwrap();
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = t.quad.map(|[x, y]| [x + 6.0, y]);
|
||||
let quad = t.quad;
|
||||
let before = app.session.active().unwrap().doc.clone();
|
||||
let revision = app.session.active().unwrap().revision;
|
||||
let history = app.session.active().unwrap().history.past_len();
|
||||
let params = json!({"source": source, "output": output.path(), "format": "png"});
|
||||
let result = if through_dialog {
|
||||
let reply = crate::menus::invoke(&mut app, &ctx, "file.scripts.imageProcessor", json!({})).unwrap();
|
||||
let id = reply["dialog"].as_u64().unwrap();
|
||||
let fields = &mut app.ui.dialog_mut(id).unwrap().fields;
|
||||
fields.insert("source".into(), json!(source));
|
||||
fields.insert("output".into(), json!(output.path()));
|
||||
fields.insert("format".into(), json!("png"));
|
||||
crate::dialogs::confirm(&mut app, id)
|
||||
} else {
|
||||
app.run("file.scripts.imageProcessor", params.clone())
|
||||
};
|
||||
assert_eq!(result.unwrap_err(), "Commit or cancel the transform first");
|
||||
assert_eq!(std::fs::read_dir(output.path()).unwrap().count(), 0);
|
||||
assert_eq!(app.ui.transform.as_ref().unwrap().quad, quad);
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(Arc::ptr_eq(&st.doc, &before));
|
||||
assert_eq!((st.revision, st.history.past_len()), (revision, history));
|
||||
if apply {
|
||||
commit(&mut app);
|
||||
} else {
|
||||
cancel(&mut app);
|
||||
}
|
||||
assert!(app.ui.transform.is_none());
|
||||
let result = app.run("file.scripts.imageProcessor", params).unwrap();
|
||||
assert_eq!(result["files"].as_array().unwrap().len(), 1, "{result}");
|
||||
assert!(result["errors"].as_array().unwrap().is_empty(), "{result}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Folder input, including the legacy omitted source, does not read the open transform.
|
||||
#[test]
|
||||
fn image_processor_folder_source_keeps_working_during_a_transform() {
|
||||
for source in [None, Some("folder")] {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
|
||||
let input = tempfile::tempdir().unwrap();
|
||||
let output = tempfile::tempdir().unwrap();
|
||||
app.run("file.saveACopy", json!({"path": input.path().join("source.png")})).unwrap();
|
||||
begin(&mut app, &ctx).unwrap();
|
||||
let before = app.session.active().unwrap().doc.clone();
|
||||
let mut params = json!({"input": input.path(), "output": output.path(), "format": "png"});
|
||||
if let Some(source) = source {
|
||||
params["source"] = json!(source);
|
||||
}
|
||||
let result = app.run("file.scripts.imageProcessor", params).unwrap();
|
||||
assert_eq!(result["files"].as_array().unwrap().len(), 1, "{result}");
|
||||
assert!(result["errors"].as_array().unwrap().is_empty(), "{result}");
|
||||
assert!(app.ui.transform.is_some());
|
||||
assert!(Arc::ptr_eq(&app.session.active().unwrap().doc, &before));
|
||||
}
|
||||
}
|
||||
|
||||
/// All open documents includes a transform in another tab; Current only reads its own tab.
|
||||
#[test]
|
||||
fn image_processor_checks_the_transform_document_for_each_source() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
|
||||
app.run("file.new", json!({"name": "Other", "width": 16, "height": 16})).unwrap();
|
||||
assert!(app.session.set_active(0));
|
||||
begin(&mut app, &ctx).unwrap();
|
||||
assert!(app.session.set_active(1));
|
||||
let output = tempfile::tempdir().unwrap();
|
||||
let result = app.run("file.scripts.imageProcessor", json!({"source": "open", "output": output.path(), "format": "png"}));
|
||||
assert_eq!(result.unwrap_err(), "Commit or cancel the transform first");
|
||||
assert_eq!(std::fs::read_dir(output.path()).unwrap().count(), 0);
|
||||
let result = app.run("file.scripts.imageProcessor", json!({"source": "current", "output": output.path(), "format": "png"})).unwrap();
|
||||
assert_eq!(result["files"].as_array().unwrap().len(), 1, "{result}");
|
||||
assert!(result["errors"].as_array().unwrap().is_empty(), "{result}");
|
||||
}
|
||||
|
||||
/// #6737: Save for Web exports the committed document, so it is off while a box is pending.
|
||||
#[test]
|
||||
fn save_for_web_is_off_under_a_pending_transform() {
|
||||
|
||||
Reference in new issue
Block a user