Skip to main content

gtk/auto/
gesture_swipe.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{EventController, Gesture, GestureSingle, PropagationPhase, Widget, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// [`GestureSwipe`][crate::GestureSwipe] is a [`Gesture`][crate::Gesture] implementation able to recognize
16    /// swipes, after a press/move/.../move/release sequence happens, the
17    /// [`swipe`][struct@crate::GestureSwipe#swipe] signal will be emitted, providing the velocity
18    /// and directionality of the sequence at the time it was lifted.
19    ///
20    /// If the velocity is desired in intermediate points,
21    /// [`velocity()`][Self::velocity()] can be called on eg. a
22    /// [`update`][struct@crate::Gesture#update] handler.
23    ///
24    /// All velocities are reported in pixels/sec units.
25    ///
26    /// ## Signals
27    ///
28    ///
29    /// #### `swipe`
30    ///  This signal is emitted when the recognized gesture is finished, velocity
31    /// and direction are a product of previously recorded events.
32    ///
33    ///
34    /// <details><summary><h4>Gesture</h4></summary>
35    ///
36    ///
37    /// #### `begin`
38    ///  This signal is emitted when the gesture is recognized. This means the
39    /// number of touch sequences matches [`n-points`][struct@crate::Gesture#n-points], and the [`check`][struct@crate::Gesture#check]
40    /// handler(s) returned [`true`].
41    ///
42    /// Note: These conditions may also happen when an extra touch (eg. a third touch
43    /// on a 2-touches gesture) is lifted, in that situation `sequence` won't pertain
44    /// to the current set of active touches, so don't rely on this being true.
45    ///
46    ///
47    ///
48    ///
49    /// #### `cancel`
50    ///  This signal is emitted whenever a sequence is cancelled. This usually
51    /// happens on active touches when [`EventControllerExt::reset()`][crate::prelude::EventControllerExt::reset()] is called
52    /// on `gesture` (manually, due to grabs...), or the individual `sequence`
53    /// was claimed by parent widgets' controllers (see [`GestureExt::set_sequence_state()`][crate::prelude::GestureExt::set_sequence_state()]).
54    ///
55    /// `gesture` must forget everything about `sequence` as a reaction to this signal.
56    ///
57    ///
58    ///
59    ///
60    /// #### `end`
61    ///  This signal is emitted when `gesture` either stopped recognizing the event
62    /// sequences as something to be handled (the [`check`][struct@crate::Gesture#check] handler returned
63    /// [`false`]), or the number of touch sequences became higher or lower than
64    /// [`n-points`][struct@crate::Gesture#n-points].
65    ///
66    /// Note: `sequence` might not pertain to the group of sequences that were
67    /// previously triggering recognition on `gesture` (ie. a just pressed touch
68    /// sequence that exceeds [`n-points`][struct@crate::Gesture#n-points]). This situation may be detected
69    /// by checking through [`GestureExt::handles_sequence()`][crate::prelude::GestureExt::handles_sequence()].
70    ///
71    ///
72    ///
73    ///
74    /// #### `sequence-state-changed`
75    ///  This signal is emitted whenever a sequence state changes. See
76    /// [`GestureExt::set_sequence_state()`][crate::prelude::GestureExt::set_sequence_state()] to know more about the expectable
77    /// sequence lifetimes.
78    ///
79    ///
80    ///
81    ///
82    /// #### `update`
83    ///  This signal is emitted whenever an event is handled while the gesture is
84    /// recognized. `sequence` is guaranteed to pertain to the set of active touches.
85    ///
86    ///
87    /// </details>
88    ///
89    /// # Implements
90    ///
91    /// [`GestureSingleExt`][trait@crate::prelude::GestureSingleExt], [`GestureExt`][trait@crate::prelude::GestureExt], [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`]
92    #[doc(alias = "GtkGestureSwipe")]
93    pub struct GestureSwipe(Object<ffi::GtkGestureSwipe, ffi::GtkGestureSwipeClass>) @extends GestureSingle, Gesture, EventController;
94
95    match fn {
96        type_ => || ffi::gtk_gesture_swipe_get_type(),
97    }
98}
99
100impl GestureSwipe {
101    /// Returns a newly created [`Gesture`][crate::Gesture] that recognizes swipes.
102    /// ## `widget`
103    /// a [`Widget`][crate::Widget]
104    ///
105    /// # Returns
106    ///
107    /// a newly created [`GestureSwipe`][crate::GestureSwipe]
108    #[doc(alias = "gtk_gesture_swipe_new")]
109    pub fn new(widget: &impl IsA<Widget>) -> GestureSwipe {
110        skip_assert_initialized!();
111        unsafe {
112            Gesture::from_glib_full(ffi::gtk_gesture_swipe_new(widget.as_ref().to_glib_none().0))
113                .unsafe_cast()
114        }
115    }
116
117    // rustdoc-stripper-ignore-next
118    /// Creates a new builder-pattern struct instance to construct [`GestureSwipe`] objects.
119    ///
120    /// This method returns an instance of [`GestureSwipeBuilder`](crate::builders::GestureSwipeBuilder) which can be used to create [`GestureSwipe`] objects.
121    pub fn builder() -> GestureSwipeBuilder {
122        GestureSwipeBuilder::new()
123    }
124
125    /// If the gesture is recognized, this function returns [`true`] and fill in
126    /// `velocity_x` and `velocity_y` with the recorded velocity, as per the
127    /// last event(s) processed.
128    ///
129    /// # Returns
130    ///
131    /// whether velocity could be calculated
132    ///
133    /// ## `velocity_x`
134    /// return value for the velocity in the X axis, in pixels/sec
135    ///
136    /// ## `velocity_y`
137    /// return value for the velocity in the Y axis, in pixels/sec
138    #[doc(alias = "gtk_gesture_swipe_get_velocity")]
139    #[doc(alias = "get_velocity")]
140    pub fn velocity(&self) -> Option<(f64, f64)> {
141        unsafe {
142            let mut velocity_x = std::mem::MaybeUninit::uninit();
143            let mut velocity_y = std::mem::MaybeUninit::uninit();
144            let ret = from_glib(ffi::gtk_gesture_swipe_get_velocity(
145                self.to_glib_none().0,
146                velocity_x.as_mut_ptr(),
147                velocity_y.as_mut_ptr(),
148            ));
149            if ret {
150                Some((velocity_x.assume_init(), velocity_y.assume_init()))
151            } else {
152                None
153            }
154        }
155    }
156
157    /// This signal is emitted when the recognized gesture is finished, velocity
158    /// and direction are a product of previously recorded events.
159    /// ## `velocity_x`
160    /// velocity in the X axis, in pixels/sec
161    /// ## `velocity_y`
162    /// velocity in the Y axis, in pixels/sec
163    #[doc(alias = "swipe")]
164    pub fn connect_swipe<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
165        unsafe extern "C" fn swipe_trampoline<F: Fn(&GestureSwipe, f64, f64) + 'static>(
166            this: *mut ffi::GtkGestureSwipe,
167            velocity_x: std::ffi::c_double,
168            velocity_y: std::ffi::c_double,
169            f: glib::ffi::gpointer,
170        ) {
171            unsafe {
172                let f: &F = &*(f as *const F);
173                f(&from_glib_borrow(this), velocity_x, velocity_y)
174            }
175        }
176        unsafe {
177            let f: Box_<F> = Box_::new(f);
178            connect_raw(
179                self.as_ptr() as *mut _,
180                c"swipe".as_ptr(),
181                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
182                    swipe_trampoline::<F> as *const (),
183                )),
184                Box_::into_raw(f),
185            )
186        }
187    }
188}
189
190impl Default for GestureSwipe {
191    fn default() -> Self {
192        glib::object::Object::new::<Self>()
193    }
194}
195
196// rustdoc-stripper-ignore-next
197/// A [builder-pattern] type to construct [`GestureSwipe`] objects.
198///
199/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
200#[must_use = "The builder must be built to be used"]
201pub struct GestureSwipeBuilder {
202    builder: glib::object::ObjectBuilder<'static, GestureSwipe>,
203}
204
205impl GestureSwipeBuilder {
206    fn new() -> Self {
207        Self {
208            builder: glib::object::Object::builder(),
209        }
210    }
211
212    /// Mouse button number to listen to, or 0 to listen for any button.
213    pub fn button(self, button: u32) -> Self {
214        Self {
215            builder: self.builder.property("button", button),
216        }
217    }
218
219    /// Whether the gesture is exclusive. Exclusive gestures only listen to pointer
220    /// and pointer emulated events.
221    pub fn exclusive(self, exclusive: bool) -> Self {
222        Self {
223            builder: self.builder.property("exclusive", exclusive),
224        }
225    }
226
227    /// Whether the gesture handles only touch events.
228    pub fn touch_only(self, touch_only: bool) -> Self {
229        Self {
230            builder: self.builder.property("touch-only", touch_only),
231        }
232    }
233
234    /// The number of touch points that trigger recognition on this gesture,
235    pub fn n_points(self, n_points: u32) -> Self {
236        Self {
237            builder: self.builder.property("n-points", n_points),
238        }
239    }
240
241    /// If non-[`None`], the gesture will only listen for events that happen on
242    /// this [`gdk::Window`][crate::gdk::Window], or a child of it.
243    pub fn window(self, window: &gdk::Window) -> Self {
244        Self {
245            builder: self.builder.property("window", window.clone()),
246        }
247    }
248
249    /// The propagation phase at which this controller will handle events.
250    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
251        Self {
252            builder: self
253                .builder
254                .property("propagation-phase", propagation_phase),
255        }
256    }
257
258    /// The widget receiving the `GdkEvents` that the controller will handle.
259    pub fn widget(self, widget: &impl IsA<Widget>) -> Self {
260        Self {
261            builder: self.builder.property("widget", widget.clone().upcast()),
262        }
263    }
264
265    // rustdoc-stripper-ignore-next
266    /// Build the [`GestureSwipe`].
267    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
268    pub fn build(self) -> GestureSwipe {
269        assert_initialized_main_thread!();
270        self.builder.build()
271    }
272}