Skip to main content

nativeview_abi/runtime/
gesture.rs

1//! Gesture policy defaults ([`GESTURE_POLICY`], `nv_policy!`) and the live OS values hosts report
2//! through [`super::NvRuntimeApi::set_gesture_settings`] ([`NvGestureSettings`]).
3//!
4//! The policy is the fallback; hosts pass what their OS reports (Android `ViewConfiguration`,
5//! Win32 `SM_CXDRAG` / `GetDoubleClickTime`, GTK `GtkSettings`, Qt `QStyleHints`, Apple
6//! `NSEvent.doubleClickInterval`, Flutter `MediaQuery.gestureSettings`), once and again on change
7//! (Android's long-press timeout is an accessibility setting). A `0` field keeps the policy value.
8
9use core::mem::{align_of, size_of};
10
11use nativeview_abi_macros::NvAbi;
12
13use crate::id::DurationMs;
14use crate::policy::{PolicyConsumer, PolicyHome, PolicyLang};
15
16crate::nv_policy! {
17    /// Gesture recognition defaults every host and the core pointer resolver share. The Flutter
18    /// canvas reports `MediaQuery.gestureSettings` over these defaults and, until it sends raw
19    /// samples (P5b), applies the touch slop itself.
20    pub policy GESTURE_POLICY = "gesture" in PolicyHome::Core, consumers [
21        PolicyConsumer { lang: PolicyLang::Dart, path: "packages/backends/flutter/lib/" },
22    ];
23    /// Touch / pen travel (logical px) before a contact stops being a tap (Android 8 dp).
24    pub const TOUCH_SLOP_DP: u32 = 8;
25    /// Mouse travel (logical px) before a press becomes a drag (Win32 `SM_CXDRAG` / 2).
26    pub const MOUSE_DRAG_SLOP_PX: u32 = 4;
27    /// Hold time (ms) before a touch contact requests a context menu.
28    pub const LONG_PRESS_MS: u32 = 500;
29    /// Maximum gap (ms) between the taps of a double tap / click.
30    pub const DOUBLE_TAP_MS: u32 = 300;
31    /// Minimum release velocity (logical px/s) that starts a fling.
32    pub const MIN_FLING_DP_S: u32 = 50;
33    /// Capacity of the core `PointerTable` (simultaneously tracked active pointers).
34    pub const MAX_ACTIVE_POINTERS: usize = 10;
35}
36
37/// Live OS gesture settings (24 bytes); a `0` field keeps the [`GESTURE_POLICY`] default and
38/// `max_pointers` is clamped to [`MAX_ACTIVE_POINTERS`].
39#[repr(C)]
40#[derive(Debug, Clone, Copy, Default, PartialEq, NvAbi)]
41#[nativeview_abi(size = 24)]
42pub struct NvGestureSettings {
43    #[nativeview_abi(offset = 0)]
44    pub touch_slop_dp: f32,
45    #[nativeview_abi(offset = 4)]
46    pub mouse_drag_slop_px: f32,
47    #[nativeview_abi(offset = 8)]
48    pub long_press_ms: DurationMs,
49    #[nativeview_abi(offset = 12)]
50    pub double_tap_ms: DurationMs,
51    #[nativeview_abi(offset = 16)]
52    pub min_fling_dp_s: f32,
53    #[nativeview_abi(offset = 20)]
54    pub max_pointers: u32,
55}
56
57impl NvGestureSettings {
58    /// The policy defaults.
59    pub const DEFAULT: Self = Self {
60        touch_slop_dp: TOUCH_SLOP_DP as f32,
61        mouse_drag_slop_px: MOUSE_DRAG_SLOP_PX as f32,
62        long_press_ms: DurationMs(LONG_PRESS_MS),
63        double_tap_ms: DurationMs(DOUBLE_TAP_MS),
64        min_fling_dp_s: MIN_FLING_DP_S as f32,
65        max_pointers: MAX_ACTIVE_POINTERS as u32,
66    };
67
68    /// `self` with every `0` (or non-finite / negative) field replaced by its policy default and
69    /// `max_pointers` clamped to `1..=MAX_ACTIVE_POINTERS`.
70    pub fn resolved(self) -> Self {
71        let f = |v: f32, d: f32| if v.is_finite() && v > 0.0 { v } else { d };
72        let u = |v: u32, d: u32| if v > 0 { v } else { d };
73        let ms = |v: DurationMs, d: DurationMs| DurationMs(u(v.0, d.0));
74        let d = Self::DEFAULT;
75        Self {
76            touch_slop_dp: f(self.touch_slop_dp, d.touch_slop_dp),
77            mouse_drag_slop_px: f(self.mouse_drag_slop_px, d.mouse_drag_slop_px),
78            long_press_ms: ms(self.long_press_ms, d.long_press_ms),
79            double_tap_ms: ms(self.double_tap_ms, d.double_tap_ms),
80            min_fling_dp_s: f(self.min_fling_dp_s, d.min_fling_dp_s),
81            max_pointers: u(self.max_pointers, d.max_pointers).min(d.max_pointers),
82        }
83    }
84}
85
86const _: () = {
87    assert!(size_of::<NvGestureSettings>() == 24);
88    assert!(align_of::<NvGestureSettings>() == 4);
89};