diff --git a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs index a8c7305f38..4852f5ce92 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/dialog.rs @@ -12,7 +12,7 @@ use super::preview::{PreviewPane, PreviewView, PreviewZoom}; use super::rows::{ParamRows, ParamRowsOutput}; -use crate::contract::{Bind, BodyCx, NoValue, Outcome, Remember as _, Sampler as _, SaveKey, Show as _, ShowContainer as _}; +use crate::contract::{Bind, BodyCx, CanvasGesture, NoValue, Outcome, Remember as _, Sampler as _, SaveKey, Show as _, ShowContainer as _}; use crate::dialog::{Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, FooterHooks, HeldAnswer, HeldOk, StackDialog, dialog_id}; @@ -202,11 +202,14 @@ pub struct ParamDialog<'a> { #[prop] preview: Option, /// Whether a click on the preview centres it on the point clicked (`PreviewPane::click_centres`); a dialog whose - /// preview clicks pick a colour turns it off. The default is true. + /// preview clicks pick a colour turns it off. The default is true. While it is on and the pane shows, a primary + /// press on the dialog's [`canvas`](ParamDialog::canvas) passes the modal layer too: the image is the pane's + /// locator. #[prop] click_centres: bool, /// The canvas the rows' droppers sample (`ParamRows::canvas`). While the dialog shows, its view stays usable under - /// the modal layer (`Sampler::keep_view_open`). + /// the modal layer (`Sampler::keep_view_open`), and while its preview pane centres on clicks a primary press on it + /// passes (`Sampler::pass_canvas_gesture`): the app centres the pane on the point under it. #[prop] canvas: Option, /// Extra footer buttons after OK and Cancel (Color Table's Load… and Save…), each an id and a label. They keep the @@ -525,6 +528,11 @@ impl<'a> ParamDialog<'a> { if let Some(canvas) = canvas { cx.keep_view_open(canvas); } + // The canvas shows the picture the pane does: while a click on the pane centres it, a press on the image is + // the pane's locator press, which the app answers with `HostedParamDialog::centre_preview`. + if let Some(canvas) = canvas.filter(|_| click_centres && preview.is_some()) { + cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY); + } // A measuring run closes nothing: the real run of the same pass makes the close. if finish_held && !crate::runtime::measure::active(ui.ctx()) { cx.request_close_as(CloseReason::Activated); diff --git a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs index 72da6c86f2..2ad66a6ccc 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/dialog.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/dialog.rs @@ -2,7 +2,8 @@ //! a multi-line row and on a focused Cancel, Alt+Reset to `reset_to`, the held OK, reopening under the same key, the //! request slots, disabled options and the remembered values a row refuses. -use craft_ui::contract::{Bind, Role, SaveKey}; +use craft_ui::buttons::Button; +use craft_ui::contract::{Bind, Outcome as _, Role, SaveKey, Show as _, clicked}; use craft_ui::dialog::{DialogHost, DialogResult, HeldOk, dialog_id}; use craft_ui::patterns::form_dialogs::{DialogFields, FieldValue, HostedParamDialog, Param, ParamDialog, ParamKind, ParamResult}; use craft_ui::patterns::presets::PresetRow; @@ -438,6 +439,51 @@ fn alt_reset_sets_the_reset_to_values_and_keeps_the_other_keys() { assert!(fixture.by_label("Blur").is_some(), "Reset keeps the dialog open"); } +/// A blur dialog that previews, over `canvas`, whose pane centres on clicks when `centres`. +fn locating(canvas: egui::Id, centres: bool) -> impl for<'b> Fn(&'b mut DialogFields) -> ParamDialog<'b> + Send + 'static { + move |fields| blur(fields).previews(true).canvas(canvas).click_centres(centres) +} + +// The image under a dialog whose preview pane centres on clicks is the pane's locator: a primary press on the canvas +// passes the modal layer while the pane shows. Before the pane has a picture, or with a pane that does not centre on +// clicks, the press stays blocked. +#[trial] +fn a_preview_that_centres_on_clicks_passes_a_press_on_the_canvas_under_it() { + let (host, canvas, presses) = (Rc::new(RefCell::new(DialogHost::new())), Rc::new(Cell::new(None)), Rc::new(Cell::new(0))); + let (shown, seen, counted) = (Rc::clone(&host), Rc::clone(&canvas), Rc::clone(&presses)); + let mut fixture = Fixture::new(move |ui| { + let surface = Button::new("Canvas").show(ui); + seen.set(Some((surface.response().id, surface.response().rect.center()))); + if clicked(&surface) { + counted.set(counted.get() + 1); + } + shown.borrow_mut().run(ui); + }); + fixture.room(Vec2::new(1200.0, 900.0)); + fixture.settle(); + let (id, at) = canvas.get().expect("the canvas showed"); + let picture = SizedTexture::new(TextureId::default(), vec2(64.0, 48.0)); + // The modal layer takes the canvas out of the access tree: the press goes to the canvas's point. + let press = |fixture: &mut Fixture| { + fixture.click_at(at).settle(); + }; + + host.borrow_mut().open(HostedParamDialog::new(fixture.ctx(), "blur", DialogFields::new(), locating(id, true))); + fixture.idle(4); + press(&mut fixture); + assert_eq!(presses.get(), 0, "no pane yet: the modal layer blocks the press"); + on_dialog(&host, |dialog| dialog.set_preview(Some(picture))); + fixture.idle(4); + press(&mut fixture); + assert_eq!(presses.get(), 1, "with the pane shown the press reaches the canvas"); + + host.borrow_mut().open(HostedParamDialog::new(fixture.ctx(), "blur", DialogFields::new(), locating(id, false))); + on_dialog(&host, |dialog| dialog.set_preview(Some(picture))); + fixture.idle(4); + press(&mut fixture); + assert_eq!(presses.get(), 1, "a pane that does not centre on clicks leaves the press blocked"); +} + /// A dialog with one colour row. fn tint(fields: &mut DialogFields) -> ParamDialog<'_> { ParamDialog::new("tint", "Tint", vec![Param::new("tint", "Tint", ParamKind::color(None))]).values(Bind::bound(fields))