Skip to main content

argui_core/
input.rs

1use crate::{Modifiers, Point};
2
3/// Platform-independent timing and distance thresholds for pointer gestures.
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct PointerSettings {
6    multi_click_interval: std::time::Duration,
7    multi_click_distance: f32,
8    long_press_interval: std::time::Duration,
9    touch_slop: f32,
10}
11
12impl Default for PointerSettings {
13    fn default() -> Self {
14        Self {
15            multi_click_interval: std::time::Duration::from_millis(500),
16            multi_click_distance: 5.0,
17            long_press_interval: std::time::Duration::from_millis(500),
18            touch_slop: 10.0,
19        }
20    }
21}
22
23impl PointerSettings {
24    #[must_use]
25    pub fn multi_click(mut self, interval: std::time::Duration, distance: f32) -> Self {
26        assert!(distance.is_finite() && distance >= 0.0);
27        self.multi_click_interval = interval;
28        self.multi_click_distance = distance;
29        self
30    }
31
32    #[must_use]
33    pub fn long_press(mut self, interval: std::time::Duration, touch_slop: f32) -> Self {
34        assert!(touch_slop.is_finite() && touch_slop >= 0.0);
35        self.long_press_interval = interval;
36        self.touch_slop = touch_slop;
37        self
38    }
39
40    #[must_use]
41    pub const fn multi_click_interval(self) -> std::time::Duration {
42        self.multi_click_interval
43    }
44
45    #[must_use]
46    pub const fn multi_click_distance(self) -> f32 {
47        self.multi_click_distance
48    }
49
50    #[must_use]
51    pub const fn long_press_interval(self) -> std::time::Duration {
52        self.long_press_interval
53    }
54
55    #[must_use]
56    pub const fn touch_slop(self) -> f32 {
57        self.touch_slop
58    }
59}
60
61/// Device-independent wheel data. Line deltas remain distinct until a scroll
62/// container applies its configured logical line size.
63#[derive(Clone, Copy, Debug, PartialEq)]
64pub enum ScrollDelta {
65    Lines(Point),
66    Pixels(Point),
67}
68
69#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
70pub struct PointerId(u64);
71
72impl PointerId {
73    pub const MOUSE: Self = Self(0);
74
75    #[must_use]
76    pub const fn new(value: u64) -> Self {
77        Self(value)
78    }
79
80    #[must_use]
81    pub const fn get(self) -> u64 {
82        self.0
83    }
84}
85
86#[derive(Clone, Copy, Debug, Eq, PartialEq)]
87pub enum PointerKind {
88    Mouse,
89    Touch,
90    Pen,
91}
92
93#[derive(Clone, Copy, Debug, Eq, PartialEq)]
94pub enum PointerPhase {
95    Entered,
96    Moved,
97    Pressed,
98    Released,
99    Left,
100    Cancelled,
101}
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq)]
104pub enum PointerButton {
105    Primary,
106    Secondary,
107    Middle,
108    Back,
109    Forward,
110    Other(u16),
111}
112
113#[derive(Clone, Copy, Debug, PartialEq)]
114pub struct PointerEvent {
115    pub id: PointerId,
116    pub kind: PointerKind,
117    pub phase: PointerPhase,
118    pub position: Point,
119    pub button: Option<PointerButton>,
120    pub buttons: u16,
121    pub pressure: Option<f32>,
122    pub primary: bool,
123    pub modifiers: Modifiers,
124    pub timestamp: std::time::Duration,
125}
126
127impl PointerEvent {
128    #[must_use]
129    pub const fn mouse(phase: PointerPhase, position: Point) -> Self {
130        Self {
131            id: PointerId::MOUSE,
132            kind: PointerKind::Mouse,
133            phase,
134            position,
135            button: None,
136            buttons: 0,
137            pressure: None,
138            primary: true,
139            modifiers: Modifiers {
140                shift: false,
141                control: false,
142                alt: false,
143                super_key: false,
144            },
145            timestamp: std::time::Duration::ZERO,
146        }
147    }
148}