diff --git a/crates/draw/src/ui/transform/transform_box.rs b/crates/draw/src/ui/transform/transform_box.rs index 16f67148e..8a917a715 100644 --- a/crates/draw/src/ui/transform/transform_box.rs +++ b/crates/draw/src/ui/transform/transform_box.rs @@ -922,10 +922,11 @@ impl Component for TransformBox<'_> { } /// The whole box, the corners, the edges and the reference point the rules allow, in keyboard order, then each - /// side as a pointer-only band outside it, the centre and the move strips where declared, and the outside. A handle, the - /// reference point and the centre rank alike, so the nearest wins where they overlap on a small box; they rank above - /// a side, which ranks above the inside, which ranks above the outside. A live line's controls declare its two - /// ends only. + /// side as a pointer-only band outside it, the centre and the move strips where declared, and the outside. A + /// handle, the reference point and the centre rank alike, so the nearest wins where they overlap on a small box; + /// they rank above a side, which ranks above the inside, which ranks above the outside. Every part takes a press's + /// click with its drag, so a press on the box is the box's alone, not split with what lies under it (8.3). A live + /// line's controls declare its two ends only. fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { let (Some(space), Some(state)) = (canvas_space(&cx.env), cx.value().copied()) else { return }; let style = cx.style; @@ -946,7 +947,7 @@ impl Component for TransformBox<'_> { } BoxPart::Pivot => ( HitShape::circle(pos(screen.pivot), style.hit), - Sense::DRAG | Sense::TAKE_FOCUS, + Sense::DRAG | Sense::CLICK | Sense::TAKE_FOCUS, HitPriority::new(HitLevel::Handle, HANDLE_SUB), FocusShape::Circle, ), @@ -954,7 +955,7 @@ impl Component for TransformBox<'_> { let at = self.part_point(&state, handle).map_or(Pos2::ZERO, |at| space.view.to_screen(at)); ( HitShape::circle(at, style.handle_reach), - Sense::DRAG | Sense::TAKE_FOCUS, + Sense::DRAG | Sense::CLICK | Sense::TAKE_FOCUS, HitPriority::new(HitLevel::Handle, HANDLE_SUB), FocusShape::Circle, ) @@ -972,7 +973,7 @@ impl Component for TransformBox<'_> { for k in (0..4u8).filter(|&k| self.rules.allows(BoxPart::Edge(k))) { let side = usize::from(k); cx.part(EDGE.at(u64::from(k)), side_band(&quad, side, style.side_band)) - .sense(Sense::DRAG | slop_sense(BoxPart::Edge(k))) + .sense(Sense::DRAG | Sense::CLICK | slop_sense(BoxPart::Edge(k))) .priority(HitPriority::new(HitLevel::Handle, EDGE_SUB)) .pointer_only(EDGE_BY_KEY); } diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 0cd415bae..3a8d7e1b6 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -788,10 +788,11 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab .cancel(cancel); let mut machine = std::mem::take(&mut state.machine); let step = machine.step(input, |at| { - // The press-target rule (8.3): exactly one root starts a press, the one whose shape is on top under it - // (`pointer::press_winner`). A root under a modal layer starts none; where egui names no target of the - // root's kind (its click target, its drag target for a drag-only root; a root whose widgets sense neither - // is never named), only a root the modal's canvas admits does. + // The press-target rule (8.3): a press's click and its drag each start on one root, the one whose shape + // is on top under it for that kind (`pointer::press_winners`); the root starts it for the kinds it won. A + // root under a modal layer starts none; where egui names no target of the root's kind (its click target, + // its drag target for a drag-only root; a root whose widgets sense neither is never named), only a root the + // modal's canvas admits does. let root = state.root.map(|root| root.id); let targets = pass_pointer.press_targets; let untargeted = if union_sense.contains(Sense::CLICK) { @@ -801,13 +802,13 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab } else { true }; - let allowed = crate::runtime::sampler::root_takes_press(ctx, root, layer, at, untargeted) - && root.is_some_and(|own| pointer::press_winner(ctx, at, targets).is_some_and(|(_, winner)| winner == own)); + let won_kinds = root.map_or(Sense::empty(), |own| pointer::press_winners(ctx, at, targets).senses_of(own)); + let allowed = crate::runtime::sampler::root_takes_press(ctx, root, layer, at, untargeted) && !won_kinds.is_empty(); if let Some(test) = pointer::diag_test() && state.root.is_some_and(|own| own.rect.contains(at)) { eprintln!( - "DIAG {test}: start root {:?} enabled {enabled} allowed {allowed} untargeted {untargeted} takes {} layer {layer:?} rect {:?} clip {:?}", + "DIAG {test}: start root {:?} enabled {enabled} allowed {allowed} won {won_kinds:?} untargeted {untargeted} takes {} layer {layer:?} rect {:?} clip {:?}", root.map(pointer::diag_id), crate::runtime::sampler::root_takes_press(ctx, root, layer, at, untargeted), state.root.map(|own| own.rect), @@ -835,7 +836,12 @@ fn outcome_in(ctx: &Context, read: &PassRead, arena: ArenaId, layer: Layer, enab won.map(|hit| (hit.owner, hit.part)) ); } - won.map(|hit| (hit, if drag_elsewhere { hit.sense.difference(Sense::DRAG) } else { hit.sense })) + // The kinds of press another root won are not this one's. + let lost = (Sense::CLICK | Sense::DRAG).difference(won_kinds); + won.map(|hit| { + let sense = hit.sense.difference(lost); + (hit, if drag_elsewhere { sense.difference(Sense::DRAG) } else { sense }) + }) }); cancelled = matches!(step.phase, Some(PressPhase::Cancel { .. })); release_held = machine.release_held(); diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index b81c51fa9..e8b04f19f 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -135,7 +135,7 @@ pub(crate) struct Shapes { next: u32, /// The press winner found over `last`, for the point and targets it was asked for: every root asks for the /// same press. - winner: Option<(Pos2, PressTargets, Option<(Winner, Id)>)>, + winner: Option<(Pos2, PressTargets, PressWinners)>, } /// The begin hook (H5): converts the pass's pointer input. @@ -529,14 +529,36 @@ pub(crate) fn made_by_component(ctx: &Context, viewport: ViewportId, id: Id) -> }) } -/// The root a press at `at` (screen points) belongs to, with the shape of it that wins the press: what the sensing -/// core starts the press on and the focus service classifies it by (8.3, H7). On the topmost layer under the press, -/// each root recorded there in the last pass resolves its own shapes by the arena rule, and the root whose winning -/// shape was recorded last in the pass, the one painted on top, takes the press. A transformed body's layer shows -/// its root's own shapes beneath everything on it. A shape counts only where its clip shows it; at a point no clip -/// on the layer holds (one automation names past the window), a shape counts when some of it shows inside its clip. -/// A press egui's hit test gives to a widget no component registered (a raw egui widget on top) has no winner. -pub(crate) fn press_winner(ctx: &Context, at: Pos2, targets: PressTargets) -> Option<(Winner, Id)> { +/// The roots a press at `at` (screen points) goes to, each with the shape of it that wins it: its click's and its +/// drag's (8.3, H7). On the topmost layer under the press, each root recorded there in the last pass resolves its own +/// shapes by the arena rule, and the roots rank by their winning shapes' place in the pass's paint order, top first. The +/// top one takes every kind of press its shape senses. Under a top shape that only drags (a window's resize grip), the +/// click goes to the first root below whose shape clicks; under one that only clicks (a button), the drag goes to the +/// first root below whose shape drags, when that root takes no click anywhere. A transformed body's layer shows its root's own shapes +/// beneath everything on it. A shape counts only where its clip shows it; at a point no clip on the layer holds (one +/// automation names past the window), a shape counts when some of it shows inside its clip. A press egui's hit test +/// gives to a widget no component registered (a raw egui widget on top) goes to no root. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub(crate) struct PressWinners { + pub click: Option<(Winner, Id)>, + pub drag: Option<(Winner, Id)>, +} + +impl PressWinners { + /// The root that starts the press for its kinds of press, with the kinds it takes. + pub(crate) fn senses_of(&self, root: Id) -> craft_behavior::Sense { + let mut sense = craft_behavior::Sense::empty(); + if self.click.is_some_and(|(_, owner)| owner == root) { + sense |= craft_behavior::Sense::CLICK; + } + if self.drag.is_some_and(|(_, owner)| owner == root) { + sense |= craft_behavior::Sense::DRAG; + } + sense + } +} + +pub(crate) fn press_winners(ctx: &Context, at: Pos2, targets: PressTargets) -> PressWinners { let viewport = pass::current_viewport(ctx); let found = PerViewport::::with(ctx, viewport, |record| { record.winner.filter(|(point, asked, _)| *point == at && *asked == targets).map(|(_, _, found)| found) @@ -544,17 +566,24 @@ pub(crate) fn press_winner(ctx: &Context, at: Pos2, targets: PressTargets) -> Op if let Some(found) = found { return found; } - let found = find_press_winner(ctx, viewport, at, targets); + let found = find_press_winners(ctx, viewport, at, targets); PerViewport::::with(ctx, viewport, |record| record.winner = Some((at, targets, found))); found } -fn find_press_winner(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTargets) -> Option<(Winner, Id)> { +/// The root a press at `at` belongs to, with its winning shape: the click's root (`press_winners`), else the drag's. +/// What the focus service classifies a press by and a host asks a press about. +pub(crate) fn press_winner(ctx: &Context, at: Pos2, targets: PressTargets) -> Option<(Winner, Id)> { + let winners = press_winners(ctx, at, targets); + winners.click.or(winners.drag) +} + +fn find_press_winners(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTargets) -> PressWinners { let Some(layer) = areas::layer_at(ctx, at) else { if let Some(test) = diag_test() { eprintln!("DIAG {test}: press_winner at {at:?}: no layer"); } - return None; + return PressWinners::default(); }; let to_layer = |layer: Layer| areas::layer_map(ctx, layer).map_or(at, |map| map.to_layer(at)); let body = PerViewport::::with(ctx, viewport, |record| record.last_bodies.get(&layer).copied()); @@ -600,9 +629,9 @@ fn find_press_winner(ctx: &Context, viewport: ViewportId, at: Pos2, targets: Pre ); } if targets.click.is_some_and(|widget| !registered_in(&record.last, widget)) { - return None; + return PressWinners::default(); } - stack + let mut ranked: Vec<_> = stack .into_iter() .filter_map(|(tier, root, entry, point)| { let shows = |index: usize| { @@ -615,23 +644,57 @@ fn find_press_winner(ctx: &Context, viewport: ViewportId, at: Pos2, targets: Pre let shapes: Vec> = kept.iter().map(|&index| entry.shapes[index].clone()).collect(); let mut winner = resolve(&shapes, point, ResolveFor::Press)?; winner.index = kept[winner.index]; - Some(((tier, entry.placed[winner.index].order), winner, root)) + Some(((tier, entry.placed[winner.index].order), winner, root, entry.click)) }) - .max_by_key(|(rank, ..)| *rank) - .map(|(_, winner, root)| (winner, root)) + .collect(); + ranked.sort_by(|a, b| b.0.cmp(&a.0)); + let mut winners = PressWinners::default(); + for (_, winner, root, root_clicks) in ranked { + let (clicks, drags) = (winner.sense.contains(craft_behavior::Sense::CLICK), winner.sense.contains(craft_behavior::Sense::DRAG)); + match (winners.click.is_some(), winners.drag.is_some()) { + // The top root takes what its shape senses; one that takes both kinds takes the whole press. + (false, false) => { + winners.click = clicks.then_some((winner, root)); + winners.drag = drags.then_some((winner, root)); + if clicks && drags { + break; + } + } + // Under a top shape that only drags, the first one below that clicks takes the click. + (false, true) => { + if clicks { + winners.click = Some((winner, root)); + break; + } + } + // Under a top shape that only clicks, the first root below whose shape drags takes the drag, unless the + // root takes clicks anywhere: a drag-only root (a scroll view) is dragged from a button in it. + (true, false) => { + if clicks || root_clicks { + break; + } + if drags { + winners.drag = Some((winner, root)); + break; + } + } + (true, true) => break, + } + } + winners }); if let Some(test) = diag_test() { - eprintln!( - "DIAG {test}: press_winner at {at:?} -> {:?}", - found.map(|(winner, root)| (diag_id(root), diag_id(winner.owner), winner.part, winner.sense)) - ); + let show = |found: Option<(Winner, Id)>| found.map(|(winner, root)| (diag_id(root), diag_id(winner.owner), winner.part, winner.sense)); + eprintln!("DIAG {test}: press_winner at {at:?} -> click {:?} drag {:?}", show(found.click), show(found.drag)); } found } /// DIAG: the test under study this thread runs, if any. pub(crate) fn diag_test() -> Option<&'static str> { - const TESTS: [&str; 6] = [ + const TESTS: [&str; 8] = [ + "delete_removes_the_plane_and_escape_drops_a_half_plane_first", + "a_window_mounts_edges_resize_the_window_and_the_childs_press_keeps_its_click", "gradient_map_stop_picker_closed_without_a_colour_changes_nothing", "photo_filter_colour_picker_previews_live_cancel_restores_and_ok_is_one_step", "every_paragraph_box_handle_resizes_the_box_with_snapping_on", @@ -639,8 +702,9 @@ pub(crate) fn diag_test() -> Option<&'static str> { "a_crop_drag_begun_before_the_dialog_still_ends_under_it", "move_pointer_still_reaches_the_liquify_workspace", ]; + // Trials run on unnamed pool threads: there the run names its trial in `CRAFT_DIAG_TEST`. let thread = std::thread::current(); - let name = thread.name()?; + let name = std::env::var("CRAFT_DIAG_TEST").ok().or_else(|| thread.name().map(str::to_owned))?; TESTS.into_iter().find(|test| name.ends_with(test)) } diff --git a/crates/ui-contract/tests/it/runtime/pointer.rs b/crates/ui-contract/tests/it/runtime/pointer.rs index c70796692..7f4d09d97 100644 --- a/crates/ui-contract/tests/it/runtime/pointer.rs +++ b/crates/ui-contract/tests/it/runtime/pointer.rs @@ -347,7 +347,7 @@ fn a_control_takes_its_press_under_a_later_body_sized_widget() { /// Over a canvas's background part, a root nested in its body (a text editor) and a member shown after it (a text /// frame with a corner handle at the editor's corner): exactly one of them starts each press, the one whose shape is -/// on top under it. +/// on top under it and takes both its click and its drag. #[trial] fn one_root_starts_a_press_where_roots_overlap_on_a_layer() { const CORNER: PartKey = PartKey::new("corner"); @@ -357,7 +357,7 @@ fn one_root_starts_a_press_where_roots_overlap_on_a_layer() { .member(ProbeMember::new("editor", rect(200.0, 150.0, 200.0, 100.0), Sense::CLICK | Sense::DRAG).nested()) .member( ProbeMember::new("frame", rect(100.0, 100.0, 500.0, 300.0), Sense::empty()) - .part(ProbePart::new(CORNER, rect(194.0, 144.0, 12.0, 12.0), Sense::DRAG).level(HitLevel::Handle, 1)), + .part(ProbePart::new(CORNER, rect(194.0, 144.0, 12.0, 12.0), Sense::CLICK | Sense::DRAG).level(HitLevel::Handle, 1)), ); let (mut show_frame, log) = scene(vec![canvas]); let mut frames = Frames::new();