Skip to main content

gtk4/auto/
event_controller_focus.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, 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    /// Tracks keyboard focus.
16    ///
17    /// The event controller offers [`enter`][struct@crate::EventControllerFocus#enter]
18    /// and [`leave`][struct@crate::EventControllerFocus#leave] signals, as well as
19    /// [`is-focus`][struct@crate::EventControllerFocus#is-focus] and
20    /// [`contains-focus`][struct@crate::EventControllerFocus#contains-focus] properties
21    /// which are updated to reflect focus changes inside the widget hierarchy
22    /// that is rooted at the controllers widget.
23    ///
24    /// ## Properties
25    ///
26    ///
27    /// #### `contains-focus`
28    ///  [`true`] if focus is contained in the controllers widget.
29    ///
30    /// See [`is-focus`][struct@crate::EventControllerFocus#is-focus] for whether
31    /// the focus is in the widget itself or inside a descendent.
32    ///
33    /// When handling focus events, this property is updated
34    /// before [`enter`][struct@crate::EventControllerFocus#enter] or
35    /// [`leave`][struct@crate::EventControllerFocus#leave] are emitted.
36    ///
37    /// Readable
38    ///
39    ///
40    /// #### `is-focus`
41    ///  [`true`] if focus is in the controllers widget itself,
42    /// as opposed to in a descendent widget.
43    ///
44    /// See also [`contains-focus`][struct@crate::EventControllerFocus#contains-focus].
45    ///
46    /// When handling focus events, this property is updated
47    /// before [`enter`][struct@crate::EventControllerFocus#enter] or
48    /// [`leave`][struct@crate::EventControllerFocus#leave] are emitted.
49    ///
50    /// Readable
51    /// <details><summary><h4>EventController</h4></summary>
52    ///
53    ///
54    /// #### `name`
55    ///  The name for this controller, typically used for debugging purposes.
56    ///
57    /// Readable | Writeable
58    ///
59    ///
60    /// #### `propagation-limit`
61    ///  The limit for which events this controller will handle.
62    ///
63    /// Readable | Writeable
64    ///
65    ///
66    /// #### `propagation-phase`
67    ///  The propagation phase at which this controller will handle events.
68    ///
69    /// Readable | Writeable
70    ///
71    ///
72    /// #### `widget`
73    ///  The widget receiving the `GdkEvents` that the controller will handle.
74    ///
75    /// Readable
76    /// </details>
77    ///
78    /// ## Signals
79    ///
80    ///
81    /// #### `enter`
82    ///  Emitted whenever the focus enters into the widget or one
83    /// of its descendents.
84    ///
85    /// Note that this means you may not get an ::enter signal
86    /// even though the widget becomes the focus location, in
87    /// certain cases (such as when the focus moves from a descendent
88    /// of the widget to the widget itself). If you are interested
89    /// in these cases, you can monitor the
90    /// [`is-focus`][struct@crate::EventControllerFocus#is-focus]
91    /// property for changes.
92    ///
93    ///
94    ///
95    ///
96    /// #### `leave`
97    ///  Emitted whenever the focus leaves the widget hierarchy
98    /// that is rooted at the widget that the controller is attached to.
99    ///
100    /// Note that this means you may not get a ::leave signal
101    /// even though the focus moves away from the widget, in
102    /// certain cases (such as when the focus moves from the widget
103    /// to a descendent). If you are interested in these cases, you
104    /// can monitor the [`is-focus`][struct@crate::EventControllerFocus#is-focus]
105    /// property for changes.
106    ///
107    ///
108    ///
109    /// # Implements
110    ///
111    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`], [`EventControllerExtManual`][trait@crate::prelude::EventControllerExtManual]
112    #[doc(alias = "GtkEventControllerFocus")]
113    pub struct EventControllerFocus(Object<ffi::GtkEventControllerFocus, ffi::GtkEventControllerFocusClass>) @extends EventController;
114
115    match fn {
116        type_ => || ffi::gtk_event_controller_focus_get_type(),
117    }
118}
119
120impl EventControllerFocus {
121    /// Creates a new event controller that will handle focus events.
122    ///
123    /// # Returns
124    ///
125    /// a new [`EventControllerFocus`][crate::EventControllerFocus]
126    #[doc(alias = "gtk_event_controller_focus_new")]
127    pub fn new() -> EventControllerFocus {
128        assert_initialized_main_thread!();
129        unsafe {
130            EventController::from_glib_full(ffi::gtk_event_controller_focus_new()).unsafe_cast()
131        }
132    }
133
134    // rustdoc-stripper-ignore-next
135    /// Creates a new builder-pattern struct instance to construct [`EventControllerFocus`] objects.
136    ///
137    /// This method returns an instance of [`EventControllerFocusBuilder`](crate::builders::EventControllerFocusBuilder) which can be used to create [`EventControllerFocus`] objects.
138    pub fn builder() -> EventControllerFocusBuilder {
139        EventControllerFocusBuilder::new()
140    }
141
142    /// Returns [`true`] if focus is within @self or one of its children.
143    ///
144    /// # Returns
145    ///
146    /// [`true`] if focus is within @self or one of its children
147    #[doc(alias = "gtk_event_controller_focus_contains_focus")]
148    #[doc(alias = "contains-focus")]
149    pub fn contains_focus(&self) -> bool {
150        unsafe {
151            from_glib(ffi::gtk_event_controller_focus_contains_focus(
152                self.to_glib_none().0,
153            ))
154        }
155    }
156
157    /// Returns [`true`] if focus is within @self, but not one of its children.
158    ///
159    /// # Returns
160    ///
161    /// [`true`] if focus is within @self, but not one of its children
162    #[doc(alias = "gtk_event_controller_focus_is_focus")]
163    #[doc(alias = "is-focus")]
164    pub fn is_focus(&self) -> bool {
165        unsafe {
166            from_glib(ffi::gtk_event_controller_focus_is_focus(
167                self.to_glib_none().0,
168            ))
169        }
170    }
171
172    /// Emitted whenever the focus enters into the widget or one
173    /// of its descendents.
174    ///
175    /// Note that this means you may not get an ::enter signal
176    /// even though the widget becomes the focus location, in
177    /// certain cases (such as when the focus moves from a descendent
178    /// of the widget to the widget itself). If you are interested
179    /// in these cases, you can monitor the
180    /// [`is-focus`][struct@crate::EventControllerFocus#is-focus]
181    /// property for changes.
182    #[doc(alias = "enter")]
183    pub fn connect_enter<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
184        unsafe extern "C" fn enter_trampoline<F: Fn(&EventControllerFocus) + 'static>(
185            this: *mut ffi::GtkEventControllerFocus,
186            f: glib::ffi::gpointer,
187        ) {
188            unsafe {
189                let f: &F = &*(f as *const F);
190                f(&from_glib_borrow(this))
191            }
192        }
193        unsafe {
194            let f: Box_<F> = Box_::new(f);
195            connect_raw(
196                self.as_ptr() as *mut _,
197                c"enter".as_ptr() as *const _,
198                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
199                    enter_trampoline::<F> as *const (),
200                )),
201                Box_::into_raw(f),
202            )
203        }
204    }
205
206    /// Emitted whenever the focus leaves the widget hierarchy
207    /// that is rooted at the widget that the controller is attached to.
208    ///
209    /// Note that this means you may not get a ::leave signal
210    /// even though the focus moves away from the widget, in
211    /// certain cases (such as when the focus moves from the widget
212    /// to a descendent). If you are interested in these cases, you
213    /// can monitor the [`is-focus`][struct@crate::EventControllerFocus#is-focus]
214    /// property for changes.
215    #[doc(alias = "leave")]
216    pub fn connect_leave<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
217        unsafe extern "C" fn leave_trampoline<F: Fn(&EventControllerFocus) + 'static>(
218            this: *mut ffi::GtkEventControllerFocus,
219            f: glib::ffi::gpointer,
220        ) {
221            unsafe {
222                let f: &F = &*(f as *const F);
223                f(&from_glib_borrow(this))
224            }
225        }
226        unsafe {
227            let f: Box_<F> = Box_::new(f);
228            connect_raw(
229                self.as_ptr() as *mut _,
230                c"leave".as_ptr() as *const _,
231                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
232                    leave_trampoline::<F> as *const (),
233                )),
234                Box_::into_raw(f),
235            )
236        }
237    }
238
239    #[doc(alias = "contains-focus")]
240    pub fn connect_contains_focus_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
241        unsafe extern "C" fn notify_contains_focus_trampoline<
242            F: Fn(&EventControllerFocus) + 'static,
243        >(
244            this: *mut ffi::GtkEventControllerFocus,
245            _param_spec: glib::ffi::gpointer,
246            f: glib::ffi::gpointer,
247        ) {
248            unsafe {
249                let f: &F = &*(f as *const F);
250                f(&from_glib_borrow(this))
251            }
252        }
253        unsafe {
254            let f: Box_<F> = Box_::new(f);
255            connect_raw(
256                self.as_ptr() as *mut _,
257                c"notify::contains-focus".as_ptr() as *const _,
258                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
259                    notify_contains_focus_trampoline::<F> as *const (),
260                )),
261                Box_::into_raw(f),
262            )
263        }
264    }
265
266    #[doc(alias = "is-focus")]
267    pub fn connect_is_focus_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
268        unsafe extern "C" fn notify_is_focus_trampoline<F: Fn(&EventControllerFocus) + 'static>(
269            this: *mut ffi::GtkEventControllerFocus,
270            _param_spec: glib::ffi::gpointer,
271            f: glib::ffi::gpointer,
272        ) {
273            unsafe {
274                let f: &F = &*(f as *const F);
275                f(&from_glib_borrow(this))
276            }
277        }
278        unsafe {
279            let f: Box_<F> = Box_::new(f);
280            connect_raw(
281                self.as_ptr() as *mut _,
282                c"notify::is-focus".as_ptr() as *const _,
283                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
284                    notify_is_focus_trampoline::<F> as *const (),
285                )),
286                Box_::into_raw(f),
287            )
288        }
289    }
290}
291
292impl Default for EventControllerFocus {
293    fn default() -> Self {
294        Self::new()
295    }
296}
297
298// rustdoc-stripper-ignore-next
299/// A [builder-pattern] type to construct [`EventControllerFocus`] objects.
300///
301/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
302#[must_use = "The builder must be built to be used"]
303pub struct EventControllerFocusBuilder {
304    builder: glib::object::ObjectBuilder<'static, EventControllerFocus>,
305}
306
307impl EventControllerFocusBuilder {
308    fn new() -> Self {
309        Self {
310            builder: glib::object::Object::builder(),
311        }
312    }
313
314    /// The name for this controller, typically used for debugging purposes.
315    pub fn name(self, name: impl Into<glib::GString>) -> Self {
316        Self {
317            builder: self.builder.property("name", name.into()),
318        }
319    }
320
321    /// The limit for which events this controller will handle.
322    pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
323        Self {
324            builder: self
325                .builder
326                .property("propagation-limit", propagation_limit),
327        }
328    }
329
330    /// The propagation phase at which this controller will handle events.
331    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
332        Self {
333            builder: self
334                .builder
335                .property("propagation-phase", propagation_phase),
336        }
337    }
338
339    // rustdoc-stripper-ignore-next
340    /// Build the [`EventControllerFocus`].
341    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
342    pub fn build(self) -> EventControllerFocus {
343        assert_initialized_main_thread!();
344        self.builder.build()
345    }
346}