diff --git a/crates/draw/src/ui/corner/stories.rs b/crates/draw/src/ui/corner/stories.rs index 6b9541ada..ef25594fb 100644 --- a/crates/draw/src/ui/corner/stories.rs +++ b/crates/draw/src/ui/corner/stories.rs @@ -135,7 +135,9 @@ fn on_canvas( /// the keyboard), ⌥ on the rectangle changes one corner, ⌥-click cycles the kind, a double click asks for the Corners /// dialog. Story values: the kinds, the widest inner angle that shows a widget, and whether the path's corners take /// their own radius. -#[story(CornerWidgets<'static>, covers = [Default], drive = Drive::Drag(Grip::part(WIDGET.at(0)).toward(vec2(1.0, 1.0))))] +// The drive grips the rectangle's top-right widget: the page shows at 100 %, and a fixture narrower than the page puts +// the top-left widget past its left edge, where no press reaches it. +#[story(CornerWidgets<'static>, covers = [Default], drive = Drive::Drag(Grip::part(WIDGET.at(1)).toward(vec2(-1.0, 1.0))))] fn edit(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let words = Env::of(ui).words::(); let kinds = *data.value("kinds", true); diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index 3dd7336d6..efcb91f98 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -507,6 +507,15 @@ impl<'m> ToolHost<'m> { } // A momentary tool takes the next press anywhere on the canvas, over the session members on its body. pointer::withhold_members(&ctx, pass.id, momentary_tool.is_some()); + if ctx.input(|i| i.pointer.button_pressed(egui::PointerButton::Secondary) || i.pointer.button_released(egui::PointerButton::Secondary)) { + eprintln!( + "DIAG space_hand_view_menu_keeps_escape_and_enter_from_the_pending_crop: host {:?} momentary {:?} gesture {} background {:?}", + pass.id, + momentary_tool, + state.gesture.is_some(), + pass.press.phase + ); + } // With no gesture and no momentary tool, a held stand-in key makes the stand-in the tool of hover, cursor and // paint while the selected tool does not take a primary press. let lent = if state.gesture.is_none() && momentary_tool.is_none() && !takes_primary { diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 1b02168db..3b97d458d 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -180,7 +180,7 @@ 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. + /// The root's clip: where its parts and members can be seen. clip: Rect, own_sense: Sense, } @@ -379,11 +379,16 @@ pub(crate) fn members(ctx: &Context, viewport: ViewportId, arena: ArenaId) -> Sm }) } -/// The clip the root `root` registered in its latest run, in the current viewport: where its parts and members can be -/// seen, so where a press egui names no target for can start. `None` for a root that has not run. +/// The clip of the arena `component` is in (its own, or the one of the root it is a member of), as that root +/// registered it in its latest run in the current viewport: where its parts and members can be seen. `None` for a +/// component in no arena that has run. #[cfg(feature = "testing")] -pub(crate) fn root_clip(ctx: &Context, root: Id) -> Option { - PerViewport::::with(ctx, pass::current_viewport(ctx), |arenas| arenas.by_root.get(&root).and_then(|state| state.root).map(|info| info.clip)) +pub(crate) fn arena_clip(ctx: &Context, component: Id) -> Option { + PerViewport::::with(ctx, pass::current_viewport(ctx), |arenas| { + let joined = |state: &ArenaState| state.members.iter().chain(&state.previous).any(|member| member.id == component); + let state = arenas.by_root.get(&component).or_else(|| arenas.by_root.values().find(|state| joined(state)))?; + state.root.map(|info| info.clip) + }) } /// Whether a member of the arena root `arena` (a canvas control in a canvas, say) has a running gesture, in @@ -765,6 +770,27 @@ fn resolve_in(state: &ArenaState, members: bool, xf: Option, point: Po won.map(|winner| Hit { owner: winner.owner, part: winner.part, sense: winner.sense, local: winner.local }) } +/// The press's winner at `point`. Past the root's clip, where only automation presses (a document point an agent +/// names off the window), the winner takes the press only when some of it shows inside the clip: a part the clip hides +/// wholly (a handle past the window's edge) is not pressed, and nothing under it is either. +fn resolve_press_in(state: &ArenaState, members: bool, xf: Option, point: Pos2, target: f32) -> Option { + let shapes = candidates(state, members, target); + let won = resolve_mapped(&shapes, point, ResolveFor::Press, |p| xf.map_or(p, |map| map.to_layer(p)))?; + let clip = state.root.map_or(Rect::NOTHING, |root| root.clip); + if !clip.contains(point) { + let shape = &shapes[won.index]; + let bounds = shape.shape.bounds(); + let bounds = match xf.filter(|_| shape.local) { + Some(map) => Rect::from_two_pos(map.to_screen(bounds.min), map.to_screen(bounds.max)), + None => bounds, + }; + if !bounds.intersects(clip) { + return None; + } + } + Some(Hit { owner: won.owner, part: won.part, sense: won.sense, local: won.local }) +} + /// Runs the press lifecycle for this pass, once: later calls in the pass return the same outcome. A repeated /// pass has no events, so phases hold and a release, click or activation does not repeat. `union_sense` is /// what the root's egui widgets sense. @@ -805,15 +831,12 @@ 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 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. + // neither, which egui never names, as where there is none. 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 => in_clip && takes(true), + None => takes(true), }; let targets = pass_pointer.press_targets; let allowed = if union_sense.contains(Sense::CLICK) { @@ -835,8 +858,14 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab // egui gives no click target where a drag-only widget is on top (a resize grip over a layer's or a // window's edge): a root under it keeps its click there, but a drag is that widget's. let drag_elsewhere = targets.click.is_none() && targets.drag.is_some_and(|id| !mine(Some(id))); - resolve_in(state, members, xf, at, ResolveFor::Press, target) - .map(|hit| (hit, if drag_elsewhere { hit.sense.difference(Sense::DRAG) } else { hit.sense })) + let won = resolve_press_in(state, members, xf, at, target); + if !state.previous.is_empty() && ctx.input(|i| i.pointer.button_pressed(egui::PointerButton::Secondary)) { + eprintln!( + "DIAG space_hand_view_menu_keeps_escape_and_enter_from_the_pending_crop: root {root:?} members {members} targets {targets:?} won {:?}", + won.map(|hit| (hit.owner, hit.part)) + ); + } + won.map(|hit| (hit, if drag_elsewhere { hit.sense.difference(Sense::DRAG) } else { hit.sense })) }); cancelled = matches!(step.phase, Some(PressPhase::Cancel { .. })); release_held = machine.release_held(); diff --git a/crates/ui-contract/src/testing/fixture.rs b/crates/ui-contract/src/testing/fixture.rs index 47c965b18..b0bc5856f 100644 --- a/crates/ui-contract/src/testing/fixture.rs +++ b/crates/ui-contract/src/testing/fixture.rs @@ -541,10 +541,11 @@ impl Fixture { crate::runtime::pointer::part_bounds(self.ctx(), owner, part.into()) } - /// The part of `target` the shown component's root can be seen in (`target` ∩ the root's clip), where a press on - /// it lands; `None` when none of it is visible. A part past the window's edge is pressed on its visible part. + /// The piece of `target` that can be seen (`target` ∩ the clip of the arena the shown component is in), where a + /// press on it lands; `None` when none of it is visible. A part past the window's edge is pressed on its visible + /// piece, and one wholly past it cannot be pressed. pub fn visible(&self, target: &Target) -> Option { - let clip = self.root_id().and_then(|root| crate::primitives::sensing::root_clip(self.ctx(), root)); + let clip = self.root_id().and_then(|root| crate::primitives::sensing::arena_clip(self.ctx(), root)); let rect = clip.map_or(target.rect, |clip| target.rect.intersect(clip)); (rect.width() >= 0.0 && rect.height() >= 0.0).then_some(Target { node: target.node, rect }) } diff --git a/crates/ui-contract/tests/it/canvas/past_the_clip.rs b/crates/ui-contract/tests/it/canvas/past_the_clip.rs index 2bbf957e5..b82aaf3ec 100644 --- a/crates/ui-contract/tests/it/canvas/past_the_clip.rs +++ b/crates/ui-contract/tests/it/canvas/past_the_clip.rs @@ -1,5 +1,6 @@ -//! 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. +//! A press past its root's clip, which only automation makes (a document point an agent names off the window): a part +//! that shows inside the clip takes it, as a tool takes an agent's press anywhere on the document; a part the clip hides +//! wholly (a handle past the window's edge) does not. use craft_behavior::{HitShape, PartKey, PressPhase, Sense}; use craft_ui::contract::{Common, Component, Cx, PartsCx, Pointer as _, Role, Show as _}; @@ -12,11 +13,12 @@ use std::cell::Cell; const HANDLE: PartKey = PartKey::new("handle"); -/// A leaf whose one part covers its whole rect. +/// A leaf with one part over `part` of its rect. #[derive(Component)] struct Handle<'a> { #[common] common: Common, + part: Rect, pressed: &'a Cell, } @@ -42,8 +44,8 @@ impl Component for Handle<'_> { Sense::empty() } - fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { - cx.part(HANDLE, HitShape::Rect(rect)).sense(Sense::DRAG | Sense::CLICK); + fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) { + cx.part(HANDLE, HitShape::Rect(self.part)).sense(Sense::DRAG | Sense::CLICK); } fn update(&mut self, cx: &mut Cx<'_, Self>) { @@ -57,12 +59,13 @@ impl Component for Handle<'_> { } } -fn pressed_at(at: Pos2) -> bool { +/// Whether a press at `at` is the press of a part over `part`. +fn pressed_at(part: Rect, 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()); + Handle { common: Common::new(), part, pressed: &pressed }.show_at(ui, area()); }; frames.frame(vec![moved(at)], &mut show); frames.frame(vec![press(at)], &mut show); @@ -70,7 +73,10 @@ fn pressed_at(at: Pos2) -> bool { } #[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"); +fn past_the_clip_only_a_part_that_shows_takes_a_press() { + let past = Pos2::new(clip().right() + 40.0, clip().center().y); + assert!(pressed_at(area(), clip().center()), "where the part shows the press is its"); + assert!(pressed_at(area(), past), "a part that shows inside the clip takes a press past it"); + let hidden = Rect::from_center_size(past, Vec2::splat(12.0)); + assert!(!pressed_at(hidden, past), "a handle the clip hides wholly is not pressed"); }