gtk4/auto/
event_controller_key.rs1use 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 #[doc(alias = "GtkEventControllerKey")]
16 pub struct EventControllerKey(Object<ffi::GtkEventControllerKey, ffi::GtkEventControllerKeyClass>) @extends EventController;
17
18 match fn {
19 type_ => || ffi::gtk_event_controller_key_get_type(),
20 }
21}
22
23impl EventControllerKey {
24 #[doc(alias = "gtk_event_controller_key_new")]
25 pub fn new() -> EventControllerKey {
26 assert_initialized_main_thread!();
27 unsafe {
28 EventController::from_glib_full(ffi::gtk_event_controller_key_new()).unsafe_cast()
29 }
30 }
31
32 pub fn builder() -> EventControllerKeyBuilder {
37 EventControllerKeyBuilder::new()
38 }
39
40 #[doc(alias = "gtk_event_controller_key_forward")]
41 pub fn forward(&self, widget: &impl IsA<Widget>) -> bool {
42 unsafe {
43 from_glib(ffi::gtk_event_controller_key_forward(
44 self.to_glib_none().0,
45 widget.as_ref().to_glib_none().0,
46 ))
47 }
48 }
49
50 #[doc(alias = "gtk_event_controller_key_get_group")]
51 #[doc(alias = "get_group")]
52 pub fn group(&self) -> u32 {
53 unsafe { ffi::gtk_event_controller_key_get_group(self.to_glib_none().0) }
54 }
55
56 #[doc(alias = "gtk_event_controller_key_get_im_context")]
57 #[doc(alias = "get_im_context")]
58 pub fn im_context(&self) -> Option<IMContext> {
59 unsafe {
60 from_glib_none(ffi::gtk_event_controller_key_get_im_context(
61 self.to_glib_none().0,
62 ))
63 }
64 }
65
66 #[doc(alias = "gtk_event_controller_key_set_im_context")]
67 pub fn set_im_context(&self, im_context: Option<&impl IsA<IMContext>>) {
68 unsafe {
69 ffi::gtk_event_controller_key_set_im_context(
70 self.to_glib_none().0,
71 im_context.map(|p| p.as_ref()).to_glib_none().0,
72 );
73 }
74 }
75
76 #[doc(alias = "im-update")]
77 pub fn connect_im_update<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
78 unsafe extern "C" fn im_update_trampoline<F: Fn(&EventControllerKey) + 'static>(
79 this: *mut ffi::GtkEventControllerKey,
80 f: glib::ffi::gpointer,
81 ) {
82 let f: &F = &*(f as *const F);
83 f(&from_glib_borrow(this))
84 }
85 unsafe {
86 let f: Box_<F> = Box_::new(f);
87 connect_raw(
88 self.as_ptr() as *mut _,
89 c"im-update".as_ptr() as *const _,
90 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
91 im_update_trampoline::<F> as *const (),
92 )),
93 Box_::into_raw(f),
94 )
95 }
96 }
97
98 #[doc(alias = "modifiers")]
99 pub fn connect_modifiers<F: Fn(&Self, gdk::ModifierType) -> glib::Propagation + 'static>(
100 &self,
101 f: F,
102 ) -> SignalHandlerId {
103 unsafe extern "C" fn modifiers_trampoline<
104 F: Fn(&EventControllerKey, gdk::ModifierType) -> glib::Propagation + 'static,
105 >(
106 this: *mut ffi::GtkEventControllerKey,
107 state: gdk::ffi::GdkModifierType,
108 f: glib::ffi::gpointer,
109 ) -> glib::ffi::gboolean {
110 let f: &F = &*(f as *const F);
111 f(&from_glib_borrow(this), from_glib(state)).into_glib()
112 }
113 unsafe {
114 let f: Box_<F> = Box_::new(f);
115 connect_raw(
116 self.as_ptr() as *mut _,
117 c"modifiers".as_ptr() as *const _,
118 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
119 modifiers_trampoline::<F> as *const (),
120 )),
121 Box_::into_raw(f),
122 )
123 }
124 }
125}
126
127impl Default for EventControllerKey {
128 fn default() -> Self {
129 Self::new()
130 }
131}
132
133#[must_use = "The builder must be built to be used"]
138pub struct EventControllerKeyBuilder {
139 builder: glib::object::ObjectBuilder<'static, EventControllerKey>,
140}
141
142impl EventControllerKeyBuilder {
143 fn new() -> Self {
144 Self {
145 builder: glib::object::Object::builder(),
146 }
147 }
148
149 pub fn name(self, name: impl Into<glib::GString>) -> Self {
150 Self {
151 builder: self.builder.property("name", name.into()),
152 }
153 }
154
155 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
156 Self {
157 builder: self
158 .builder
159 .property("propagation-limit", propagation_limit),
160 }
161 }
162
163 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
164 Self {
165 builder: self
166 .builder
167 .property("propagation-phase", propagation_phase),
168 }
169 }
170
171 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
174 pub fn build(self) -> EventControllerKey {
175 assert_initialized_main_thread!();
176 self.builder.build()
177 }
178}