nativeview_layout/scene/pointer/mod.rs
1//! Core pointer resolution: one host-agnostic stage that turns untargeted raw pointer samples
2//! (`EventFlags::RAW_POINTER`, see `nativeview_abi::EventBatchBuf::push_pointer_raw`) into the
3//! targeted W3C Pointer Events stream every app sees, on every host.
4//!
5//! Per sample the stage runs, in order:
6//! 1. **Top-layer press** (primary `PointerDown`): the HTML "hide popovers until" walk
7//! ([`super::top_layer_press`]) emits the light-dismiss `close` events; a press outside the
8//! topmost modal is swallowed ([`PointerOutcomeFlags::MODAL_BARRIER`]) until it ends.
9//! 2. **Hit-test / capture**: a captured pointer targets its capture node; otherwise the deepest
10//! interaction target ([`PointerHitTest`]). Captures are W3C: pending targets
11//! ([`InteractionTracker::pointers_mut`]) switch before the pointer's next sample, emitting
12//! `LostPointerCapture` / `GotPointerCapture`; touch and pen capture their down target
13//! implicitly; a `capture_on_primary_down` owner ([`nativeview_engine::ext::input::capture_owner`])
14//! claims the pointer ([`PointerOutcomeFlags::CLAIMED`]).
15//! 3. **`PointerTable` + tracker**: `:hover` follows the primary hover-capable pointer, `:active`
16//! and `Press` the primary pointer only; a contact beyond the table's capacity is refused
17//! ([`PointerOutcomeFlags::REFUSED`]) with a `PointerCancel`.
18//! 4. **Synthesis**: non-bubbling `PointerLeave` / `PointerEnter` transitions, the targeted
19//! pointer event, capture changes, and `Press` on a primary release over the press chain.
20//!
21//! Resolution is split in two so a hit tester may borrow the scene (or the backend owning it):
22//! [`PointerProbe::new`] reads (hits, top-layer decision, capture owner) and
23//! [`LaidOutScene::apply_pointer`] mutates the tracker. [`LaidOutScene::resolve_pointer`] runs
24//! both against the scene's own hit test (`SurfaceRuntime`); the Skia backend probes with its
25//! paint-accurate hit test. Zero heap allocations in steady state.
26
27mod geometry;
28mod resolve;
29
30use nativeview_abi::{
31 is_raw_pointer_kind, EventBatchBuf, EventFlags, InteractionEvent, NodeId, TimestampUs, ValidatedEvent,
32};
33use nativeview_engine::tracker::{PointerKey, PointerType};
34use nativeview_store::NodeStore;
35
36pub use geometry::SceneGeometry;
37pub use nativeview_abi::runtime::PointerOutcomeFlags;
38
39use super::{LaidOutScene, ResolvedOverlay, TopLayerPress};
40use crate::StoreLayoutEngine;
41
42/// The geometry queries the stage needs. Backends with paint-accurate state (live animated
43/// transforms, the painted stacking plan) implement it over their own hit test.
44pub trait PointerHitTest {
45 /// The deepest interaction target at `(x, y)` (falling back to any event target).
46 fn hit_interaction(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId>;
47 /// The deepest painted node at `(x, y)`, unfiltered (the top-layer press hit).
48 fn hit_painted(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId>;
49 /// Whether `(x, y)` lies inside the (transform-aware) box of open overlay `ro`.
50 fn overlay_contains(&self, store: &NodeStore, ro: ResolvedOverlay, x: f32, y: f32) -> bool;
51}
52
53impl<L: StoreLayoutEngine> PointerHitTest for LaidOutScene<L> {
54 #[inline]
55 fn hit_interaction(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
56 self.hit_test_interaction(store, x, y).or_else(|| self.hit_test(store, x, y))
57 }
58
59 #[inline]
60 fn hit_painted(&self, store: &NodeStore, x: f32, y: f32) -> Option<NodeId> {
61 self.hit_test_painted(store, x, y)
62 }
63
64 #[inline]
65 fn overlay_contains(&self, store: &NodeStore, ro: ResolvedOverlay, x: f32, y: f32) -> bool {
66 self.overlay_contains_point(store, ro, x, y)
67 }
68}
69
70/// What one raw sample resolved to.
71#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
72pub struct PointerResolution {
73 /// `NV_POINTER_OUTCOME_*` bits for the host.
74 pub outcome: PointerOutcomeFlags,
75 /// A pseudo-state (`:hover`, `:active`) changed: republish / repaint.
76 pub changed: bool,
77 /// The change re-ran layout (state rules patching the box).
78 pub relayout: bool,
79 /// Primary release over the press chain: the `Press` target.
80 pub clicked: Option<NodeId>,
81 /// The sample's pointer is captured after it (the capture target).
82 pub capture: Option<NodeId>,
83 /// Primary button-0 `Down`: the deepest painted node (hosts reuse it for press focus).
84 pub painted: Option<NodeId>,
85}
86
87/// The read-only half of resolution (see the module docs).
88#[derive(Debug, Clone, Copy)]
89pub struct PointerProbe {
90 pub key: PointerKey,
91 /// The sample's kind: one of [`is_raw_pointer_kind`].
92 pub phase: InteractionEvent,
93 /// The pointer button of a `Down` / `Up` (`0` = primary).
94 pub button: u8,
95 pub x: f32,
96 pub y: f32,
97 pub timestamp: TimestampUs,
98 /// The deepest interaction target (`None` while captured, for `Cancel` / `Leave`).
99 pub hit: Option<NodeId>,
100 /// Primary `Down`: the deepest painted node (hosts reuse it for press focus).
101 pub painted: Option<NodeId>,
102 /// The valid capture the sample routes to (pending capture applied, invalid targets dropped).
103 pub capture: Option<NodeId>,
104 /// `Down`: the declarative `capture_on_primary_down` owner of the hit.
105 pub owner: Option<NodeId>,
106 /// Primary `Down`: the top-layer decision (its closes are already in the output batch).
107 pub press: Option<TopLayerPress>,
108}
109
110impl PointerProbe {
111 /// Probes raw sample `ev` (`None` when it is not a raw pointer sample) against `scene`'s
112 /// tracker and `hit`, appending the top-layer light-dismiss `close` events to `out`.
113 pub fn new<L: StoreLayoutEngine>(
114 scene: &LaidOutScene<L>,
115 store: &NodeStore,
116 ev: &ValidatedEvent<'_>,
117 hit: &impl PointerHitTest,
118 out: &mut EventBatchBuf,
119 ) -> Option<Self> {
120 if !ev.is_raw_pointer() {
121 return None;
122 }
123 let phase = ev.interaction().filter(|&k| is_raw_pointer_kind(k))?;
124 let key = PointerKey::new(PointerType::try_from_u8(ev.pointer_type())?, ev.pointer_id());
125 let (x, y) = (ev.x(), ev.y());
126 let mut probe = Self {
127 key,
128 phase,
129 button: ev.pointer_button(),
130 x,
131 y,
132 timestamp: ev.timestamp(),
133 hit: None,
134 painted: None,
135 capture: None,
136 owner: None,
137 press: None,
138 };
139 let tracker = &scene.interaction;
140 probe.capture = tracker
141 .pointers()
142 .get(key)
143 .map(|s| s.pending_capture)
144 .filter(|&n| scene.is_capture_target(store, n));
145 let begins = phase == InteractionEvent::PointerDown && probe.button == 0;
146 let hits = match phase {
147 InteractionEvent::PointerDown => begins || probe.capture.is_none(),
148 InteractionEvent::PointerMove => probe.capture.is_none(),
149 InteractionEvent::PointerUp => true,
150 _ => false,
151 };
152 if hits {
153 probe.hit = hit.hit_interaction(store, x, y);
154 }
155 if begins {
156 probe.owner = probe
157 .hit
158 .and_then(|h| nativeview_engine::ext::input::capture_owner(store, h))
159 .filter(|&o| !scene.is_subtree_disabled(store, o));
160 if tracker.pointers().would_be_primary(key) {
161 probe.painted = hit.hit_painted(store, x, y);
162 let press = scene.top_layer_press_at(store, probe.painted, |ro| hit.overlay_contains(store, ro, x, y));
163 for node in press.light_dismissed(scene.open_overlays()) {
164 out.push_custom_with_flags(ev.surface(), node, ev.timestamp(), "close", &[], EventFlags::NO_BUBBLE);
165 }
166 probe.press = Some(press);
167 }
168 }
169 Some(probe)
170 }
171}
172
173impl<L: StoreLayoutEngine> LaidOutScene<L> {
174 /// Whether `node` may hold pointer capture: connected and not in a disabled subtree.
175 #[inline]
176 pub fn is_capture_target(&self, store: &NodeStore, node: NodeId) -> bool {
177 !node.is_null() && store.is_connected(node) && !self.is_subtree_disabled(store, node)
178 }
179
180 /// Caps simultaneously tracked pointers (`NvGestureSettings::max_pointers`).
181 #[inline]
182 pub fn set_max_pointers(&mut self, max: usize) {
183 self.interaction.pointers_mut().set_limit(max);
184 }
185
186 /// The top-layer press decision over the open overlays for press hit `painted`.
187 pub fn top_layer_press_at(
188 &self,
189 store: &NodeStore,
190 painted: Option<NodeId>,
191 contains: impl Fn(ResolvedOverlay) -> bool,
192 ) -> TopLayerPress {
193 super::top_layer_press(
194 self.open_overlays(),
195 self.topmost_modal(),
196 painted,
197 |node, ancestor| nativeview_engine::is_within_subtree(store, node, ancestor),
198 |&ro| contains(ro),
199 )
200 }
201
202 /// Resolves raw sample `ev` against the scene's own hit test, appending the targeted events
203 /// to `out` (`None` when `ev` is not a raw pointer sample).
204 pub fn resolve_pointer(
205 &mut self,
206 store: &NodeStore,
207 ev: &ValidatedEvent<'_>,
208 out: &mut EventBatchBuf,
209 ) -> Option<PointerResolution> {
210 let probe = PointerProbe::new(self, store, ev, self, out)?;
211 Some(self.apply_pointer(store, ev, &probe, out))
212 }
213}