Skip to main content

gtk4/auto/
event_controller_key.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, IMContext, PropagationLimit, PropagationPhase, Widget, 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    /// Provides access to key events.
16    ///
17    /// ## Signals
18    ///
19    ///
20    /// #### `im-update`
21    ///  Emitted whenever the input method context filters away
22    /// a keypress and prevents the @controller receiving it.
23    ///
24    /// See [`EventControllerKey::set_im_context()`][crate::EventControllerKey::set_im_context()] and
25    /// [`IMContextExt::filter_keypress()`][crate::prelude::IMContextExt::filter_keypress()].
26    ///
27    ///
28    ///
29    ///
30    /// #### `key-pressed`
31    ///  Emitted whenever a key is pressed.
32    ///
33    ///
34    ///
35    ///
36    /// #### `key-released`
37    ///  Emitted whenever a key is released.
38    ///
39    ///
40    ///
41    ///
42    /// #### `modifiers`
43    ///  Emitted whenever the state of modifier keys and pointer buttons change.
44    ///
45    ///
46    ///
47    /// # Implements
48    ///
49    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`], [`EventControllerExtManual`][trait@crate::prelude::EventControllerExtManual]
50    #[doc(alias = "GtkEventControllerKey")]
51    pub struct EventControllerKey(Object<ffi::GtkEventControllerKey, ffi::GtkEventControllerKeyClass>) @extends EventController;
52
53    match fn {
54        type_ => || ffi::gtk_event_controller_key_get_type(),
55    }
56}
57
58impl EventControllerKey {
59    /// Creates a new event controller that will handle key events.
60    ///
61    /// # Returns
62    ///
63    /// a new [`EventControllerKey`][crate::EventControllerKey]
64    #[doc(alias = "gtk_event_controller_key_new")]
65    pub fn new() -> EventControllerKey {
66        assert_initialized_main_thread!();
67        unsafe {
68            EventController::from_glib_full(ffi::gtk_event_controller_key_new()).unsafe_cast()
69        }
70    }
71
72    // rustdoc-stripper-ignore-next
73    /// Creates a new builder-pattern struct instance to construct [`EventControllerKey`] objects.
74    ///
75    /// This method returns an instance of [`EventControllerKeyBuilder`](crate::builders::EventControllerKeyBuilder) which can be used to create [`EventControllerKey`] objects.
76    pub fn builder() -> EventControllerKeyBuilder {
77        EventControllerKeyBuilder::new()
78    }
79
80    /// Forwards the current event of this @self to a @widget.
81    ///
82    /// This function can only be used in handlers for the
83    /// [`key-pressed`][struct@crate::EventControllerKey#key-pressed],
84    /// [`key-released`][struct@crate::EventControllerKey#key-released]
85    /// or [`modifiers`][struct@crate::EventControllerKey#modifiers] signals.
86    /// ## `widget`
87    /// a [`Widget`][crate::Widget]
88    ///
89    /// # Returns
90    ///
91    /// whether the @widget handled the event
92    #[doc(alias = "gtk_event_controller_key_forward")]
93    pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
94        unsafe {
95            from_glib(ffi::gtk_event_controller_key_forward(
96                self.to_glib_none().0,
97                widget.as_ref().to_glib_none().0,
98            ))
99        }
100    }
101
102    /// Gets the key group of the current event of this @self.
103    ///
104    /// See `Gdk::KeyEvent::get_layout()`.
105    ///
106    /// # Returns
107    ///
108    /// the key group
109    #[doc(alias = "gtk_event_controller_key_get_group")]
110    #[doc(alias = "get_group")]
111    pub fn group(&self) -> u32 {
112        unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
113    }
114
115    /// Gets the input method context of the key @self.
116    ///
117    /// # Returns
118    ///
119    /// the [`IMContext`][crate::IMContext]
120    #[doc(alias = "gtk_event_controller_key_get_im_context")]
121    #[doc(alias = "get_im_context")]
122    pub fn im_context(&self) -> Option<IMContext> {
123        unsafe {
124            from_glib_none(ffi::gtk_event_controller_key_get_im_context(
125                self.to_glib_none().0,
126            ))
127        }
128    }
129
130    /// Sets the input method context of the key @self.
131    /// ## `im_context`
132    /// a [`IMContext`][crate::IMContext]
133    #[doc(alias = "gtk_event_controller_key_set_im_context")]
134    pub fn set_im_context(&self, im_context: Option<&impl IsA<IMContext>>) {
135        unsafe {
136            ffi::gtk_event_controller_key_set_im_context(
137                self.to_glib_none().0,
138                im_context.map(|p| p.as_ref()).to_glib_none().0,
139            );
140        }
141    }
142
143    /// Emitted whenever the input method context filters away
144    /// a keypress and prevents the @controller receiving it.
145    ///
146    /// See [`set_im_context()`][Self::set_im_context()] and
147    /// [`IMContextExt::filter_keypress()`][crate::prelude::IMContextExt::filter_keypress()].
148    #[doc(alias = "im-update")]
149    pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
150        unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
151            this: *mut ffi::GtkEventControllerKey,
152            f: glib::ffi::gpointer,
153        ) {
154            unsafe {
155                let f: &F = &*(f as *const F);
156                f(&from_glib_borrow(this))
157            }
158        }
159        unsafe {
160            let f: Box_<F> = Box_::new(f);
161            connect_raw(
162                self.as_ptr() as *mut _,
163                c"im-update".as_ptr() as *const _,
164                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
165                    im_update_trampoline::<F> as *const (),
166                )),
167                Box_::into_raw(f),
168            )
169        }
170    }
171
172    /// Emitted whenever the state of modifier keys and pointer buttons change.
173    /// ## `state`
174    /// the bitmask, representing the new state of modifier keys and
175    ///   pointer buttons.
176    ///
177    /// # Returns
178    ///
179    /// whether to ignore modifiers
180    #[doc(alias = "modifiers")]
181    pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> glib::Propagation + 'static>(
182        &self,
183        f: F,
184    ) -> SignalHandlerId {
185        unsafe extern "C" fn modifiers_trampoline<
186            F: Fn(&EventControllerKey, gdk::ModifierType) -> glib::Propagation + 'static,
187        >(
188            this: *mut ffi::GtkEventControllerKey,
189            state: gdk::ffi::GdkModifierType,
190            f: glib::ffi::gpointer,
191        ) -> glib::ffi::gboolean {
192            unsafe {
193                let f: &F = &*(f as *const F);
194                f(&from_glib_borrow(this), from_glib(state)).into_glib()
195            }
196        }
197        unsafe {
198            let f: Box_<F> = Box_::new(f);
199            connect_raw(
200                self.as_ptr() as *mut _,
201                c"modifiers".as_ptr() as *const _,
202                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
203                    modifiers_trampoline::<F> as *const (),
204                )),
205                Box_::into_raw(f),
206            )
207        }
208    }
209}
210
211impl Default for EventControllerKey {
212    fn default() -> Self {
213        Self::new()
214    }
215}
216
217// rustdoc-stripper-ignore-next
218/// A [builder-pattern] type to construct [`EventControllerKey`] objects.
219///
220/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
221#[must_use = "The builder must be built to be used"]
222pub struct EventControllerKeyBuilder {
223    builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
224}
225
226impl EventControllerKeyBuilder {
227    fn new() -> Self {
228        Self {
229            builder: glib::object::Object::builder(),
230        }
231    }
232
233    /// The name for this controller, typically used for debugging purposes.
234    pub fn name(self, name: impl Into<glib::GString>) -> Self {
235        Self {
236            builder: self.builder.property("name", name.into()),
237        }
238    }
239
240    /// The limit for which events this controller will handle.
241    pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
242        Self {
243            builder: self
244                .builder
245                .property("propagation-limit", propagation_limit),
246        }
247    }
248
249    /// The propagation phase at which this controller will handle events.
250    pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
251        Self {
252            builder: self
253                .builder
254                .property("propagation-phase", propagation_phase),
255        }
256    }
257
258    // rustdoc-stripper-ignore-next
259    /// Build the [`EventControllerKey`].
260    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
261    pub fn build(self) -> EventControllerKey {
262        assert_initialized_main_thread!();
263        self.builder.build()
264    }
265}