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::{ffi, EventController, Gesture, GestureSingle, PropagationLimit, PropagationPhase};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// [`GestureSwipe`][crate::GestureSwipe] is a [`Gesture`][crate::Gesture] for 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`]
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 let f: &F = &*(f as *const F);
181 f(&from_glib_borrow(this), velocity_x, velocity_y)
182 }
183 unsafe {
184 let f: Box_<F> = Box_::new(f);
185 connect_raw(
186 self.as_ptr() as *mut _,
187 b"swipe\0".as_ptr() as *const _,
188 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
189 swipe_trampoline::<F> as *const (),
190 )),
191 Box_::into_raw(f),
192 )
193 }
194 }
195}
196
197impl Default for GestureSwipe {
198 fn default() -> Self {
199 Self::new()
200 }
201}
202
203// rustdoc-stripper-ignore-next
204/// A [builder-pattern] type to construct [`GestureSwipe`] objects.
205///
206/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
207#[must_use = "The builder must be built to be used"]
208pub struct GestureSwipeBuilder {
209 builder: glib::object::ObjectBuilder<'static, GestureSwipe>,
210}
211
212impl GestureSwipeBuilder {
213 fn new() -> Self {
214 Self {
215 builder: glib::object::Object::builder(),
216 }
217 }
218
219 /// Mouse button number to listen to, or 0 to listen for any button.
220 pub fn button(self, button: u32) -> Self {
221 Self {
222 builder: self.builder.property("button", button),
223 }
224 }
225
226 /// Whether the gesture is exclusive.
227 ///
228 /// Exclusive gestures only listen to pointer and pointer emulated events.
229 pub fn exclusive(self, exclusive: bool) -> Self {
230 Self {
231 builder: self.builder.property("exclusive", exclusive),
232 }
233 }
234
235 /// Whether the gesture handles only touch events.
236 pub fn touch_only(self, touch_only: bool) -> Self {
237 Self {
238 builder: self.builder.property("touch-only", touch_only),
239 }
240 }
241
242 /// The number of touch points that trigger
243 /// recognition on this gesture.
244 pub fn n_points(self, n_points: u32) -> Self {
245 Self {
246 builder: self.builder.property("n-points", n_points),
247 }
248 }
249
250 /// The name for this controller, typically used for debugging purposes.
251 pub fn name(self, name: impl Into<glib::GString>) -> Self {
252 Self {
253 builder: self.builder.property("name", name.into()),
254 }
255 }
256
257 /// The limit for which events this controller will handle.
258 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
259 Self {
260 builder: self
261 .builder
262 .property("propagation-limit", propagation_limit),
263 }
264 }
265
266 /// The propagation phase at which this controller will handle events.
267 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
268 Self {
269 builder: self
270 .builder
271 .property("propagation-phase", propagation_phase),
272 }
273 }
274
275 // rustdoc-stripper-ignore-next
276 /// Build the [`GestureSwipe`].
277 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
278 pub fn build(self) -> GestureSwipe {
279 assert_initialized_main_thread!();
280 self.builder.build()
281 }
282}