Skip to main content

gtk4/auto/
event_controller_scroll.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, EventControllerScrollFlags, 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    /// Handles scroll events.
16    ///
17    /// It is capable of handling both discrete and continuous scroll
18    /// events from mice or touchpads, abstracting them both with the
19    /// [`scroll`][struct@crate::EventControllerScroll#scroll] signal. Deltas in
20    /// the discrete case are multiples of 1.
21    ///
22    /// In the case of continuous scroll events, [`EventControllerScroll`][crate::EventControllerScroll]
23    /// encloses all [`scroll`][struct@crate::EventControllerScroll#scroll] emissions
24    /// between two [`scroll-begin`][struct@crate::EventControllerScroll#scroll-begin] and
25    /// [`scroll-end`][struct@crate::EventControllerScroll#scroll-end] signals.
26    ///
27    /// The behavior of the event controller can be modified by the flags
28    /// given at creation time, or modified at a later point through
29    /// [`set_flags()`][Self::set_flags()] (e.g. because the scrolling
30    /// conditions of the widget changed).
31    ///
32    /// The controller can be set up to emit motion for either/both vertical
33    /// and horizontal scroll events through [`EventControllerScrollFlags::VERTICAL`][crate::EventControllerScrollFlags::VERTICAL],
34    /// [`EventControllerScrollFlags::HORIZONTAL`][crate::EventControllerScrollFlags::HORIZONTAL] and [`EventControllerScrollFlags::BOTH_AXES`][crate::EventControllerScrollFlags::BOTH_AXES].
35    /// If any axis is disabled, the respective [`scroll`][struct@crate::EventControllerScroll#scroll]
36    /// delta will be 0. Vertical scroll events will be translated to horizontal
37    /// motion for the devices incapable of horizontal scrolling.
38    ///
39    /// The event controller can also be forced to emit discrete events on all
40    /// devices through [`EventControllerScrollFlags::DISCRETE`][crate::EventControllerScrollFlags::DISCRETE]. This can be used
41    /// to implement discrete actions triggered through scroll events (e.g.
42    /// switching across combobox options).
43    ///
44    /// The [`EventControllerScrollFlags::KINETIC`][crate::EventControllerScrollFlags::KINETIC] flag toggles the emission of the
45    /// [`decelerate`][struct@crate::EventControllerScroll#decelerate] signal, emitted at the end
46    /// of scrolling with two X/Y velocity arguments that are consistent with the
47    /// motion that was received.
48    ///
49    /// ## Properties
50    ///
51    ///
52    /// #### `flags`
53    ///  The flags affecting event controller behavior.
54    ///
55    /// Readable | Writeable
56    /// <details><summary><h4>EventController</h4></summary>
57    ///
58    ///
59    /// #### `name`
60    ///  The name for this controller, typically used for debugging purposes.
61    ///
62    /// Readable | Writeable
63    ///
64    ///
65    /// #### `propagation-limit`
66    ///  The limit for which events this controller will handle.
67    ///
68    /// Readable | Writeable
69    ///
70    ///
71    /// #### `propagation-phase`
72    ///  The propagation phase at which this controller will handle events.
73    ///
74    /// Readable | Writeable
75    ///
76    ///
77    /// #### `widget`
78    ///  The widget receiving the `GdkEvents` that the controller will handle.
79    ///
80    /// Readable
81    /// </details>
82    ///
83    /// ## Signals
84    ///
85    ///
86    /// #### `decelerate`
87    ///  Emitted after scroll is finished if the
88    /// [`EventControllerScrollFlags::KINETIC`][crate::EventControllerScrollFlags::KINETIC] flag is set.
89    ///
90    /// @vel_x and @vel_y express the initial velocity that was
91    /// imprinted by the scroll events. @vel_x and @vel_y are expressed in
92    /// pixels/ms.
93    ///
94    ///
95    ///
96    ///
97    /// #### `scroll`
98    ///  Signals that the widget should scroll by the
99    /// amount specified by @dx and @dy.
100    ///
101    /// For the representation unit of the deltas, see
102    /// [`EventControllerScroll::unit()`][crate::EventControllerScroll::unit()].
103    ///
104    ///
105    ///
106    ///
107    /// #### `scroll-begin`
108    ///  Signals that a new scrolling operation has begun.
109    ///
110    /// It will only be emitted on devices capable of it.
111    ///
112    ///
113    ///
114    ///
115    /// #### `scroll-end`
116    ///  Signals that a scrolling operation has finished.
117    ///
118    /// It will only be emitted on devices capable of it.
119    ///
120    ///
121    ///
122    /// # Implements
123    ///
124    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`], [`EventControllerExtManual`][trait@crate::prelude::EventControllerExtManual]
125    #[doc(alias = "GtkEventControllerScroll")]
126    pub struct EventControllerScroll(Object<ffi::GtkEventControllerScroll, ffi::GtkEventControllerScrollClass>) @extends EventController;
127
128    match fn {
129        type_ => || ffi::gtk_event_controller_scroll_get_type(),
130    }
131}
132
133impl EventControllerScroll {
134    /// Creates a new event controller that will handle scroll events.
135    /// ## `flags`
136    /// flags affecting the controller behavior
137    ///
138    /// # Returns
139    ///
140    /// a new [`EventControllerScroll`][crate::EventControllerScroll]
141    #[doc(alias = "gtk_event_controller_scroll_new")]
142    pub fn new(flags: EventControllerScrollFlags) -> EventControllerScroll {
143        assert_initialized_main_thread!();
144        unsafe {
145            EventController::from_glib_full(ffi::gtk_event_controller_scroll_new(flags.into_glib()))
146                .unsafe_cast()
147        }
148    }
149
150    // rustdoc-stripper-ignore-next
151    /// Creates a new builder-pattern struct instance to construct [`EventControllerScroll`] objects.
152    ///
153    /// This method returns an instance of [`EventControllerScrollBuilder`](crate::builders::EventControllerScrollBuilder) which can be used to create [`EventControllerScroll`] objects.
154    pub fn builder() -> EventControllerScrollBuilder {
155        EventControllerScrollBuilder::new()
156    }
157
158    /// Gets the flags conditioning the scroll controller behavior.
159    ///
160    /// # Returns
161    ///
162    /// the controller flags.
163    #[doc(alias = "gtk_event_controller_scroll_get_flags")]
164    #[doc(alias = "get_flags")]
165    pub fn flags(&self) -> EventControllerScrollFlags {
166        unsafe {
167            from_glib(ffi::gtk_event_controller_scroll_get_flags(
168                self.to_glib_none().0,
169            ))
170        }
171    }
172
173    /// Gets the scroll unit of the last
174    /// [`scroll`][struct@crate::EventControllerScroll#scroll] signal received.
175    ///
176    /// Always returns [`gdk::ScrollUnit::Wheel`][crate::gdk::ScrollUnit::Wheel] if the
177    /// [`EventControllerScrollFlags::DISCRETE`][crate::EventControllerScrollFlags::DISCRETE] flag is set.
178    ///
179    /// # Returns
180    ///
181    /// the scroll unit.
182    #[cfg(feature = "v4_8")]
183    #[cfg_attr(docsrs, doc(cfg(feature = "v4_8")))]
184    #[doc(alias = "gtk_event_controller_scroll_get_unit")]
185    #[doc(alias = "get_unit")]
186    pub fn unit(&self) -> gdk::ScrollUnit {
187        unsafe {
188            from_glib(ffi::gtk_event_controller_scroll_get_unit(
189                self.to_glib_none().0,
190            ))
191        }
192    }
193
194    /// Sets the flags conditioning scroll controller behavior.
195    /// ## `flags`
196    /// flags affecting the controller behavior
197    #[doc(alias = "gtk_event_controller_scroll_set_flags")]
198    #[doc(alias = "flags")]
199    pub fn set_flags(&self, flags: EventControllerScrollFlags) {
200        unsafe {
201            ffi::gtk_event_controller_scroll_set_flags(self.to_glib_none().0, flags.into_glib());
202        }
203    }
204
205    /// Emitted after scroll is finished if the
206    /// [`EventControllerScrollFlags::KINETIC`][crate::EventControllerScrollFlags::KINETIC] flag is set.
207    ///
208    /// @vel_x and @vel_y express the initial velocity that was
209    /// imprinted by the scroll events. @vel_x and @vel_y are expressed in
210    /// pixels/ms.
211    /// ## `vel_x`
212    /// X velocity
213    /// ## `vel_y`
214    /// Y velocity
215    #[doc(alias = "decelerate")]
216    pub fn connect_decelerate<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
217        unsafe extern "C" fn decelerate_trampoline<
218            F: Fn(&EventControllerScroll, f64, f64) + 'static,
219        >(
220            this: *mut ffi::GtkEventControllerScroll,
221            vel_x: std::ffi::c_double,
222            vel_y: std::ffi::c_double,
223            f: glib::ffi::gpointer,
224        ) {
225            unsafe {
226                let f: &F = &*(f as *const F);
227                f(&from_glib_borrow(this), vel_x, vel_y)
228            }
229        }
230        unsafe {
231            let f: Box_<F> = Box_::new(f);
232            connect_raw(
233                self.as_ptr() as *mut _,
234                c"decelerate".as_ptr() as *const _,
235                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
236                    decelerate_trampoline::<F> as *const (),
237                )),
238                Box_::into_raw(f),
239            )
240        }
241    }
242
243    /// Signals that the widget should scroll by the
244    /// amount specified by @dx and @dy.
245    ///
246    /// For the representation unit of the deltas, see
247    /// [`unit()`][Self::unit()].
248    /// ## `dx`
249    /// X delta
250    /// ## `dy`
251    /// Y delta
252    ///
253    /// # Returns
254    ///
255    /// [`true`] if the scroll event was handled,
256    ///   [`false`] otherwise.
257    #[doc(alias = "scroll")]
258    pub fn connect_scroll<F: Fn(&Self, f64, f64) -> glib::Propagation + 'static>(
259        &self,
260        f: F,
261    ) -> SignalHandlerId {
262        unsafe extern "C" fn scroll_trampoline<
263            F: Fn(&EventControllerScroll, f64, f64) -> glib::Propagation + 'static,
264        >(
265            this: *mut ffi::GtkEventControllerScroll,
266            dx: std::ffi::c_double,
267            dy: std::ffi::c_double,
268            f: glib::ffi::gpointer,
269        ) -> glib::ffi::gboolean {
270            unsafe {
271                let f: &F = &*(f as *const F);
272                f(&from_glib_borrow(this), dx, dy).into_glib()
273            }
274        }
275        unsafe {
276            let f: Box_<F> = Box_::new(f);
277            connect_raw(
278                self.as_ptr() as *mut _,
279                c"scroll".as_ptr() as *const _,
280                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
281                    scroll_trampoline::<F> as *const (),
282                )),
283                Box_::into_raw(f),
284            )
285        }
286    }
287
288    /// Signals that a new scrolling operation has begun.
289    ///
290    /// It will only be emitted on devices capable of it.
291    #[doc(alias = "scroll-begin")]
292    pub fn connect_scroll_begin<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
293        unsafe extern "C" fn scroll_begin_trampoline<F: Fn(&EventControllerScroll) + 'static>(
294            this: *mut ffi::GtkEventControllerScroll,
295            f: glib::ffi::gpointer,
296        ) {
297            unsafe {
298                let f: &F = &*(f as *const F);
299                f(&from_glib_borrow(this))
300            }
301        }
302        unsafe {
303            let f: Box_<F> = Box_::new(f);
304            connect_raw(
305                self.as_ptr() as *mut _,
306                c"scroll-begin".as_ptr() as *const _,
307                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
308                    scroll_begin_trampoline::<F> as *const (),
309                )),
310                Box_::into_raw(f),
311            )
312        }
313    }
314
315    /// Signals that a scrolling operation has finished.
316    ///
317    /// It will only be emitted on devices capable of it.
318    #[doc(alias = "scroll-end")]
319    pub fn connect_scroll_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
320        unsafe extern "C" fn scroll_end_trampoline<F: Fn(&EventControllerScroll) + 'static>(
321            this: *mut ffi::GtkEventControllerScroll,
322            f: glib::ffi::gpointer,
323        ) {
324            unsafe {
325                let f: &F = &*(f as *const F);
326                f(&from_glib_borrow(this))
327            }
328        }
329        unsafe {
330            let f: Box_<F> = Box_::new(f);
331            connect_raw(
332                self.as_ptr() as *mut _,
333                c"scroll-end".as_ptr() as *const _,
334                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
335                    scroll_end_trampoline::<F> as *const (),
336                )),
337                Box_::into_raw(f),
338            )
339        }
340    }
341
342    #[doc(alias = "flags")]
343    pub fn connect_flags_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
344        unsafe extern "C" fn notify_flags_trampoline<F: Fn(&EventControllerScroll) + 'static>(
345            this: *mut ffi::GtkEventControllerScroll,
346            _param_spec: glib::ffi::gpointer,
347            f: glib::ffi::gpointer,
348        ) {
349            unsafe {
350                let f: &F = &*(f as *const F);
351                f(&from_glib_borrow(this))
352            }
353        }
354        unsafe {
355            let f: Box_<F> = Box_::new(f);
356            connect_raw(
357                self.as_ptr() as *mut _,
358                c"notify::flags".as_ptr() as *const _,
359                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
360                    notify_flags_trampoline::<F> as *const (),
361                )),
362                Box_::into_raw(f),
363            )
364        }
365    }
366}
367
368impl Default for EventControllerScroll {
369    fn default() -> Self {
370        glib::object::Object::new::<Self>()
371    }
372}
373
374// rustdoc-stripper-ignore-next
375/// A [builder-pattern] type to construct [`EventControllerScroll`] objects.
376///
377/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
378#[must_use = "The builder must be built to be used"]
379pub struct EventControllerScrollBuilder {
380    builder: glib::object::ObjectBuilder<'static, EventControllerScroll>,
381}
382
383impl EventControllerScrollBuilder {
384    fn new() -> Self {
385        Self {
386            builder: glib::object::Object::builder(),
387        }
388    }
389
390    /// The flags affecting event controller behavior.
391    pub fn flags(self, flags: EventControllerScrollFlags) -> Self {
392        Self {
393            builder: self.builder.property("flags", flags),
394        }
395    }
396
397    /// The name for this controller, typically used for debugging purposes.
398    pub fn name(self, name: impl Into<glib::GString>) -> Self {
399        Self {
400            builder: self.builder.property("name", name.into()),
401        }
402    }
403
404    /// The limit for which events this controller will handle.
405    pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
406        Self {
407            builder: self
408                .builder
409                .property("propagation-limit", propagation_limit),
410        }
411    }
412
413    /// The propagation phase at which this controller will handle events.
414    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
415        Self {
416            builder: self
417                .builder
418                .property("propagation-phase", propagation_phase),
419        }
420    }
421
422    // rustdoc-stripper-ignore-next
423    /// Build the [`EventControllerScroll`].
424    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
425    pub fn build(self) -> EventControllerScroll {
426        assert_initialized_main_thread!();
427        self.builder.build()
428    }
429}