diff --git a/crates/ui-contract/src/contract/sampler.rs b/crates/ui-contract/src/contract/sampler.rs index be1b02e74..c8a2b8918 100644 --- a/crates/ui-contract/src/contract/sampler.rs +++ b/crates/ui-contract/src/contract/sampler.rs @@ -29,7 +29,8 @@ mod sealed { /// The canvas reach under a modal layer (15.5, 8.3): a control of a dialog keeps the canvas below it usable. It is /// implemented for `Cx`, `BodyCx` and so for `FormCx`, which derefs to `BodyCx`. A registration holds for this pass -/// and the next and belongs to the modal layer whose body calls it: a modal opened over that layer blocks the canvas. +/// and the next and belongs to the modal layer whose body calls it, or, called outside every modal body (the tool host +/// passing the running tool's gestures), to the top modal layer: a modal opened over that layer blocks the canvas. /// The registrations of one pass add up. pub trait Sampler: sealed::Host { /// Lets `gesture` on `canvas` (the canvas component's id) through the modal layer, and the view navigation with it diff --git a/crates/ui-contract/src/runtime/sampler.rs b/crates/ui-contract/src/runtime/sampler.rs index eda3c8928..8088eb075 100644 --- a/crates/ui-contract/src/runtime/sampler.rs +++ b/crates/ui-contract/src/runtime/sampler.rs @@ -163,14 +163,20 @@ struct Registry { over: bool, } -/// Registers `canvas` with `reach` for the modal layer whose body is running. `Sampler::pass_canvas_gesture` and -/// `Sampler::keep_view_open` (contract layer) call it. +/// Registers `canvas` with `reach` for the modal layer whose body is running; outside every modal body (the tool host +/// that runs the canvas's tool), for the top modal layer, here or in another viewport. `Sampler::pass_canvas_gesture` +/// and `Sampler::keep_view_open` (contract layer) call it. pub(crate) fn register(ctx: &Context, canvas: Id, reach: Reach) { if !crate::runtime::measure::effect(ctx, "sampler::register") { return; } let now = pass::root_frame_nr(ctx); - let modal = overlay::modal_drawing(ctx); + let viewport = pass::current_viewport(ctx); + let modal = overlay::modal_drawing(ctx).or_else(|| { + let shown = overlay::top_modal_in(ctx, viewport) + .or_else(|| overlay::modal_viewport(ctx, viewport).and_then(|other| overlay::top_modal_in(ctx, other))); + shown.map(|top| top.id) + }); Service::::run(ctx, |registry| { let reach = match registry.made.take() { Some((made, held)) if made == now && held.canvas == canvas && held.modal == modal => held.reach.join(reach), diff --git a/crates/ui-widgets/tests/it/overlay/sampler.rs b/crates/ui-widgets/tests/it/overlay/sampler.rs index bdf3570cb..9c6b82ee1 100644 --- a/crates/ui-widgets/tests/it/overlay/sampler.rs +++ b/crates/ui-widgets/tests/it/overlay/sampler.rs @@ -41,12 +41,14 @@ struct Took { zooms: u32, } -/// The canvas: a surface that takes presses of any button, their drags and the wheel. +/// The canvas: a surface that takes presses of any button, their drags and the wheel, and passes `outside` itself, as +/// a tool host passes its running tool's gesture. #[derive(Component)] struct Surface<'a> { #[common] common: Common, took: &'a RefCell, + outside: Option, } impl Component for Surface<'_> { @@ -67,6 +69,10 @@ impl Component for Surface<'_> { } fn update(&mut self, cx: &mut Cx<'_, Self>) { + if let Some(gesture) = self.outside { + let id = cx.id; + cx.pass_canvas_gesture(id, gesture); + } let mut took = self.took.borrow_mut(); took.id = Some(cx.id); let phase = cx.press().phase; @@ -155,6 +161,8 @@ struct Scene { armer_id: Cell, open: Cell, arm: Cell, + /// The gesture the canvas passes itself, outside the dialog's body. + outside: Cell>, dialog: Opened, } @@ -166,6 +174,7 @@ impl Scene { armer_id: Cell::new(Id::NULL), open: Cell::new(false), arm: Cell::new(arm), + outside: Cell::new(None), dialog: Opened::default(), } } @@ -173,7 +182,7 @@ impl Scene { fn show(&self, ui: &mut Ui) { self.dialog.shown.set(false); let canvas = self.canvas.borrow().id.unwrap_or(Id::NULL); - Surface { common: Common::new(), took: &self.canvas }.id_salt("canvas").show_at(ui, CANVAS); + Surface { common: Common::new(), took: &self.canvas, outside: self.outside.get() }.id_salt("canvas").show_at(ui, CANVAS); Dialog { common: Common::new(), scene: self, canvas }.id_salt("dialog").show_at(ui, OPENER); } @@ -299,3 +308,25 @@ fn a_gesture_with_a_modifier_passes_only_with_the_modifier_held() { assert!(scene.took(PointerButton::Primary) && scene.dragged_canvas(), "the ⌘-press and its drag reached the canvas"); scene.assert_dialog_holds(&frames); } + +/// A gesture the canvas passes outside the dialog's body, as a tool host passes its running tool's gesture, belongs to +/// the top modal: it reaches the canvas under the open dialog, and once the dialog closes it lapses with it, so the +/// next dialog blocks the canvas while nothing passes the gesture again. +#[trial] +fn a_gesture_passed_outside_the_dialog_reaches_the_canvas_and_lapses_with_the_dialog() { + let mut frames = Frames::new(); + let scene = Scene::new(Arm::Nothing); + scene.outside.set(Some(CanvasGesture::PRIMARY)); + scene.open_dialog(&mut frames); + scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0)); + assert!(scene.took(PointerButton::Primary) && scene.dragged_canvas(), "the press and its drag reached the canvas"); + scene.assert_dialog_holds(&frames); + scene.frame(&mut frames, vec![key_press(Key::Escape, Modifiers::NONE)]); + scene.settle(&mut frames, 30); + assert!(!scene.dialog.shown.get(), "Escape closes the dialog"); + scene.outside.set(None); + scene.open_dialog(&mut frames); + scene.forget_canvas(); + scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0)); + assert!(!scene.took(PointerButton::Primary), "the registration lapsed with its dialog"); +}