PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge main into #3283
aiongg3458eee8611a
diff --cc crates/ui-egui/src/dialogs.rsindex e9d463d4a,3fe62df42..8bc204944--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@@ -656,13 -656,11 +660,17 @@@ pub fn confirm(app: &mut PhotocraftAppapp.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 (cmd, params) = match crate::smart_ui::confirm_command(app, &d.fields, cmd, crate::filter_dialog::params_of(&d.fields)) {+ Ok(edit) => edit,+ Err(e) => {+ app.filter_preview = None;+ return Err(e);+ }+ };let result = app.run(&cmd, params.clone());// A filter that runs as a background job keeps its preview on screen until the job// lands (`canvas::committed_filter_preview`) instead of flashing the unfiltered image.diff --cc crates/ui-egui/src/filter_dialog.rsindex a03204c2e,e2e9a3523..7880579b0--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@@ -684,6 -684,8 +684,8 @@@ fn uses_angle_dial(command: &str, key:/// eyedropper like Photoshop's. While armed, the next click on the image writes the merged/// composite colour into the `color` field (the same sampler the Color Picker uses).const REPLACE_COLOR: &str = "image.adjustments.replaceColor";-/// What OK on an edited smart filter runs (`smart_ui::confirm_command`); its preview too.++/// What OK on an edited smart filter runs (`smart_ui::confirm_command`).+ const SMART_FILTER_SET_PARAMS: &str = "layer.smartFilter.setParams";const COLOR_EYEDROPPER: &str = "__colorEyedropper";fn color_eyedropper_owns(f: &Map<String, Value>) -> bool {@@@ -1004,6 -1006,45 +1006,45 @@@ pub fn params_of(f: &Map<String, 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<String, Value>) -> Result<Value, String> {+ 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 (cmd, params) = crate::smart_ui::confirm_command(app, &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<photocraft_doc::LayerId>, command: &str, params: &Value, k: u32) -> Option<Document> {preview_document_with(doc, active, command, params, k, None)@@@ -1052,31 -1093,10 +1093,34 @@@ pub(crate) fn preview_document_for_targreturn crate::select_mask_view::preview_document(doc, active, params, k, cancel);}// Warp Text renders the type again from the layer model (font size, position), which a- // reduced proxy doesn't scale: it runs at full size and the result is reduced (#218). So does- // a smart filter being edited: the smart object renders again from its source (#6324).- let (run_k, reduce_k) = if command == "type.warpText" || command == SMART_FILTER_SET_PARAMS { (1, k) } else { (k, 1) };- let proxy = crate::proxy::proxy_document(doc, run_k);+ // reduced proxy doesn't scale: it runs at full size and the result is reduced (#218).+ let (run_k, reduce_k) = if command == "type.warpText" { (1, k) } else { (k, 1) };+ let mut proxy = crate::proxy::proxy_document(doc, run_k);+ let mut p = params.clone();+ if is_smart_edit(command) {- let layer = photocraft_doc::LayerId(params.get("layer")?.as_u64()?);++ let layer = match params.get("layer") {++ Some(id) => photocraft_doc::LayerId(id.as_u64()?),++ None => active?,++ };+ let index = usize::try_from(params.get("index")?.as_u64()?).ok()?;+ let photocraft_doc::LayerContent::Smart(sm) = &mut proxy.layer_mut(layer)?.content else { return None };+ // Proxy_document scales the cache, placement and shared mask (#5766). Editing a filter+ // re-renders its source: scale every filter's lengths and the mask's feather as well.+ if run_k > 1 {+ if let Some(mask) = &mut sm.filter_mask {+ mask.feather /= run_k as f32;+ }+ for f in &mut sm.smart_filters {+ scale_preview_params(&f.command, &mut f.params, run_k);+ }+ if command == "layer.smartFilter.setParams" {+ let f = sm.smart_filters.get(index)?;+ scale_preview_params(&f.command, p.get_mut("params")?, run_k);+ }+ }+ } else {+ scale_preview_params(command, &mut p, run_k);+ }let mut s = photocraft_engine::Session::new();s.color.settings = color.clone();s.set_inline_job_ctx(cancel.cloned());