diff --git a/crates/ui-egui/src/control/dispatch.rs b/crates/ui-egui/src/control/dispatch.rs index c7a0fb53ab8..eb2c072176e 100644 --- a/crates/ui-egui/src/control/dispatch.rs +++ b/crates/ui-egui/src/control/dispatch.rs @@ -1159,14 +1159,19 @@ fn pointer_event_intercepted(app: &mut PhotocraftApp, ctx: &egui::Context, g: &P } /// A filter dialog with a preview window on top: the image is its locator, as for the mouse -/// (#9648). A press or drag with the primary button centres the window there; every event is -/// consumed, so nothing reaches the tool under the dialog. +/// (#9648). A press or drag with the primary button centres the window there; every other event +/// is consumed too, so nothing reaches the tool under the dialog, except the release that ends a +/// tool gesture begun before the dialog opened. fn preview_locator_took(app: &mut PhotocraftApp, g: &PointerGesture, s: &PointerSample) -> bool { if crate::filter_preview_ui::locator(app).is_none() { return false; } - if !matches!(s.ev, ToolEvent::Up { .. }) && !g.secondary && !g.middle && !g.space { - crate::filter_preview_ui::center_at(app, s.x, s.y); + let (down, up) = (matches!(s.ev, ToolEvent::Down { .. }), matches!(s.ev, ToolEvent::Up { .. })); + if up && !g.secondary && !g.middle && app.drag.is_some() { + return false; + } + if !g.secondary && !g.middle && !g.space { + crate::filter_preview_ui::pointer_event(app, down, up, s.x, s.y); } true } diff --git a/crates/ui-egui/src/dialogs/filter_preview_ui.rs b/crates/ui-egui/src/dialogs/filter_preview_ui.rs index 0415264f5b3..8c3ca50e961 100644 --- a/crates/ui-egui/src/dialogs/filter_preview_ui.rs +++ b/crates/ui-egui/src/dialogs/filter_preview_ui.rs @@ -28,6 +28,9 @@ const PREVIEW_H: f32 = 150.0; const ZOOM_FIELD: &str = "__pvZoom"; const CENTER_X_FIELD: &str = "__pvCx"; const CENTER_Y_FIELD: &str = "__pvCy"; +/// Whether a `ui.pointer` press on the locator is held (agents send press, moves and release as +/// separate requests). +const POINTER_DOWN_FIELD: &str = "__pvPointerDown"; /// Draw the filter dialog's in-dialog preview window below its parameters (its `fields` `f`). A /// no-op for dialogs without a preview and for Select and Mask, which has its own view. @@ -173,13 +176,27 @@ pub(crate) fn center_at(app: &mut PhotocraftApp, x: f64, y: f64) -> bool { return false; } let Some(size) = app.session.active().map(|st| st.doc.size) else { return false }; - let (cx, cy) = clamp_center(x as f32, y as f32, size.width as f32, size.height as f32); + // Clamped before narrowing: a huge finite point (1e40 from `ui.pointer`) would overflow `f32` + // to infinity, which `clamp_center` reads as corrupt and puts at 0, not at the far edge. + let (cx, cy) = (x.clamp(0.0, f64::from(size.width)) as f32, y.clamp(0.0, f64::from(size.height)) as f32); let Some(d) = app.ui.dialog_mut(id) else { return false }; d.fields.insert(CENTER_X_FIELD.into(), json!(cx)); d.fields.insert(CENTER_Y_FIELD.into(), json!(cy)); true } +/// A `ui.pointer` primary-button event on the top dialog's locator (#9648): a press centres the +/// preview window, and so does a move while that press is held, remembered across requests as a +/// held mouse button is; a hover or a release does not. +pub(crate) fn pointer_event(app: &mut PhotocraftApp, down: bool, up: bool, x: f64, y: f64) { + let Some(d) = locator(app).and_then(|id| app.ui.dialog_mut(id)) else { return }; + let held = !up && (down || d.fields.get(POINTER_DOWN_FIELD).and_then(Value::as_bool) == Some(true)); + d.fields.insert(POINTER_DOWN_FIELD.into(), json!(held)); + if held { + center_at(app, x, y); + } +} + /// The zoom levels the − / + buttons step through, like the Filter Gallery's. const ZOOM_STEPS: [f32; 7] = [0.125, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0]; @@ -553,6 +570,11 @@ mod tests { assert!(!center_at(&mut app, f64::NAN, 5.0)); assert!(!center_at(&mut app, 5.0, f64::INFINITY)); assert_eq!(centre(&app), (Some(json!(0.0)), Some(json!(48.0)))); + // A huge but finite point (past `f32`'s range) clamps to the far edge, not to 0. + assert!(center_at(&mut app, 1e40, -1e300)); + assert_eq!(centre(&app), (Some(json!(64.0)), Some(json!(0.0)))); + assert!(center_at(&mut app, -1e300, 1e40)); + assert_eq!(centre(&app), (Some(json!(0.0)), Some(json!(48.0)))); let st = app.session.active().unwrap(); assert_eq!((st.revision, st.history.past_len()), (revision, history), "the document and its history are untouched"); // The view fields are private: the command's params don't carry them. diff --git a/crates/ui-egui/src/filter_preview_locator_tests.rs b/crates/ui-egui/src/filter_preview_locator_tests.rs index b0a6338b319..9aa43b73651 100644 --- a/crates/ui-egui/src/filter_preview_locator_tests.rs +++ b/crates/ui-egui/src/filter_preview_locator_tests.rs @@ -200,3 +200,44 @@ fn agents_centre_the_preview_with_ui_pointer_without_painting() { let _ = crate::control::handle(h.state_mut(), &ctx, &req); assert_ne!(edits(&h).0, before.0, "the Brush paints once no preview dialog is open"); } + +/// Sends `events` as one `ui.pointer` request, then lets the frames run as between requests. +fn pointer(h: &mut Harness<'static, PhotocraftApp>, events: Value) { + let (req, _rx) = crate::control::ControlRequest::new("ui.pointer", json!({"events": events})); + let _ = crate::control::handle(h.state_mut(), &egui::Context::default(), &req); + h.run_steps(2); +} + +/// Agents send a press, its moves and its release as separate requests: only a held press centres +/// the preview, a bare hover does not. +#[test] +fn ui_pointer_centres_only_while_the_press_is_held() { + let mut h = harness(Some("filter.sharpen.smartSharpen"), 0.0); + pointer(&mut h, json!([{"kind": "move", "x": 40, "y": 30}])); + // An event without a kind is a move, so a hover too. + pointer(&mut h, json!([{"x": 50, "y": 40}])); + assert!(near(centre(&h), [200.0, 150.0]), "a hover leaves the preview where it was: {:?}", centre(&h)); + pointer(&mut h, json!([{"kind": "down", "x": 40, "y": 30}])); + assert!(near(centre(&h), [40.0, 30.0]), "{:?}", centre(&h)); + pointer(&mut h, json!([{"kind": "move", "x": 80, "y": 60}])); + assert!(near(centre(&h), [80.0, 60.0]), "a move while the press is held drags it: {:?}", centre(&h)); + pointer(&mut h, json!([{"kind": "up", "x": 80, "y": 60}])); + pointer(&mut h, json!([{"kind": "move", "x": 120, "y": 90}])); + assert!(near(centre(&h), [80.0, 60.0]), "after the release a move is a hover again: {:?}", centre(&h)); +} + +/// A Brush stroke an agent began before the dialog opened still ends: its release reaches the tool. +#[test] +fn a_tool_gesture_begun_before_the_dialog_still_ends_under_it() { + let mut h = harness(None, 0.0); + let before = edits(&h); + pointer(&mut h, json!([{"kind": "down", "x": 40, "y": 30}, {"kind": "move", "x": 80, "y": 30}])); + assert!(h.state().drag.is_some(), "the Brush stroke is under way"); + crate::filter_dialog::open(h.state_mut(), "filter.sharpen.smartSharpen").unwrap(); + h.run_steps(2); + assert!(h.state().drag.is_some(), "the stroke is still open under the dialog"); + pointer(&mut h, json!([{"kind": "up", "x": 80, "y": 30}])); + assert!(h.state().drag.is_none(), "the release reached the Brush"); + assert_eq!(edits(&h).1, before.1 + 1, "the stroke is one history step"); + assert!(near(centre(&h), [200.0, 150.0]), "the release is not a locator press: {:?}", centre(&h)); +}