diff --git a/crates/ui-contract/src/contract/overlay/opener.rs b/crates/ui-contract/src/contract/overlay/opener.rs index eddf596da..c8a9a42cd 100644 --- a/crates/ui-contract/src/contract/overlay/opener.rs +++ b/crates/ui-contract/src/contract/overlay/opener.rs @@ -125,14 +125,14 @@ impl Opener { } } -/// Whether a secondary-button release lands inside `response`'s rect in this pass: the secondary click a +/// Whether a secondary-button release lands inside `response`'s rect, where its clip shows it, in this pass: the secondary click a /// `Trigger::Press` opener opens on. pub(super) fn secondary_release_in(response: &Response) -> bool { pointer::with_pass_raw(&response.ctx, |pass| { pass.frame.as_ref().is_some_and(|frame| { frame.release == Some(PointerButton::Secondary) && frame.pos.is_some_and(|pos| { - response.rect.contains(areas::layer_map(&response.ctx, response.layer_id).map_or(pos, |map| map.to_layer(pos))) + response.interact_rect.contains(areas::layer_map(&response.ctx, response.layer_id).map_or(pos, |map| map.to_layer(pos))) }) }) }) diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 9c20b5f70..d87489043 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -629,14 +629,14 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { } else { Sense::HOVER | spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense).intersection(Sense::FOCUS) }; - // A root outside its clip gets an empty response rect at its corner, never an inverted one. - let shown = spec.rect.intersect(clip); - let response = response::widget(ui, spec.id, Rect::from_min_max(shown.min, shown.max.max(shown.min)), sense); + // The response rect is the root's whole rect and egui's hit test uses its part inside the clip (`interact_rect`), + // as for any egui widget: a root the clip cuts reports and reveals where it is, not where it shows. + let response = response::widget(ui, spec.id, spec.rect, sense); // Each edge or handle part that reaches outside the root rect gets its own widget, resolved as the root. let mut widgets = vec![spec.id, sub(spec.id, "transform")]; for part in spec.own.iter().filter(|part| reaches_out(spec.rect, part)) { let id = sub(spec.id, ("edge", part.key, part.index)); - response::widget(ui, id, part.shape.bounds().intersect(clip), part.sense); + response::widget(ui, id, part.shape.bounds(), part.sense); crate::runtime::egui::access::hide(ctx, id); widgets.push(id); } @@ -723,7 +723,7 @@ pub(crate) fn place_over_widgets(ui: &Ui, id: Id, member: Option, decls let widgets: Vec = over_parts(decls) .map(|part| { let widget = over_widget(id, part); - response::widget(ui, widget, part.shape.bounds().intersect(clip), part.sense); + response::widget(ui, widget, part.shape.bounds(), part.sense); crate::runtime::egui::access::hide(ctx, widget); widget }) @@ -771,7 +771,7 @@ pub(crate) fn widen_root(ctx: &Context, id: Id, hidden: bool) { /// record or press lifecycle, only its egui widget and its entry in the widget record. pub(crate) fn sense_none(ui: &Ui, id: Id, rect: Rect) -> RootResult { let ctx = ui.ctx(); - let response = response::widget(ui, id, rect.intersect(ui.clip_rect()), Sense::empty()); + let response = response::widget(ui, id, rect, Sense::empty()); pointer::record_widgets(ctx, ui.layer_id(), id, &[id, sub(id, "transform")], false); RootResult { response, parts: PartResults::new(), press: PressState::IDLE, activation: false, activated_member: None, sense: Sense::empty() } } diff --git a/crates/ui-contract/src/runtime/egui/response.rs b/crates/ui-contract/src/runtime/egui/response.rs index 901a14f2f..58d89971f 100644 --- a/crates/ui-contract/src/runtime/egui/response.rs +++ b/crates/ui-contract/src/runtime/egui/response.rs @@ -51,9 +51,9 @@ pub(crate) fn widget(ui: &Ui, id: Id, rect: Rect, sense: Sense) -> Response { /// Adds `sense` to the egui widget `id` the running pass registered, at its rect: egui ORs the senses of a widget /// registered again in a pass. Returns whether the widget took a sense it lacked; nothing for a widget the pass has -/// not registered, or one with no area, which egui's hit test never names. +/// not registered, or one with no area inside its clip, which egui's hit test never names. pub(crate) fn widen(ctx: &egui::Context, id: Id, sense: egui::Sense) -> bool { - let registered = ctx.viewport(|viewport| viewport.this_pass.widgets.get(id).map(|widget| (widget.sense, widget.rect))); + let registered = ctx.viewport(|viewport| viewport.this_pass.widgets.get(id).map(|widget| (widget.sense, widget.interact_rect))); let Some((has, rect)) = registered else { return false }; if has.contains(sense) || !(rect.is_positive() && rect.is_finite()) { return false; diff --git a/crates/ui-contract/tests/it/runtime/pointer.rs b/crates/ui-contract/tests/it/runtime/pointer.rs index 1c09d44bf..272682332 100644 --- a/crates/ui-contract/tests/it/runtime/pointer.rs +++ b/crates/ui-contract/tests/it/runtime/pointer.rs @@ -121,6 +121,21 @@ fn a_root_widget_senses_what_its_members_sense_in_the_same_pass() { assert_eq!(clicks(&frames), [Some(true); 2], "the widgets click again in the frame the member comes back"); } +/// A root its clip cuts registers its whole rect as its egui widget's rect, and the part inside the clip as the rect +/// egui's hit test uses, as egui's own widgets do: its response reports and reveals where the root is. +#[trial] +fn a_root_cut_by_its_clip_keeps_its_whole_rect_and_senses_inside_the_clip() { + let (whole, clip) = (rect(100.0, 100.0, 200.0, 100.0), rect(0.0, 0.0, 400.0, 150.0)); + let mut show_frame = move |ui: &mut Ui| { + ui.set_clip_rect(clip); + show(ui, &[ProbeRoot::new("cut", whole, Sense::CLICK)]); + }; + let mut frames = Frames::new(); + frames.idle(1, &mut show_frame); + let widget = frames.ctx.widget_rect_last_pass(ProbeRoot::id("cut")).expect("the root's widget"); + assert_eq!((widget.rect, widget.interact_rect), (whole, whole.intersect(clip)), "the whole rect, sensed inside the clip"); +} + /// Skeleton: `frames_pointer::button_on_canvas_click_and_slop_drag` and `..._on_zoomed_canvas_...`. fn member_on_canvas(transform: Option) { let (mut show_frame, log) = scene(vec![canvas(transform)]);