diff --git a/crates/ui-egui/src/dialogs/liquify_ui.rs b/crates/ui-egui/src/dialogs/liquify_ui.rs index 29536064a5..12425ea81a 100644 --- a/crates/ui-egui/src/dialogs/liquify_ui.rs +++ b/crates/ui-egui/src/dialogs/liquify_ui.rs @@ -194,8 +194,9 @@ pub struct LiquifyDialog { pub dab_ms: f64, /// CPU time spent refreshing the zoomed-in source-resolution preview. pub preview_ms: f64, - /// The smart filter being edited: OK updates it instead of adding a new Liquify filter. - smart_filter: Option, + /// The smart filter being edited, with its `smart_filter_dialog::target`: OK checks the target + /// and updates that filter instead of adding a new Liquify filter. + smart_filter: Option<(usize, Value)>, } fn union(a: Option<[usize; 4]>, b: [usize; 4]) -> Option<[usize; 4]> { @@ -233,7 +234,7 @@ impl LiquifyDialog { "dabMs": self.dab_ms, "fullResolutionPreview": self.view_bounds.is_some(), "previewMs": self.preview_ms, - "smartFilter": self.smart_filter, + "smartFilter": self.smart_filter.as_ref().map(|(index, _)| index), }) } @@ -498,7 +499,7 @@ pub fn open(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> /// Re-opens Liquify smart filter `index` of smart object `layer` with its recorded strokes and /// mesh size, previewing on the pixels below it; OK updates that filter -/// (`layer.smartFilter.setParams`). +/// (`layer.smartFilter.setParams`) unless the document or the filter changed in the meantime. pub fn open_smart_filter(app: &mut PhotocraftApp, ctx: &egui::Context, layer: LayerId, index: usize) -> Result<(), String> { use photocraft_engine::distort_cmds::{LIQUIFY, liquify_params}; let st = app.session.active().ok_or("no document")?; @@ -513,7 +514,8 @@ pub fn open_smart_filter(app: &mut PhotocraftApp, ctx: &egui::Context, layer: La let surf = photocraft_engine::smart_cmds::render_below_filter(&st.doc, sm, index) .map_err(|e| e.to_string())? .ok_or("the smart object's contents are unavailable (missing linked file?)")?; - open_on(app, ctx, layer, surf, strokes, cell, Some(index)) + let target = crate::smart_filter_dialog::target(app, layer, index)?; + open_on(app, ctx, layer, surf, strokes, cell, Some((index, target))) } fn open_on( @@ -523,7 +525,7 @@ fn open_on( surf: Surface, strokes: Vec, cell: f64, - smart_filter: Option, + smart_filter: Option<(usize, Value)>, ) -> Result<(), String> { let st = app.session.active().ok_or("no document")?; let canvas = st.doc.bounds(); @@ -593,16 +595,25 @@ pub fn cancel(app: &mut PhotocraftApp) { } /// OK: runs `filter.liquify` with the recorded strokes (one history step). When the dialog edits -/// a smart filter, OK updates that filter instead (also when Restore All left no strokes). The -/// dialog closes either way. `app.run` shows a failed command's error in the status bar, and -/// `commit` returns it. +/// a smart filter, OK updates that filter instead (also when Restore All left no strokes), after +/// `smart_filter_dialog::validate_target` checks that the document and the filter did not change. +/// The dialog closes either way. The status bar shows the error of a failed check or command +/// (`app.run` shows the latter), and `commit` returns it. pub fn commit(app: &mut PhotocraftApp) -> Result<(), String> { let Some(mut d) = app.distort.liquify.take() else { return Ok(()) }; remember(app, &d.opts); d.end(); - if let Some(index) = d.smart_filter { + if let Some((_, target)) = &d.smart_filter { + let (layer, index) = match crate::smart_filter_dialog::validate_target(app, target) { + Ok(edit) => edit, + Err(e) => { + app.ui.status = e.clone(); + app.ui.status_error = true; + return Err(e); + } + }; let params = json!({"strokes": d.strokes, "meshSize": d.cell}); - return app.run("layer.smartFilter.setParams", json!({"layer": d.layer.0, "index": index, "params": params})).map(|_| ()); + return app.run("layer.smartFilter.setParams", json!({"layer": layer.0, "index": index, "params": params})).map(|_| ()); } if d.strokes.is_empty() { return Ok(()); diff --git a/crates/ui-egui/src/dialogs/liquify_ui/tests.rs b/crates/ui-egui/src/dialogs/liquify_ui/tests.rs index 9b50c50677..743c41197d 100644 --- a/crates/ui-egui/src/dialogs/liquify_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/liquify_ui/tests.rs @@ -743,7 +743,7 @@ fn smart_filter_double_click_edits_the_recorded_liquify() { crate::smart_ui::open_editor(&mut app, &ctx, layer.0, &sm, 0); assert!(app.ui.dialogs.is_empty(), "no generic parameter form"); let d = app.distort.liquify.as_ref().expect("the Liquify workspace opens"); - assert_eq!((d.smart_filter, d.cell), (Some(0), 3.0)); + assert_eq!((d.smart_filter.as_ref().map(|(index, _)| *index), d.cell), (Some(0), 3.0)); assert_eq!(d.strokes, recorded); assert_eq!(d.field, LiquifyField::from_strokes(d.canvas, 3.0, &recorded)); // The preview starts from the pixels below the filter (no liquify, no blur). @@ -812,12 +812,37 @@ fn smart_filter_edit_that_cannot_be_applied_reports_the_error() { let r = control(&mut app, &json!({"commit": true})); assert!(app.distort.liquify.is_none()); assert!(app.ui.status_error); - assert!(app.ui.status.contains("no smart filters"), "{}", app.ui.status); + assert!(app.ui.status.contains("no longer exists"), "{}", app.ui.status); assert!(smart(&app, layer).smart_filters.is_empty()); assert_eq!(app.session.active().unwrap().history.past_len(), steps); assert_eq!(r, Err(app.ui.status.clone())); } +/// OK after the stack was reordered while the workspace was open: the filter now at the +/// edited index (the Gaussian Blur) is left alone and the status bar asks to reopen. +#[test] +fn smart_filter_edit_after_the_stack_changed_updates_nothing() { + let ctx = egui::Context::default(); + let (mut app, layer) = smart_liquify_app(); + open_smart_filter(&mut app, &ctx, layer, 0).unwrap(); + drag(&mut app, 70.0, 84.0); + app.session.execute("layer.smartFilter.move", json!({"layer": layer.0, "index": 0, "to": 1})).unwrap(); + let before = smart(&app, layer).smart_filters; + assert_eq!(before[0].command, "filter.blur.gaussianBlur"); + let steps = app.session.active().unwrap().history.past_len(); + let r = control(&mut app, &json!({"commit": true})); + assert!(app.distort.liquify.is_none()); + assert!(app.ui.status_error); + assert!(app.ui.status.contains("Reopen its dialog"), "{}", app.ui.status); + let after = smart(&app, layer).smart_filters; + assert_eq!(after.len(), before.len()); + for (a, b) in after.iter().zip(&before) { + assert_eq!((&a.command, &a.params), (&b.command, &b.params)); + } + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + assert_eq!(r, Err(app.ui.status.clone())); +} + /// OK after the layer was deleted: the dialog closes, the status bar shows the error and the /// control channel returns it. #[test] diff --git a/crates/ui-egui/src/panels/smart_ui.rs b/crates/ui-egui/src/panels/smart_ui.rs index 8e531b47b6..89c1e76b5d 100644 --- a/crates/ui-egui/src/panels/smart_ui.rs +++ b/crates/ui-egui/src/panels/smart_ui.rs @@ -203,6 +203,18 @@ pub fn filter_rows(app: &mut PhotocraftApp, ui: &mut egui::Ui, doc: &Document, l if let Some(i) = index && hit(egui::Response::double_clicked) { + // The editor captures the document's revision as its target, so the click's queued + // `layer.select` must not run after it opens (OK would refuse the edit): select first, + // as the Layers panel runs it. + if !active { + actions.retain(|(id, p)| !(id == "layer.select" && p.get("layer").and_then(Value::as_u64) == Some(l.id.0))); + if app.ui.text_edit.is_some() { + crate::type_tool::commit(app); + } + if app.run("layer.select", json!({"layer": l.id.0})).is_err() { + continue; + } + } open_editor(app, ui.ctx(), l.id.0, sm, i); } if let (Some(i), Some(r)) = (index, blend) { diff --git a/crates/ui-egui/src/smart_filter_rows_tests.rs b/crates/ui-egui/src/smart_filter_rows_tests.rs index 7457b0c780..e65123e989 100644 --- a/crates/ui-egui/src/smart_filter_rows_tests.rs +++ b/crates/ui-egui/src/smart_filter_rows_tests.rs @@ -9,6 +9,7 @@ use photocraft_doc::{LayerContent, LayerId}; use serde_json::json; use crate::PhotocraftApp; +use crate::canvas::ToolEvent; /// Smart objects "Smart A" (Gaussian Blur and Offset) and "Smart B" (no filters), and a pixel /// layer "Pixels" on top: (harness, A, B, pixels). @@ -339,3 +340,57 @@ fn a_click_selects_a_filter_of_the_bottom_layer() { assert_eq!(st.selected_layers(), [LayerId(id)], "the click did not deselect the layers"); assert!(selected(&h, "Gaussian Blur")); } + +/// A double-click whose two clicks land in one frame, as a real mouse delivers them when a frame +/// takes longer than the gap between the clicks (`Harness::event` would run a frame per event). +fn double_click_in_one_frame(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, p: Pos2) { + h.hover_at(p); + h.run_steps(1); + for pressed in [true, false, true, false] { + h.input_mut().events.push(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + } + h.run_steps(4); +} + +/// #7153: a double-click on a filter of a smart object that isn't the active layer also selects +/// it. That selection must not land after the editor captured its target, or OK refuses the edit +/// ("The document or Smart Filter changed"). +#[test] +fn double_clicking_a_filter_of_an_inactive_smart_object_edits_it() { + let (mut h, a, _, c) = panel(); + assert_eq!(h.state().session.active().unwrap().active_layer, Some(LayerId(c))); + let blur = at(&h, "Gaussian Blur"); + double_click_in_one_frame(&mut h, blur); + assert_eq!(h.state().session.active().unwrap().active_layer, Some(LayerId(a)), "the smart object becomes active"); + let d = h.state_mut().ui.dialogs.last_mut().expect("the filter's dialog opened"); + d.fields.insert("radius".into(), json!(3.0)); + let dialog = d.id; + crate::dialogs::confirm(h.state_mut(), dialog).unwrap(); + assert!(!h.state().ui.status_error, "{}", h.state().ui.status); + let LayerContent::Smart(sm) = &h.state().session.active().unwrap().doc.layer(LayerId(a)).unwrap().content else { panic!("smart object") }; + assert_eq!(sm.smart_filters.len(), 2, "edited in place, not added"); + assert_eq!(sm.smart_filters[0].params["radius"], json!(3.0)); +} + +/// The same for Liquify, whose workspace takes the strokes with it: a refused OK lost them. +#[test] +fn double_clicking_the_liquify_filter_of_an_inactive_smart_object_edits_it() { + let (mut h, a, _, c) = panel(); + let stroke = json!({"tool": "forwardWarp", "size": 8, "points": [[4.0, 8.0, 1.0], [8.0, 8.0, 1.0]]}); + h.state_mut().run("layer.select", json!({"layer": a})).unwrap(); + h.state_mut().run("filter.liquify", json!({"strokes": [stroke], "meshSize": 3.0})).unwrap(); + h.state_mut().run("layer.select", json!({"layer": c})).unwrap(); + h.run_steps(4); + let liquify = at(&h, "Liquify"); + double_click_in_one_frame(&mut h, liquify); + assert!(h.state().distort.liquify.is_some(), "the Liquify workspace opens"); + for ev in [ToolEvent::Down { x: 6.0, y: 4.0, pressure: 1.0 }, ToolEvent::Move { x: 10.0, y: 4.0, pressure: 1.0 }, ToolEvent::Up { x: 10.0, y: 4.0 }] { + crate::liquify_ui::pointer(h.state_mut(), ev, Modifiers::NONE); + } + h.run_steps(2); + crate::liquify_ui::control(h.state_mut(), &json!({"commit": true})).unwrap(); + assert!(!h.state().ui.status_error, "{}", h.state().ui.status); + let LayerContent::Smart(sm) = &h.state().session.active().unwrap().doc.layer(LayerId(a)).unwrap().content else { panic!("smart object") }; + let liquify = sm.smart_filters.iter().find(|f| f.command == photocraft_engine::distort_cmds::LIQUIFY).unwrap(); + assert_eq!(liquify.params["strokes"].as_array().map(Vec::len), Some(2), "the new stroke was kept"); +}