diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 01116879cd..3fe62df427 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -656,6 +656,10 @@ pub fn confirm(app: &mut PhotocraftApp, id: u64) -> Result { app.filter_preview = None; return Err("dialog has no command".into()); }; + if crate::filter_dialog::needs_displace_map(&cmd, &crate::filter_dialog::params_of(&d.fields)) { + app.filter_preview = None; + return crate::filter_dialog::ask_displace_map(app, cmd, d.fields.clone()); + } let (cmd, params) = crate::smart_ui::confirm_command(&d.fields, cmd, crate::filter_dialog::params_of(&d.fields)); let result = app.run(&cmd, params.clone()); // A filter that runs as a background job keeps its preview on screen until the job diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs index ad185f315c..e2e9a35234 100644 --- a/crates/ui-egui/src/filter_dialog.rs +++ b/crates/ui-egui/src/filter_dialog.rs @@ -1006,6 +1006,45 @@ pub fn params_of(f: &Map) -> Value { Value::Object(f.iter().filter(|(k, v)| !k.starts_with("__") && !unset(k, v)).map(|(k, v)| (k.clone(), v.clone())).collect()) } +/// Filter › Distort › Displace confirmed with no map: no open document picked and no path typed +/// (#5806). Photoshop asks for the displacement map file after OK; so does PhotoCraft. +pub(crate) fn needs_displace_map(command: &str, params: &Value) -> bool { + command == "filter.distort.displace" + && params.get("mapPath").and_then(Value::as_str).is_none_or(|p| p.trim().is_empty()) + && params.get("mapDocument").and_then(Value::as_i64).is_none_or(|i| i < 0) +} + +/// Asks for the displacement map, then runs Displace with the dialog's values and that file, as +/// OK in a dialog that had one would (smart filters included). Cancel applies nothing. In the +/// browser the picker gives contents, not a path the filter can read: the map has to be opened +/// as a document and picked as Map Document there. It displaces the document that asked, even +/// when another one was activated while the picker was open. +pub(crate) fn ask_displace_map(app: &mut PhotocraftApp, command: String, fields: Map) -> Result { + use crate::file_dialog::{FileDialogAnswer, FileDialogRequest}; + let doc = app.active_doc_id()?; + let initial_dir = crate::file_dialog::last_used_dir(&app.ui.recent_files); + app.ask_file(FileDialogRequest::Open { multiple: false, initial_dir, extensions: None }, move |app, answer| { + let path = match answer { + FileDialogAnswer::Paths(paths) => paths.into_iter().next().ok_or("no displacement map was chosen")?, + FileDialogAnswer::Unavailable(e) => return Err(e), + FileDialogAnswer::Contents(..) | FileDialogAnswer::ContentsMany(_) => { + return Err("open the displacement map as a document, then choose it as the Map Document".into()); + } + FileDialogAnswer::SaveTo(_) | FileDialogAnswer::FolderTo(_) => return Err("unexpected answer from the file dialog".into()), + }; + let mut fields = fields; + fields.insert("mapPath".into(), json!(path)); + app.refocus(doc)?; + let (cmd, params) = crate::smart_ui::confirm_command(&fields, command, params_of(&fields)); + let result = app.run(&cmd, params); + if result.is_ok() { + remember(app, &cmd, &fields); + let _ = crate::actions::resume(app, &fields); + } + result + }) +} + /// Compute a preview document: run `command` with `params` on the proxy (scaled) copy of `doc`. pub fn preview_document(doc: &Document, active: Option, command: &str, params: &Value, k: u32) -> Option { preview_document_with(doc, active, command, params, k, None) @@ -1359,6 +1398,51 @@ mod tests { assert!(app.ui.dialogs.iter().any(|d| d.id == id)); } + /// #5806: OK in Displace with no map asks for the map file, as Photoshop does, and displaces + /// with the chosen file; Cancel in the picker applies nothing; a map already given runs at once. + #[test] + fn displace_asks_for_its_map_after_ok() { + let dir = std::env::temp_dir().join(format!("photocraft-displace-map-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let map = dir.join("map.png"); + let mut m = photocraft_engine::Session::new(); + m.execute("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap(); + std::fs::write(&map, photocraft_io::export(&m.active().unwrap().doc, "map.png", &Default::default()).unwrap().bytes).unwrap(); + let answers = vec![Some(crate::file_dialog::FileDialogAnswer::Paths(vec![map.to_string_lossy().into_owned()])), None]; + let (show, asked) = crate::file_dialog::fake(answers); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services { file_dialog: Some(show), ..Default::default() }); + app.run("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap(); + app.run("select.rect", json!({"x": 0, "y": 0, "width": 8, "height": 16})).unwrap(); + app.run("edit.fill", json!({"color": "#000000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + let steps = app.session.active().unwrap().history.past_len(); + let ctx = egui::Context::default(); + let id = open(&mut app, "filter.distort.displace").unwrap(); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert!(app.file_dialog_open(), "OK leaves the map picker pending"); + // Another document activated while the picker is open is left alone. + app.run("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap(); + let other_steps = app.session.documents()[1].history.past_len(); + // The frame shows the picker (the service is asked then) and acts on its answer. + app.poll_file_dialog(&ctx, None); + assert_eq!(asked.borrow().len(), 1, "OK asks for the map"); + assert_eq!(app.session.active_index(), Some(0), "the document that asked is shown"); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1, "Displace ran with the chosen map"); + assert_eq!(app.session.documents()[1].history.past_len(), other_steps, "not on the other document"); + // Cancelled: nothing is applied. + let id = open(&mut app, "filter.distort.displace").unwrap(); + crate::dialogs::confirm(&mut app, id).unwrap(); + app.poll_file_dialog(&ctx, None); + assert_eq!(asked.borrow().len(), 2); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1); + // A map given in the dialog runs without asking. + assert!(!needs_displace_map("filter.distort.displace", &json!({"mapPath": "/maps/x.psd"}))); + assert!(!needs_displace_map("filter.distort.displace", &json!({"mapDocument": 0}))); + assert!(needs_displace_map("filter.distort.displace", &json!({"mapDocument": -1, "mapPath": " "}))); + assert!(!needs_displace_map("filter.blur.gaussianBlur", &json!({}))); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn remembering_dialogs_never_persists_paths_or_document_indices() { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());