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