diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index b9b329c17..c06aa55ec 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -180,6 +180,8 @@ struct RootInfo { rect: Rect, /// The part of `rect` inside the root's clip: where its own background lies. shown: Rect, + /// The root's clip: where its parts and members can be seen, so pressed. + clip: Rect, own_sense: Sense, } @@ -543,7 +545,7 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { let known = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| { state.own.clear(); state.own.extend_from_slice(spec.own); - state.root = Some(RootInfo { id: spec.id, rect: spec.rect, shown, own_sense: spec.own_sense }); + state.root = Some(RootInfo { id: spec.id, rect: spec.rect, shown, clip, own_sense: spec.own_sense }); state.widgets = widgets; state.last_layer = Some(layer); state.last_sense = sense; @@ -796,12 +798,15 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab let step = machine.step(input, |at| { // The press-target rule: egui's click target decides, its drag target only for a drag-only root; the // root starts a press when that target is its own or there is none, and a root whose widgets sense - // neither, which egui never names, as where there is none. A root under a modal layer starts none. + // neither, which egui never names, as where there is none. A press with no target starts only inside the + // root's clip: a part beyond it (a handle past the window's edge) is not seen, so not pressed. A root under + // a modal layer starts none. let root = state.root.map(|root| root.id); let takes = |untargeted| crate::runtime::sampler::root_takes_press(ctx, root, layer, at, untargeted); + let in_clip = state.root.is_some_and(|root| root.clip.contains(at)); let mine = |id: Option| match id { Some(id) => state.widgets.contains(&id) || member_widget_of(&state.previous, id), - None => takes(true), + None => in_clip && takes(true), }; let targets = pass_pointer.press_targets; let allowed = if union_sense.contains(Sense::CLICK) { diff --git a/crates/ui-contract/tests/it/canvas/mod.rs b/crates/ui-contract/tests/it/canvas/mod.rs index 0182c0287..7c25209c0 100644 --- a/crates/ui-contract/tests/it/canvas/mod.rs +++ b/crates/ui-contract/tests/it/canvas/mod.rs @@ -1,3 +1,4 @@ //! `Canvas` (20.6): a press away from every canvas control's parts reaches the tool. mod outside_polygon; +mod past_the_clip; diff --git a/crates/ui-contract/tests/it/canvas/past_the_clip.rs b/crates/ui-contract/tests/it/canvas/past_the_clip.rs new file mode 100644 index 000000000..2bbf957e5 --- /dev/null +++ b/crates/ui-contract/tests/it/canvas/past_the_clip.rs @@ -0,0 +1,76 @@ +//! A part reaching past its root's clip: a press on the part where the clip hides it is no press on it (a handle past +//! the window's edge); where it shows, the press is the part's. + +use craft_behavior::{HitShape, PartKey, PressPhase, Sense}; +use craft_ui::contract::{Common, Component, Cx, PartsCx, Pointer as _, Role, Show as _}; +use craft_ui::style::StyleCx; +use craft_ui::testing::pointer::{moved, press}; +use craft_ui::testing::{Frames, trial}; +use craft_ui_test_kit::contract::PlainStyle; +use egui::{Pos2, Rect, Response, Vec2}; +use std::cell::Cell; + +const HANDLE: PartKey = PartKey::new("handle"); + +/// A leaf whose one part covers its whole rect. +#[derive(Component)] +struct Handle<'a> { + #[common] + common: Common, + pressed: &'a Cell, +} + +fn area() -> Rect { + Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(200.0, 120.0)) +} + +/// The clip the leaf is shown in: its left half. +fn clip() -> Rect { + Rect::from_min_size(area().min, Vec2::new(100.0, 120.0)) +} + +impl Component for Handle<'_> { + const ROLE: Role = Role::None; + type Style = PlainStyle; + type Output = Response; + + fn style(_: &StyleCx<'_>, _: HandleStyleKey) -> PlainStyle { + PlainStyle::min(Vec2::new(200.0, 120.0)) + } + + fn sense(&self) -> Sense { + Sense::empty() + } + + fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { + cx.part(HANDLE, HitShape::Rect(rect)).sense(Sense::DRAG | Sense::CLICK); + } + + fn update(&mut self, cx: &mut Cx<'_, Self>) { + if matches!(cx.part(HANDLE).press.phase, PressPhase::Down { .. }) { + self.pressed.set(true); + } + } + + fn finish(self, cx: Cx<'_, Self>) -> Response { + cx.response.clone() + } +} + +fn pressed_at(at: Pos2) -> bool { + let pressed = Cell::new(false); + let mut frames = Frames::new(); + let mut show = |ui: &mut egui::Ui| { + ui.set_clip_rect(clip()); + Handle { common: Common::new(), pressed: &pressed }.show_at(ui, area()); + }; + frames.frame(vec![moved(at)], &mut show); + frames.frame(vec![press(at)], &mut show); + pressed.get() +} + +#[trial] +fn a_press_where_the_clip_hides_a_part_is_not_the_parts() { + assert!(pressed_at(clip().center()), "where the part shows the press is its"); + assert!(!pressed_at(Pos2::new(clip().right() + 40.0, clip().center().y)), "past the clip the part is not pressed"); +}