Skip to main content

ferroui_base/input/
swipe_gesture_event_args.rs

1use super::{InputElement, SwipeDirection};
2use crate::interactivity::RoutedEventArgs;
3use crate::{ferro_routed_event_args, Vector};
4use std::sync::atomic::{AtomicI32, Ordering};
5
6static NEXT_ID: AtomicI32 = AtomicI32::new(0);
7
8/// Provides data for swipe gesture events.
9#[derive(Clone)]
10pub struct SwipeGestureEventArgs {
11    base: RoutedEventArgs,
12    id: i32,
13    delta: Vector,
14    velocity: Vector,
15    swipe_direction: SwipeDirection,
16}
17
18ferro_routed_event_args!(SwipeGestureEventArgs: RoutedEventArgs);
19
20impl SwipeGestureEventArgs {
21    /// Creates swipe gesture event args.
22    ///
23    /// `id` is the unique identifier for this gesture, `delta` the pixel
24    /// delta since the last event and `velocity` the current swipe velocity
25    /// in pixels per second.
26    pub fn new(id: i32, delta: Vector, velocity: Vector) -> Self {
27        let swipe_direction = if delta.x.abs() >= delta.y.abs() {
28            if delta.x <= 0.0 {
29                SwipeDirection::Right
30            } else {
31                SwipeDirection::Left
32            }
33        } else if delta.y <= 0.0 {
34            SwipeDirection::Down
35        } else {
36            SwipeDirection::Up
37        };
38
39        Self {
40            base: RoutedEventArgs::with_event(InputElement::swipe_gesture_event()),
41            id,
42            delta,
43            velocity,
44            swipe_direction,
45        }
46    }
47
48    /// The unique identifier for this gesture sequence.
49    #[inline]
50    pub fn id(&self) -> i32 {
51        self.id
52    }
53
54    /// The pixel delta since the last event.
55    #[inline]
56    pub fn delta(&self) -> Vector {
57        self.delta
58    }
59
60    /// The current swipe velocity in pixels per second.
61    #[inline]
62    pub fn velocity(&self) -> Vector {
63        self.velocity
64    }
65
66    /// The direction of the dominant swipe axis.
67    #[inline]
68    pub fn swipe_direction(&self) -> SwipeDirection {
69        self.swipe_direction
70    }
71
72    #[allow(dead_code)]
73    pub(crate) fn get_next_free_id() -> i32 {
74        NEXT_ID.fetch_add(1, Ordering::Relaxed) + 1
75    }
76}
77
78/// Provides data for the swipe gesture ended event.
79#[derive(Clone)]
80pub struct SwipeGestureEndedEventArgs {
81    base: RoutedEventArgs,
82    id: i32,
83    velocity: Vector,
84}
85
86ferro_routed_event_args!(SwipeGestureEndedEventArgs: RoutedEventArgs);
87
88impl SwipeGestureEndedEventArgs {
89    /// Creates swipe gesture ended event args.
90    ///
91    /// `id` is the unique identifier for this gesture and `velocity` the
92    /// swipe velocity at release in pixels per second.
93    pub fn new(id: i32, velocity: Vector) -> Self {
94        Self { base: RoutedEventArgs::with_event(InputElement::swipe_gesture_ended_event()), id, velocity }
95    }
96
97    /// The unique identifier for this gesture sequence.
98    #[inline]
99    pub fn id(&self) -> i32 {
100        self.id
101    }
102
103    /// The swipe velocity at release in pixels per second.
104    #[inline]
105    pub fn velocity(&self) -> Vector {
106        self.velocity
107    }
108}