1use crate::{KeyModifiers, Length, ListModel, Property, RowKey, RowLifetime, VecModel};
2use std::{
3 cell::{Cell, OnceCell},
4 fmt,
5 ops::Range,
6 rc::Rc,
7};
8
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
10pub enum PointerEventKind {
11 Down,
12 Move,
13 Up,
14 #[default]
15 Cancel,
16}
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19pub enum PointerButton {
20 #[default]
21 Primary,
22 Secondary,
23 Middle,
24}
25
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28pub struct PointerButtons(u8);
29
30impl PointerButtons {
31 pub fn contains(self, button: PointerButton) -> bool {
32 self.0 & (1 << button as u8) != 0
33 }
34}
35
36impl<const N: usize> From<[PointerButton; N]> for PointerButtons {
37 fn from(buttons: [PointerButton; N]) -> Self {
38 Self(
39 buttons
40 .into_iter()
41 .fold(0, |mask, button| mask | (1 << button as u8)),
42 )
43 }
44}
45
46#[derive(Clone, Copy, Debug, Default, PartialEq)]
47pub struct PointerEvent {
48 pub kind: PointerEventKind,
49 pub button: PointerButton,
50 pub x: Length,
51 pub y: Length,
52 pub inside: bool,
53 pub is_touch: bool,
54 pub touch_finger_id: i64,
56 pub modifiers: KeyModifiers,
57}
58
59#[derive(Clone, Copy, Debug, Default, PartialEq)]
60pub struct TouchPoint {
61 pub id: i64,
62 pub x: Length,
63 pub y: Length,
64 pub start_x: Length,
65 pub start_y: Length,
66 pub inside: bool,
67}
68
69#[derive(Clone, Default, PartialEq, Eq)]
71pub struct TouchPoints(VecModel<TouchPoint>);
72
73impl From<Vec<TouchPoint>> for TouchPoints {
74 fn from(points: Vec<TouchPoint>) -> Self {
75 Self(points.into())
76 }
77}
78
79impl TouchPoints {
80 pub fn snapshot(&self) -> Vec<TouchPoint> {
81 self.0.snapshot()
82 }
83
84 #[doc(hidden)]
85 pub fn values_eq(&self, other: &Self) -> bool {
86 self.0.values_eq(&other.0)
87 }
88}
89
90impl ListModel for TouchPoints {
91 type Item = TouchPoint;
92
93 fn row_count(&self) -> usize {
94 self.0.row_count()
95 }
96
97 fn item(&self, row: usize) -> TouchPoint {
98 self.0.item(row)
99 }
100
101 fn row_key(&self, row: usize) -> RowKey {
102 ListModel::row_key(&self.0, row)
103 }
104
105 fn row_lifetime(&self, key: RowKey) -> RowLifetime {
106 self.0.row_lifetime(key)
107 }
108
109 fn structure_stamp(&self) -> (u64, u64) {
110 self.0.structure_stamp()
111 }
112
113 fn index_of_key(&self, key: RowKey) -> Option<usize> {
114 self.0.index_of_key(key)
115 }
116
117 fn visit_items(&self, visit: impl FnMut(usize, RowKey, &TouchPoint)) {
118 self.0.visit_items(visit);
119 }
120
121 fn visit_row_keys(&self, range: Range<usize>, visit: impl FnMut(RowKey)) {
122 self.0.visit_row_keys(range, visit);
123 }
124}
125
126#[derive(Default)]
127struct PointerState {
128 pressed: Property<bool>,
129 contains_press: Property<bool>,
130 has_hover: Property<bool>,
131 mouse_x: Property<Length>,
132 mouse_y: Property<Length>,
133 pressed_x: Property<Length>,
134 pressed_y: Property<Length>,
135 mouse_press: Cell<Option<bool>>,
136 touch_points: OnceCell<VecModel<TouchPoint>>,
137}
138
139#[derive(Clone, Default)]
141#[doc(hidden)]
142pub struct PointerHandle(Rc<PointerState>);
143
144impl fmt::Debug for PointerHandle {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 f.debug_struct("PointerHandle").finish_non_exhaustive()
147 }
148}
149
150impl PartialEq for PointerHandle {
151 fn eq(&self, other: &Self) -> bool {
152 Rc::ptr_eq(&self.0, &other.0)
153 }
154}
155
156impl PointerHandle {
157 pub fn pressed(&self) -> bool {
158 self.0.pressed.get(|| false)
159 }
160
161 pub fn contains_press(&self) -> bool {
162 self.0.contains_press.get(|| false)
163 }
164
165 pub fn has_hover(&self) -> bool {
166 self.0.has_hover.get(|| false)
167 }
168
169 pub fn mouse_x(&self) -> Length {
170 self.0.mouse_x.get(Length::default)
171 }
172
173 pub fn mouse_y(&self) -> Length {
174 self.0.mouse_y.get(Length::default)
175 }
176
177 pub fn pressed_x(&self) -> Length {
178 self.0.pressed_x.get(Length::default)
179 }
180
181 pub fn pressed_y(&self) -> Length {
182 self.0.pressed_y.get(Length::default)
183 }
184
185 pub fn active_touch_count(&self) -> i64 {
186 self.touch_model().len() as i64
187 }
188
189 pub fn touch_points(&self) -> TouchPoints {
190 TouchPoints(self.touch_model().clone())
191 }
192
193 fn touch_model(&self) -> &VecModel<TouchPoint> {
194 self.0.touch_points.get_or_init(VecModel::default)
195 }
196
197 pub fn publish_hover(&self, position: Option<(Length, Length)>) {
198 if let Some((x, y)) = position {
199 self.0.mouse_x.set_if_changed(x);
200 self.0.mouse_y.set_if_changed(y);
201 }
202 self.0.has_hover.set_if_changed(position.is_some());
203 }
204
205 pub fn publish_event(&self, event: &PointerEvent) {
206 use PointerEventKind::{Cancel, Down, Move, Up};
207
208 if event.kind != Cancel {
209 self.0.mouse_x.set_if_changed(event.x);
210 self.0.mouse_y.set_if_changed(event.y);
211 }
212 if event.kind == Down {
213 self.0.pressed_x.set_if_changed(event.x);
214 self.0.pressed_y.set_if_changed(event.y);
215 }
216 if event.touch_finger_id != 0 {
217 let points = self.touch_model();
218 let mut row = None;
219 points.visit_rows_untracked(usize::MAX, |index, _, point| {
220 if point.id == event.touch_finger_id {
221 row = Some((index, *point));
222 }
223 });
224 match event.kind {
225 Down | Move => {
226 if let Some((index, point)) = row {
227 points.set_row(
228 index,
229 TouchPoint {
230 x: event.x,
231 y: event.y,
232 inside: event.inside,
233 ..point
234 },
235 );
236 } else if event.kind == Down {
237 points.push(TouchPoint {
238 id: event.touch_finger_id,
239 x: event.x,
240 y: event.y,
241 start_x: event.x,
242 start_y: event.y,
243 inside: event.inside,
244 });
245 }
246 }
247 Up | Cancel => {
248 if let Some((index, _)) = row {
249 points.remove(index);
250 }
251 }
252 }
253 } else {
254 match event.kind {
255 Down => self.0.mouse_press.set(Some(event.inside)),
256 Move if self.0.mouse_press.get().is_some() => {
257 self.0.mouse_press.set(Some(event.inside))
258 }
259 Up | Cancel => self.0.mouse_press.set(None),
260 Move => {}
261 }
262 }
263 let mouse = self.0.mouse_press.get();
264 let mut inside = mouse.unwrap_or(false);
265 let mut pressed = mouse.is_some();
266 if let Some(points) = self.0.touch_points.get() {
267 points.visit_rows_untracked(usize::MAX, |_, _, point| {
268 pressed = true;
269 inside |= point.inside;
270 });
271 }
272 self.0.pressed.set_if_changed(pressed);
273 self.0.contains_press.set_if_changed(inside);
274 }
275}