craft-ui: a root's egui widget takes its whole rect and egui clips its interact rect

As egui's own widgets do (Response::rect and interact_rect): a root its
clip cuts reports and reveals where it is, and egui's hit test uses the
part inside the clip. Widening a widget's sense checks its interact
rect; a secondary release opens a press opener only inside it.
This commit is contained in:
aiongg committed 2026-11-20 06:08:46 +00:00
1 parent 16952f629e
commit f43fb4e2c2
4 files changed
+25 -10

No files matched your search

@@ -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. /// `Trigger::Press` opener opens on.
pub(super) fn secondary_release_in(response: &Response) -> bool { pub(super) fn secondary_release_in(response: &Response) -> bool {
pointer::with_pass_raw(&response.ctx, |pass| { pointer::with_pass_raw(&response.ctx, |pass| {
pass.frame.as_ref().is_some_and(|frame| { pass.frame.as_ref().is_some_and(|frame| {
frame.release == Some(PointerButton::Secondary) frame.release == Some(PointerButton::Secondary)
&& frame.pos.is_some_and(|pos| { && 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)))
}) })
}) })
}) })
+6 -6
View File
@@ -629,14 +629,14 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult {
} else { } else {
Sense::HOVER | spec.own.iter().fold(spec.own_sense, |sense, part| sense | part.sense).intersection(Sense::FOCUS) 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. // The response rect is the root's whole rect and egui's hit test uses its part inside the clip (`interact_rect`),
let shown = spec.rect.intersect(clip); // 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, Rect::from_min_max(shown.min, shown.max.max(shown.min)), sense); 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. // 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")]; let mut widgets = vec![spec.id, sub(spec.id, "transform")];
for part in spec.own.iter().filter(|part| reaches_out(spec.rect, part)) { for part in spec.own.iter().filter(|part| reaches_out(spec.rect, part)) {
let id = sub(spec.id, ("edge", part.key, part.index)); 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); crate::runtime::egui::access::hide(ctx, id);
widgets.push(id); widgets.push(id);
} }
@@ -723,7 +723,7 @@ pub(crate) fn place_over_widgets(ui: &Ui, id: Id, member: Option<ArenaId>, decls
let widgets: Vec<Id> = over_parts(decls) let widgets: Vec<Id> = over_parts(decls)
.map(|part| { .map(|part| {
let widget = over_widget(id, 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); crate::runtime::egui::access::hide(ctx, widget);
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. /// 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 { pub(crate) fn sense_none(ui: &Ui, id: Id, rect: Rect) -> RootResult {
let ctx = ui.ctx(); 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); 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() } RootResult { response, parts: PartResults::new(), press: PressState::IDLE, activation: false, activated_member: None, sense: Sense::empty() }
} }
@@ -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 /// 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 /// 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 { 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 }; let Some((has, rect)) = registered else { return false };
if has.contains(sense) || !(rect.is_positive() && rect.is_finite()) { if has.contains(sense) || !(rect.is_positive() && rect.is_finite()) {
return false; return false;
@@ -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"); 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_...`. /// Skeleton: `frames_pointer::button_on_canvas_click_and_slop_drag` and `..._on_zoomed_canvas_...`.
fn member_on_canvas(transform: Option<CanvasTransform>) { fn member_on_canvas(transform: Option<CanvasTransform>) {
let (mut show_frame, log) = scene(vec![canvas(transform)]); let (mut show_frame, log) = scene(vec![canvas(transform)]);