Skip to main content

gdk/
event_touch.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5/// Used for touch events.
6/// `type` field will be one of [`EventType::TouchBegin`][crate::EventType::TouchBegin], [`EventType::TouchUpdate`][crate::EventType::TouchUpdate],
7/// [`EventType::TouchEnd`][crate::EventType::TouchEnd] or [`EventType::TouchCancel`][crate::EventType::TouchCancel].
8///
9/// Touch events are grouped into sequences by means of the `sequence`
10/// field, which can also be obtained with `gdk_event_get_event_sequence()`.
11/// Each sequence begins with a [`EventType::TouchBegin`][crate::EventType::TouchBegin] event, followed by
12/// any number of [`EventType::TouchUpdate`][crate::EventType::TouchUpdate] events, and ends with a [`EventType::TouchEnd`][crate::EventType::TouchEnd]
13/// (or [`EventType::TouchCancel`][crate::EventType::TouchCancel]) event. With multitouch devices, there may be
14/// several active sequences at the same time.
15#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct EventTouch(crate::Event);
17
18event_wrapper!(EventTouch, GdkEventTouch);
19event_subtype!(
20    EventTouch,
21    ffi::GDK_TOUCH_BEGIN | ffi::GDK_TOUCH_UPDATE | ffi::GDK_TOUCH_END | ffi::GDK_TOUCH_CANCEL
22);
23
24impl EventTouch {
25    #[doc(alias = "get_time")]
26    pub fn time(&self) -> u32 {
27        self.as_ref().time
28    }
29
30    #[doc(alias = "get_position")]
31    pub fn position(&self) -> (f64, f64) {
32        let x = self.as_ref().x;
33        let y = self.as_ref().y;
34        (x, y)
35    }
36
37    #[doc(alias = "get_state")]
38    pub fn state(&self) -> crate::ModifierType {
39        unsafe { from_glib(self.as_ref().state) }
40    }
41
42    pub fn is_emulating_pointer(&self) -> bool {
43        unsafe { from_glib(self.as_ref().emulating_pointer) }
44    }
45
46    #[doc(alias = "get_device")]
47    pub fn device(&self) -> Option<crate::Device> {
48        unsafe { from_glib_none(self.as_ref().device) }
49    }
50
51    #[doc(alias = "get_axes")]
52    pub fn axes(&self) -> Option<(f64, f64)> {
53        let axes = self.as_ref().axes;
54
55        if axes.is_null() {
56            None
57        } else {
58            unsafe { Some((*axes, *axes.offset(1))) }
59        }
60    }
61
62    #[doc(alias = "get_root")]
63    pub fn root(&self) -> (f64, f64) {
64        let x_root = self.as_ref().x_root;
65        let y_root = self.as_ref().y_root;
66        (x_root, y_root)
67    }
68
69    #[doc(alias = "get_event_sequence")]
70    pub fn event_sequence(&self) -> Option<crate::EventSequence> {
71        unsafe { from_glib_none(self.as_ref().sequence) }
72    }
73}