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::{ffi, EventController, 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    /// 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`]
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            let f: &F = &*(f as *const F);
189            f(&from_glib_borrow(this))
190        }
191        unsafe {
192            let f: Box_<F> = Box_::new(f);
193            connect_raw(
194                self.as_ptr() as *mut _,
195                c"enter".as_ptr() as *const _,
196                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
197                    enter_trampoline::<F> as *const (),
198                )),
199                Box_::into_raw(f),
200            )
201        }
202    }
203
204    /// Emitted whenever the focus leaves the widget hierarchy
205    /// that is rooted at the widget that the controller is attached to.
206    ///
207    /// Note that this means you may not get a ::leave signal
208    /// even though the focus moves away from the widget, in
209    /// certain cases (such as when the focus moves from the widget
210    /// to a descendent). If you are interested in these cases, you
211    /// can monitor the [`is-focus`][struct@crate::EventControllerFocus#is-focus]
212    /// property for changes.
213    #[doc(alias = "leave")]
214    pub fn connect_leave<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
215        unsafe extern "C" fn leave_trampoline<F: Fn(&EventControllerFocus) + 'static>(
216            this: *mut ffi::GtkEventControllerFocus,
217            f: glib::ffi::gpointer,
218        ) {
219            let f: &F = &*(f as *const F);
220            f(&from_glib_borrow(this))
221        }
222        unsafe {
223            let f: Box_<F> = Box_::new(f);
224            connect_raw(
225                self.as_ptr() as *mut _,
226                c"leave".as_ptr() as *const _,
227                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
228                    leave_trampoline::<F> as *const (),
229                )),
230                Box_::into_raw(f),
231            )
232        }
233    }
234
235    #[doc(alias = "contains-focus")]
236    pub fn connect_contains_focus_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
237        unsafe extern "C" fn notify_contains_focus_trampoline<
238            F: Fn(&EventControllerFocus) + 'static,
239        >(
240            this: *mut ffi::GtkEventControllerFocus,
241            _param_spec: glib::ffi::gpointer,
242            f: glib::ffi::gpointer,
243        ) {
244            let f: &F = &*(f as *const F);
245            f(&from_glib_borrow(this))
246        }
247        unsafe {
248            let f: Box_<F> = Box_::new(f);
249            connect_raw(
250                self.as_ptr() as *mut _,
251                c"notify::contains-focus".as_ptr() as *const _,
252                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
253                    notify_contains_focus_trampoline::<F> as *const (),
254                )),
255                Box_::into_raw(f),
256            )
257        }
258    }
259
260    #[doc(alias = "is-focus")]
261    pub fn connect_is_focus_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
262        unsafe extern "C" fn notify_is_focus_trampoline<F: Fn(&EventControllerFocus) + 'static>(
263            this: *mut ffi::GtkEventControllerFocus,
264            _param_spec: glib::ffi::gpointer,
265            f: glib::ffi::gpointer,
266        ) {
267            let f: &F = &*(f as *const F);
268            f(&from_glib_borrow(this))
269        }
270        unsafe {
271            let f: Box_<F> = Box_::new(f);
272            connect_raw(
273                self.as_ptr() as *mut _,
274                c"notify::is-focus".as_ptr() as *const _,
275                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
276                    notify_is_focus_trampoline::<F> as *const (),
277                )),
278                Box_::into_raw(f),
279            )
280        }
281    }
282}
283
284impl Default for EventControllerFocus {
285    fn default() -> Self {
286        Self::new()
287    }
288}
289
290// rustdoc-stripper-ignore-next
291/// A [builder-pattern] type to construct [`EventControllerFocus`] objects.
292///
293/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
294#[must_use = "The builder must be built to be used"]
295pub struct EventControllerFocusBuilder {
296    builder: glib::object::ObjectBuilder<'static, EventControllerFocus>,
297}
298
299impl EventControllerFocusBuilder {
300    fn new() -> Self {
301        Self {
302            builder: glib::object::Object::builder(),
303        }
304    }
305
306    /// The name for this controller, typically used for debugging purposes.
307    pub fn name(self, name: impl Into<glib::GString>) -> Self {
308        Self {
309            builder: self.builder.property("name", name.into()),
310        }
311    }
312
313    /// The limit for which events this controller will handle.
314    pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
315        Self {
316            builder: self
317                .builder
318                .property("propagation-limit", propagation_limit),
319        }
320    }
321
322    /// The propagation phase at which this controller will handle events.
323    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
324        Self {
325            builder: self
326                .builder
327                .property("propagation-phase", propagation_phase),
328        }
329    }
330
331    // rustdoc-stripper-ignore-next
332    /// Build the [`EventControllerFocus`].
333    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
334    pub fn build(self) -> EventControllerFocus {
335        assert_initialized_main_thread!();
336        self.builder.build()
337    }
338}