Skip to main content

faststep/
events.rs

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