freya_elements/events/
touch.rs

1use torin::geometry::CursorPoint;
2pub use winit::event::{
3    Force,
4    TouchPhase,
5};
6
7use crate::{
8    events::ErasedEventData,
9    impl_event,
10};
11
12impl_event! [
13    TouchData;
14
15    /// The `touchcancel` event fires when the user cancels the touching, this is usually caused by the hardware or the OS.
16    /// Also see [`ontouchend`](crate::events::ontouchend()).
17    ///
18    /// Event Data: [`TouchData`](crate::events::TouchData)
19    ///
20    /// ### Example
21    ///
22    /// ```rust, no_run
23    /// # use freya::prelude::*;
24    /// fn app() -> Element {
25    ///     rsx!(
26    ///         rect {
27    ///             width: "100",
28    ///             height: "100",
29    ///             background: "red",
30    ///             ontouchcancel: |_| println!("Touching canceled!")
31    ///         }
32    ///     )
33    /// }
34    /// ```
35    ontouchcancel
36
37    /// The `touchend` event fires when the user stops touching an element.
38    ///
39    /// Event Data: [`TouchData`](crate::events::TouchData)
40    ///
41    /// ### Example
42    ///
43    /// ```rust, no_run
44    /// # use freya::prelude::*;
45    /// fn app() -> Element {
46    ///     rsx!(
47    ///         rect {
48    ///             width: "100",
49    ///             height: "100",
50    ///             background: "red",
51    ///             ontouchend: |_| println!("Stopped touching!")
52    ///         }
53    ///     )
54    /// }
55    /// ```
56    ontouchend
57
58    /// The `touchmove` event fires when the user is touching over an element.
59    ///
60    /// Event Data: [`TouchData`](crate::events::TouchData)
61    ///
62    /// ### Example
63    ///
64    /// ```rust, no_run
65    /// # use freya::prelude::*;
66    /// fn app() -> Element {
67    ///     rsx!(
68    ///         rect {
69    ///             width: "100",
70    ///             height: "100",
71    ///             background: "red",
72    ///             ontouchmove: |_| println!("Touching!")
73    ///         }
74    ///     )
75    /// }
76    /// ```
77    ontouchmove
78
79    /// The `touchstart` event fires when the user starts touching an element.
80    ///
81    /// Event Data: [`TouchData`](crate::events::TouchData)
82    ///
83    /// ### Example
84    ///
85    /// ```rust, no_run
86    /// # use freya::prelude::*;
87    /// fn app() -> Element {
88    ///     rsx!(
89    ///         rect {
90    ///             width: "100",
91    ///             height: "100",
92    ///             background: "red",
93    ///             ontouchstart: |_| println!("Started touching!")
94    ///         }
95    ///     )
96    /// }
97    /// ```
98    ontouchstart
99];
100
101/// Data of a Touch event.
102#[derive(Debug, Clone, PartialEq)]
103pub struct TouchData {
104    pub screen_coordinates: CursorPoint,
105    pub element_coordinates: CursorPoint,
106    pub finger_id: u64,
107    pub phase: TouchPhase,
108    pub force: Option<Force>,
109}
110
111impl TouchData {
112    pub fn new(
113        screen_coordinates: CursorPoint,
114        element_coordinates: CursorPoint,
115        finger_id: u64,
116        phase: TouchPhase,
117        force: Option<Force>,
118    ) -> Self {
119        Self {
120            screen_coordinates,
121            element_coordinates,
122            finger_id,
123            phase,
124            force,
125        }
126    }
127
128    /// Get the touch coordinates relative to the window bounds.
129    pub fn get_screen_coordinates(&self) -> CursorPoint {
130        self.screen_coordinates
131    }
132
133    /// Get the touch coordinates relatives to the element bounds.
134    pub fn get_element_coordinates(&self) -> CursorPoint {
135        self.element_coordinates
136    }
137
138    /// Get the finger that triggered this event.
139    pub fn get_finger_id(&self) -> u64 {
140        self.finger_id
141    }
142
143    /// Get the touch phase of this event.
144    pub fn get_touch_phase(&self) -> TouchPhase {
145        self.phase
146    }
147
148    /// Get the touch force of this event.
149    pub fn get_touch_force(&self) -> Option<Force> {
150        self.force
151    }
152}
153
154impl From<&ErasedEventData> for TouchData {
155    fn from(val: &ErasedEventData) -> Self {
156        val.downcast::<TouchData>().cloned().unwrap()
157    }
158}