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