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::{ffi, EventController, EventControllerScrollFlags, 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 /// 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`]
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 let f: &F = &*(f as *const F);
226 f(&from_glib_borrow(this), vel_x, vel_y)
227 }
228 unsafe {
229 let f: Box_<F> = Box_::new(f);
230 connect_raw(
231 self.as_ptr() as *mut _,
232 c"decelerate".as_ptr() as *const _,
233 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
234 decelerate_trampoline::<F> as *const (),
235 )),
236 Box_::into_raw(f),
237 )
238 }
239 }
240
241 /// Signals that the widget should scroll by the
242 /// amount specified by @dx and @dy.
243 ///
244 /// For the representation unit of the deltas, see
245 /// [`unit()`][Self::unit()].
246 /// ## `dx`
247 /// X delta
248 /// ## `dy`
249 /// Y delta
250 ///
251 /// # Returns
252 ///
253 /// [`true`] if the scroll event was handled,
254 /// [`false`] otherwise.
255 #[doc(alias = "scroll")]
256 pub fn connect_scroll<F: Fn(&Self, f64, f64) -> glib::Propagation + 'static>(
257 &self,
258 f: F,
259 ) -> SignalHandlerId {
260 unsafe extern "C" fn scroll_trampoline<
261 F: Fn(&EventControllerScroll, f64, f64) -> glib::Propagation + 'static,
262 >(
263 this: *mut ffi::GtkEventControllerScroll,
264 dx: std::ffi::c_double,
265 dy: std::ffi::c_double,
266 f: glib::ffi::gpointer,
267 ) -> glib::ffi::gboolean {
268 let f: &F = &*(f as *const F);
269 f(&from_glib_borrow(this), dx, dy).into_glib()
270 }
271 unsafe {
272 let f: Box_<F> = Box_::new(f);
273 connect_raw(
274 self.as_ptr() as *mut _,
275 c"scroll".as_ptr() as *const _,
276 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
277 scroll_trampoline::<F> as *const (),
278 )),
279 Box_::into_raw(f),
280 )
281 }
282 }
283
284 /// Signals that a new scrolling operation has begun.
285 ///
286 /// It will only be emitted on devices capable of it.
287 #[doc(alias = "scroll-begin")]
288 pub fn connect_scroll_begin<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
289 unsafe extern "C" fn scroll_begin_trampoline<F: Fn(&EventControllerScroll) + 'static>(
290 this: *mut ffi::GtkEventControllerScroll,
291 f: glib::ffi::gpointer,
292 ) {
293 let f: &F = &*(f as *const F);
294 f(&from_glib_borrow(this))
295 }
296 unsafe {
297 let f: Box_<F> = Box_::new(f);
298 connect_raw(
299 self.as_ptr() as *mut _,
300 c"scroll-begin".as_ptr() as *const _,
301 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
302 scroll_begin_trampoline::<F> as *const (),
303 )),
304 Box_::into_raw(f),
305 )
306 }
307 }
308
309 /// Signals that a scrolling operation has finished.
310 ///
311 /// It will only be emitted on devices capable of it.
312 #[doc(alias = "scroll-end")]
313 pub fn connect_scroll_end<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
314 unsafe extern "C" fn scroll_end_trampoline<F: Fn(&EventControllerScroll) + 'static>(
315 this: *mut ffi::GtkEventControllerScroll,
316 f: glib::ffi::gpointer,
317 ) {
318 let f: &F = &*(f as *const F);
319 f(&from_glib_borrow(this))
320 }
321 unsafe {
322 let f: Box_<F> = Box_::new(f);
323 connect_raw(
324 self.as_ptr() as *mut _,
325 c"scroll-end".as_ptr() as *const _,
326 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
327 scroll_end_trampoline::<F> as *const (),
328 )),
329 Box_::into_raw(f),
330 )
331 }
332 }
333
334 #[doc(alias = "flags")]
335 pub fn connect_flags_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
336 unsafe extern "C" fn notify_flags_trampoline<F: Fn(&EventControllerScroll) + 'static>(
337 this: *mut ffi::GtkEventControllerScroll,
338 _param_spec: glib::ffi::gpointer,
339 f: glib::ffi::gpointer,
340 ) {
341 let f: &F = &*(f as *const F);
342 f(&from_glib_borrow(this))
343 }
344 unsafe {
345 let f: Box_<F> = Box_::new(f);
346 connect_raw(
347 self.as_ptr() as *mut _,
348 c"notify::flags".as_ptr() as *const _,
349 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
350 notify_flags_trampoline::<F> as *const (),
351 )),
352 Box_::into_raw(f),
353 )
354 }
355 }
356}
357
358impl Default for EventControllerScroll {
359 fn default() -> Self {
360 glib::object::Object::new::<Self>()
361 }
362}
363
364// rustdoc-stripper-ignore-next
365/// A [builder-pattern] type to construct [`EventControllerScroll`] objects.
366///
367/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
368#[must_use = "The builder must be built to be used"]
369pub struct EventControllerScrollBuilder {
370 builder: glib::object::ObjectBuilder<'static, EventControllerScroll>,
371}
372
373impl EventControllerScrollBuilder {
374 fn new() -> Self {
375 Self {
376 builder: glib::object::Object::builder(),
377 }
378 }
379
380 /// The flags affecting event controller behavior.
381 pub fn flags(self, flags: EventControllerScrollFlags) -> Self {
382 Self {
383 builder: self.builder.property("flags", flags),
384 }
385 }
386
387 /// The name for this controller, typically used for debugging purposes.
388 pub fn name(self, name: impl Into<glib::GString>) -> Self {
389 Self {
390 builder: self.builder.property("name", name.into()),
391 }
392 }
393
394 /// The limit for which events this controller will handle.
395 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
396 Self {
397 builder: self
398 .builder
399 .property("propagation-limit", propagation_limit),
400 }
401 }
402
403 /// The propagation phase at which this controller will handle events.
404 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
405 Self {
406 builder: self
407 .builder
408 .property("propagation-phase", propagation_phase),
409 }
410 }
411
412 // rustdoc-stripper-ignore-next
413 /// Build the [`EventControllerScroll`].
414 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
415 pub fn build(self) -> EventControllerScroll {
416 assert_initialized_main_thread!();
417 self.builder.build()
418 }
419}