1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use embedded_graphics::{prelude::Point, primitives::Rectangle};
/// Phase of a touch interaction.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TouchPhase {
/// Finger or stylus touched the surface.
Start,
/// Pointer moved while still active.
Move,
/// Pointer was released.
End,
/// Gesture was cancelled by the system.
Cancel,
}
/// One touch sample routed through the UI framework.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TouchEvent {
/// Absolute point in display coordinates.
pub point: Point,
/// Gesture phase for this sample.
pub phase: TouchPhase,
/// Monotonic timestamp in milliseconds.
pub timestamp_ms: u32,
}
impl TouchEvent {
/// Creates a new touch event.
pub const fn new(point: Point, phase: TouchPhase, timestamp_ms: u32) -> Self {
Self {
point,
phase,
timestamp_ms,
}
}
/// Returns `true` for touch release events.
pub fn is_release(self) -> bool {
matches!(self.phase, TouchPhase::End)
}
/// Returns `true` while the touch is active.
pub fn is_active(self) -> bool {
matches!(self.phase, TouchPhase::Start | TouchPhase::Move)
}
/// Returns `true` when the point lies within `frame`.
pub fn within(self, frame: Rectangle) -> bool {
let left = frame.top_left.x;
let top = frame.top_left.y;
let right = left + frame.size.width as i32;
let bottom = top + frame.size.height as i32;
self.point.x >= left && self.point.x < right && self.point.y >= top && self.point.y < bottom
}
}