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::{ffi, EventController, IMContext, PropagationLimit, PropagationPhase, Widget};
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 /// 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`]
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 let f: &F = &*(f as *const F);
155 f(&from_glib_borrow(this))
156 }
157 unsafe {
158 let f: Box_<F> = Box_::new(f);
159 connect_raw(
160 self.as_ptr() as *mut _,
161 c"im-update".as_ptr() as *const _,
162 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
163 im_update_trampoline::<F> as *const (),
164 )),
165 Box_::into_raw(f),
166 )
167 }
168 }
169
170 /// Emitted whenever the state of modifier keys and pointer buttons change.
171 /// ## `state`
172 /// the bitmask, representing the new state of modifier keys and
173 /// pointer buttons.
174 ///
175 /// # Returns
176 ///
177 /// whether to ignore modifiers
178 #[doc(alias = "modifiers")]
179 pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> glib::Propagation + 'static>(
180 &self,
181 f: F,
182 ) -> SignalHandlerId {
183 unsafe extern "C" fn modifiers_trampoline<
184 F: Fn(&EventControllerKey, gdk::ModifierType) -> glib::Propagation + 'static,
185 >(
186 this: *mut ffi::GtkEventControllerKey,
187 state: gdk::ffi::GdkModifierType,
188 f: glib::ffi::gpointer,
189 ) -> glib::ffi::gboolean {
190 let f: &F = &*(f as *const F);
191 f(&from_glib_borrow(this), from_glib(state)).into_glib()
192 }
193 unsafe {
194 let f: Box_<F> = Box_::new(f);
195 connect_raw(
196 self.as_ptr() as *mut _,
197 c"modifiers".as_ptr() as *const _,
198 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
199 modifiers_trampoline::<F> as *const (),
200 )),
201 Box_::into_raw(f),
202 )
203 }
204 }
205}
206
207impl Default for EventControllerKey {
208 fn default() -> Self {
209 Self::new()
210 }
211}
212
213// rustdoc-stripper-ignore-next
214/// A [builder-pattern] type to construct [`EventControllerKey`] objects.
215///
216/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
217#[must_use = "The builder must be built to be used"]
218pub struct EventControllerKeyBuilder {
219 builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
220}
221
222impl EventControllerKeyBuilder {
223 fn new() -> Self {
224 Self {
225 builder: glib::object::Object::builder(),
226 }
227 }
228
229 /// The name for this controller, typically used for debugging purposes.
230 pub fn name(self, name: impl Into<glib::GString>) -> Self {
231 Self {
232 builder: self.builder.property("name", name.into()),
233 }
234 }
235
236 /// The limit for which events this controller will handle.
237 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
238 Self {
239 builder: self
240 .builder
241 .property("propagation-limit", propagation_limit),
242 }
243 }
244
245 /// The propagation phase at which this controller will handle events.
246 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
247 Self {
248 builder: self
249 .builder
250 .property("propagation-phase", propagation_phase),
251 }
252 }
253
254 // rustdoc-stripper-ignore-next
255 /// Build the [`EventControllerKey`].
256 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
257 pub fn build(self) -> EventControllerKey {
258 assert_initialized_main_thread!();
259 self.builder.build()
260 }
261}