Skip to main content

nativeview_engine/ext/input/
mod.rs

1//! Shared input extension points: what every host runs around an app dispatch, so backends and
2//! hosts keep only platform glue.
3//!
4//! - [`InputInterceptor`] (X1): pre-dispatch hooks; the first that consumes an event stops it
5//!   before the app sees it (web key registry and `nativeview-tv-web`, Skia shell key routing).
6//! - [`DefaultAction`] (X2): runs on an event the app did not consume, with the same shape. The
7//!   close request is the statically ordered built-in #0 (`nativeview_layout::CloseRequestAction`); a
8//!   registered chain follows it (`nativeview-tv-web` Back → overlay dismissal, HTML CloseWatcher order).
9//! - [`PointerGeometry`] (X3): the box a pointer norm is relative to (laid-out scene, Skia visual
10//!   rects, Web DOM rects); [`annotate_pointer_norms`] fills `TARGET_LOCAL_NORM` with it.
11//!
12//! Hooks live in an [`InputChain`]: ordered stably by `(`[`InputPriority`] DESC, registration
13//! order ASC)`, each entry carries an [`InputKinds`] mask (cached as a chain-wide union), an
14//! empty chain or non-matching kind costs one branch, and dispatch never allocates.
15
16mod chain;
17mod pointer;
18
19use std::cell::RefCell;
20
21use nativeview_abi::{EventSet, InteractionEvent, NodeId, ValidatedEvent};
22
23use crate::LayoutRect;
24
25pub use chain::{InputChain, InputHookId, InputPriority};
26pub use pointer::{
27    annotate_pointer_norms, capture_owner, has_ancestor_listener, has_direct_listener, local_norm,
28    needs_pointer_norms, pointer_norm_owner,
29};
30
31/// What a hook did with an event.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
33pub enum Disposition {
34    /// Not handled: later hooks (and, for interceptors, the app) see the event.
35    #[default]
36    PassThrough,
37    /// Handled: stop here, keep the platform's own default action (e.g. a browser's native
38    /// activation `click`).
39    Consumed,
40    /// Handled: stop here and suppress the platform default (`preventDefault()`).
41    ConsumedPreventDefault,
42}
43
44impl Disposition {
45    #[inline]
46    pub const fn is_consumed(self) -> bool {
47        !matches!(self, Self::PassThrough)
48    }
49
50    #[inline]
51    pub const fn prevents_default(self) -> bool {
52        matches!(self, Self::ConsumedPreventDefault)
53    }
54}
55
56/// Per-entry kind mask: the interaction kinds a hook is called for.
57pub type InputKinds = EventSet<InteractionEvent>;
58
59/// [`InputKinds`] of key hooks.
60pub const KEY_DOWN: InputKinds = InputKinds::from_raw(1 << InteractionEvent::KeyDown as u32);
61
62/// Pre-dispatch hook (X1). `cx` is the host's context: the facts only the host knows (Web: DOM
63/// target facts; Skia shell: the session it acts on).
64pub trait InputInterceptor<Cx: ?Sized> {
65    fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;
66
67    /// Whether this interceptor currently has active rules/work requiring the host to keep input
68    /// listeners attached. Defaults to `true`.
69    #[inline]
70    fn is_active(&self) -> bool {
71        true
72    }
73}
74
75/// Post-dispatch hook (X2) for an event the app did not consume; same shape as
76/// [`InputInterceptor`].
77pub trait DefaultAction<Cx: ?Sized> {
78    fn run(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;
79}
80
81/// Geometry pointer norms are relative to (X3).
82pub trait PointerGeometry {
83    /// `node`'s visual box in the events' coordinate space (scroll offsets and transforms
84    /// applied); `None` when it has none.
85    fn local_rect(&self, node: NodeId) -> Option<LayoutRect>;
86}
87
88/// Registered [`InputInterceptor`]s.
89pub type InterceptorChain<Cx> = InputChain<dyn InputInterceptor<Cx>>;
90/// Registered [`DefaultAction`]s.
91pub type DefaultActionChain<Cx> = InputChain<dyn DefaultAction<Cx>>;
92
93impl<Cx: ?Sized> InputChain<dyn InputInterceptor<Cx>> {
94    /// Returns `true` when at least one registered interceptor reports [`InputInterceptor::is_active`].
95    #[inline]
96    pub fn has_active(&self) -> bool {
97        self.any_hook(|hook| hook.is_active())
98    }
99
100    /// Runs the matching interceptors in order until one consumes `ev`.
101    #[inline]
102    pub fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
103        self.run(ev, |hook| hook.intercept(ev, cx))
104    }
105
106    /// [`Self::intercept`] through a shared cell: interceptors may register, remove or dispatch
107    /// again while they run (see [`InputChain::run_shared`]).
108    #[inline]
109    pub fn intercept_shared(cell: &RefCell<Self>, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
110        Self::run_shared(cell, ev, |hook| hook.intercept(ev, cx))
111    }
112}
113
114impl<Cx: ?Sized> InputChain<dyn DefaultAction<Cx>> {
115    /// Runs the matching default actions in order until one consumes `ev`.
116    #[inline]
117    pub fn run_default(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
118        self.run(ev, |hook| hook.run(ev, cx))
119    }
120
121    /// [`Self::run_default`] through a shared cell (see [`InputChain::run_shared`]).
122    #[inline]
123    pub fn run_default_shared(cell: &RefCell<Self>, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
124        Self::run_shared(cell, ev, |hook| hook.run(ev, cx))
125    }
126}
127
128#[cfg(test)]
129mod tests;