1use crate::{EventController, IMContext, 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 #[doc(alias = "GtkEventControllerKey")]
54 pub struct EventControllerKey(Object<ffi::GtkEventControllerKey, ffi::GtkEventControllerKeyClass>) @extends EventController;
55
56 match fn {
57 type_ => || ffi::gtk_event_controller_key_get_type(),
58 }
59}
60
61impl EventControllerKey {
62 #[doc(alias = "gtk_event_controller_key_new")]
63 pub fn new(widget: &impl IsA<Widget>) -> EventControllerKey {
64 skip_assert_initialized!();
65 unsafe {
66 EventController::from_glib_full(ffi::gtk_event_controller_key_new(
67 widget.as_ref().to_glib_none().0,
68 ))
69 .unsafe_cast()
70 }
71 }
72
73 pub fn builder() -> EventControllerKeyBuilder {
78 EventControllerKeyBuilder::new()
79 }
80
81 #[doc(alias = "gtk_event_controller_key_forward")]
82 pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
83 unsafe {
84 from_glib(ffi::gtk_event_controller_key_forward(
85 self.to_glib_none().0,
86 widget.as_ref().to_glib_none().0,
87 ))
88 }
89 }
90
91 #[doc(alias = "gtk_event_controller_key_get_group")]
92 #[doc(alias = "get_group")]
93 pub fn group(&self) -> u32 {
94 unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
95 }
96
97 #[doc(alias = "gtk_event_controller_key_get_im_context")]
103 #[doc(alias = "get_im_context")]
104 pub fn im_context(&self) -> Option<IMContext> {
105 unsafe {
106 from_glib_none(ffi::gtk_event_controller_key_get_im_context(
107 self.to_glib_none().0,
108 ))
109 }
110 }
111
112 #[doc(alias = "gtk_event_controller_key_set_im_context")]
113 pub fn set_im_context(&self, im_context: &impl IsA<IMContext>) {
114 unsafe {
115 ffi::gtk_event_controller_key_set_im_context(
116 self.to_glib_none().0,
117 im_context.as_ref().to_glib_none().0,
118 );
119 }
120 }
121
122 #[doc(alias = "focus-in")]
123 pub fn connect_focus_in<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
124 unsafe extern "C" fn focus_in_trampoline<F: Fn(&EventControllerKey) + 'static>(
125 this: *mut ffi::GtkEventControllerKey,
126 f: glib::ffi::gpointer,
127 ) {
128 unsafe {
129 let f: &F = &*(f as *const F);
130 f(&from_glib_borrow(this))
131 }
132 }
133 unsafe {
134 let f: Box_<F> = Box_::new(f);
135 connect_raw(
136 self.as_ptr() as *mut _,
137 c"focus-in".as_ptr(),
138 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
139 focus_in_trampoline::<F> as *const (),
140 )),
141 Box_::into_raw(f),
142 )
143 }
144 }
145
146 #[doc(alias = "focus-out")]
147 pub fn connect_focus_out<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
148 unsafe extern "C" fn focus_out_trampoline<F: Fn(&EventControllerKey) + 'static>(
149 this: *mut ffi::GtkEventControllerKey,
150 f: glib::ffi::gpointer,
151 ) {
152 unsafe {
153 let f: &F = &*(f as *const F);
154 f(&from_glib_borrow(this))
155 }
156 }
157 unsafe {
158 let f: Box_<F> = Box_::new(f);
159 connect_raw(
160 self.as_ptr() as *mut _,
161 c"focus-out".as_ptr(),
162 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
163 focus_out_trampoline::<F> as *const (),
164 )),
165 Box_::into_raw(f),
166 )
167 }
168 }
169
170 #[doc(alias = "im-update")]
171 pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
172 unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
173 this: *mut ffi::GtkEventControllerKey,
174 f: glib::ffi::gpointer,
175 ) {
176 unsafe {
177 let f: &F = &*(f as *const F);
178 f(&from_glib_borrow(this))
179 }
180 }
181 unsafe {
182 let f: Box_<F> = Box_::new(f);
183 connect_raw(
184 self.as_ptr() as *mut _,
185 c"im-update".as_ptr(),
186 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
187 im_update_trampoline::<F> as *const (),
188 )),
189 Box_::into_raw(f),
190 )
191 }
192 }
193
194 #[cfg(feature = "v3_24")]
206 #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
207 #[doc(alias = "key-pressed")]
208 pub fn connect_key_pressed<F: Fn(&Self, u32, u32, gdk::ModifierType) -> bool + 'static>(
209 &self,
210 f: F,
211 ) -> SignalHandlerId {
212 unsafe extern "C" fn key_pressed_trampoline<
213 F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) -> bool + 'static,
214 >(
215 this: *mut ffi::GtkEventControllerKey,
216 keyval: std::ffi::c_uint,
217 keycode: std::ffi::c_uint,
218 state: gdk::ffi::GdkModifierType,
219 f: glib::ffi::gpointer,
220 ) -> glib::ffi::gboolean {
221 unsafe {
222 let f: &F = &*(f as *const F);
223 f(&from_glib_borrow(this), keyval, keycode, from_glib(state)).into_glib()
224 }
225 }
226 unsafe {
227 let f: Box_<F> = Box_::new(f);
228 connect_raw(
229 self.as_ptr() as *mut _,
230 c"key-pressed".as_ptr(),
231 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
232 key_pressed_trampoline::<F> as *const (),
233 )),
234 Box_::into_raw(f),
235 )
236 }
237 }
238
239 #[cfg(feature = "v3_24")]
247 #[cfg_attr(docsrs, doc(cfg(feature = "v3_24")))]
248 #[doc(alias = "key-released")]
249 pub fn connect_key_released<F: Fn(&Self, u32, u32, gdk::ModifierType) + 'static>(
250 &self,
251 f: F,
252 ) -> SignalHandlerId {
253 unsafe extern "C" fn key_released_trampoline<
254 F: Fn(&EventControllerKey, u32, u32, gdk::ModifierType) + 'static,
255 >(
256 this: *mut ffi::GtkEventControllerKey,
257 keyval: std::ffi::c_uint,
258 keycode: std::ffi::c_uint,
259 state: gdk::ffi::GdkModifierType,
260 f: glib::ffi::gpointer,
261 ) {
262 unsafe {
263 let f: &F = &*(f as *const F);
264 f(&from_glib_borrow(this), keyval, keycode, from_glib(state))
265 }
266 }
267 unsafe {
268 let f: Box_<F> = Box_::new(f);
269 connect_raw(
270 self.as_ptr() as *mut _,
271 c"key-released".as_ptr(),
272 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
273 key_released_trampoline::<F> as *const (),
274 )),
275 Box_::into_raw(f),
276 )
277 }
278 }
279
280 #[doc(alias = "modifiers")]
281 pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> bool + 'static>(
282 &self,
283 f: F,
284 ) -> SignalHandlerId {
285 unsafe extern "C" fn modifiers_trampoline<
286 F: Fn(&EventControllerKey, gdk::ModifierType) -> bool + 'static,
287 >(
288 this: *mut ffi::GtkEventControllerKey,
289 object: gdk::ffi::GdkModifierType,
290 f: glib::ffi::gpointer,
291 ) -> glib::ffi::gboolean {
292 unsafe {
293 let f: &F = &*(f as *const F);
294 f(&from_glib_borrow(this), from_glib(object)).into_glib()
295 }
296 }
297 unsafe {
298 let f: Box_<F> = Box_::new(f);
299 connect_raw(
300 self.as_ptr() as *mut _,
301 c"modifiers".as_ptr(),
302 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
303 modifiers_trampoline::<F> as *const (),
304 )),
305 Box_::into_raw(f),
306 )
307 }
308 }
309}
310
311impl Default for EventControllerKey {
312 fn default() -> Self {
313 glib::object::Object::new::<Self>()
314 }
315}
316
317#[must_use = "The builder must be built to be used"]
322pub struct EventControllerKeyBuilder {
323 builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
324}
325
326impl EventControllerKeyBuilder {
327 fn new() -> Self {
328 Self {
329 builder: glib::object::Object::builder(),
330 }
331 }
332
333 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
335 Self {
336 builder: self
337 .builder
338 .property("propagation-phase", propagation_phase),
339 }
340 }
341
342 pub fn widget(self, widget: &impl IsA<Widget>) -> Self {
344 Self {
345 builder: self.builder.property("widget", widget.clone().upcast()),
346 }
347 }
348
349 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
352 pub fn build(self) -> EventControllerKey {
353 assert_initialized_main_thread!();
354 self.builder.build()
355 }
356}