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