gtk4/auto/
shortcut.rs
1use crate::{ffi, ShortcutAction, ShortcutTrigger};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 #[doc(alias = "GtkShortcut")]
54 pub struct Shortcut(Object<ffi::GtkShortcut, ffi::GtkShortcutClass>);
55
56 match fn {
57 type_ => || ffi::gtk_shortcut_get_type(),
58 }
59}
60
61impl Shortcut {
62 #[doc(alias = "gtk_shortcut_new")]
74 pub fn new(
75 trigger: Option<impl IsA<ShortcutTrigger>>,
76 action: Option<impl IsA<ShortcutAction>>,
77 ) -> Shortcut {
78 assert_initialized_main_thread!();
79 unsafe {
80 from_glib_full(ffi::gtk_shortcut_new(
81 trigger.map(|p| p.upcast()).into_glib_ptr(),
82 action.map(|p| p.upcast()).into_glib_ptr(),
83 ))
84 }
85 }
86
87 pub fn builder() -> ShortcutBuilder {
92 ShortcutBuilder::new()
93 }
94
95 #[doc(alias = "gtk_shortcut_get_action")]
101 #[doc(alias = "get_action")]
102 pub fn action(&self) -> Option<ShortcutAction> {
103 unsafe { from_glib_none(ffi::gtk_shortcut_get_action(self.to_glib_none().0)) }
104 }
105
106 #[doc(alias = "gtk_shortcut_get_arguments")]
112 #[doc(alias = "get_arguments")]
113 pub fn arguments(&self) -> Option<glib::Variant> {
114 unsafe { from_glib_none(ffi::gtk_shortcut_get_arguments(self.to_glib_none().0)) }
115 }
116
117 #[doc(alias = "gtk_shortcut_get_trigger")]
123 #[doc(alias = "get_trigger")]
124 pub fn trigger(&self) -> Option<ShortcutTrigger> {
125 unsafe { from_glib_none(ffi::gtk_shortcut_get_trigger(self.to_glib_none().0)) }
126 }
127
128 #[doc(alias = "gtk_shortcut_set_action")]
133 #[doc(alias = "action")]
134 pub fn set_action(&self, action: Option<impl IsA<ShortcutAction>>) {
135 unsafe {
136 ffi::gtk_shortcut_set_action(
137 self.to_glib_none().0,
138 action.map(|p| p.upcast()).into_glib_ptr(),
139 );
140 }
141 }
142
143 #[doc(alias = "gtk_shortcut_set_arguments")]
147 #[doc(alias = "arguments")]
148 pub fn set_arguments(&self, args: Option<&glib::Variant>) {
149 unsafe {
150 ffi::gtk_shortcut_set_arguments(self.to_glib_none().0, args.to_glib_none().0);
151 }
152 }
153
154 #[doc(alias = "gtk_shortcut_set_trigger")]
159 #[doc(alias = "trigger")]
160 pub fn set_trigger(&self, trigger: Option<impl IsA<ShortcutTrigger>>) {
161 unsafe {
162 ffi::gtk_shortcut_set_trigger(
163 self.to_glib_none().0,
164 trigger.map(|p| p.upcast()).into_glib_ptr(),
165 );
166 }
167 }
168
169 #[doc(alias = "action")]
170 pub fn connect_action_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
171 unsafe extern "C" fn notify_action_trampoline<F: Fn(&Shortcut) + 'static>(
172 this: *mut ffi::GtkShortcut,
173 _param_spec: glib::ffi::gpointer,
174 f: glib::ffi::gpointer,
175 ) {
176 let f: &F = &*(f as *const F);
177 f(&from_glib_borrow(this))
178 }
179 unsafe {
180 let f: Box_<F> = Box_::new(f);
181 connect_raw(
182 self.as_ptr() as *mut _,
183 b"notify::action\0".as_ptr() as *const _,
184 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
185 notify_action_trampoline::<F> as *const (),
186 )),
187 Box_::into_raw(f),
188 )
189 }
190 }
191
192 #[doc(alias = "arguments")]
193 pub fn connect_arguments_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
194 unsafe extern "C" fn notify_arguments_trampoline<F: Fn(&Shortcut) + 'static>(
195 this: *mut ffi::GtkShortcut,
196 _param_spec: glib::ffi::gpointer,
197 f: glib::ffi::gpointer,
198 ) {
199 let f: &F = &*(f as *const F);
200 f(&from_glib_borrow(this))
201 }
202 unsafe {
203 let f: Box_<F> = Box_::new(f);
204 connect_raw(
205 self.as_ptr() as *mut _,
206 b"notify::arguments\0".as_ptr() as *const _,
207 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
208 notify_arguments_trampoline::<F> as *const (),
209 )),
210 Box_::into_raw(f),
211 )
212 }
213 }
214
215 #[doc(alias = "trigger")]
216 pub fn connect_trigger_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
217 unsafe extern "C" fn notify_trigger_trampoline<F: Fn(&Shortcut) + 'static>(
218 this: *mut ffi::GtkShortcut,
219 _param_spec: glib::ffi::gpointer,
220 f: glib::ffi::gpointer,
221 ) {
222 let f: &F = &*(f as *const F);
223 f(&from_glib_borrow(this))
224 }
225 unsafe {
226 let f: Box_<F> = Box_::new(f);
227 connect_raw(
228 self.as_ptr() as *mut _,
229 b"notify::trigger\0".as_ptr() as *const _,
230 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
231 notify_trigger_trampoline::<F> as *const (),
232 )),
233 Box_::into_raw(f),
234 )
235 }
236 }
237}
238
239impl Default for Shortcut {
240 fn default() -> Self {
241 glib::object::Object::new::<Self>()
242 }
243}
244
245#[must_use = "The builder must be built to be used"]
250pub struct ShortcutBuilder {
251 builder: glib::object::ObjectBuilder<'static, Shortcut>,
252}
253
254impl ShortcutBuilder {
255 fn new() -> Self {
256 Self {
257 builder: glib::object::Object::builder(),
258 }
259 }
260
261 pub fn action(self, action: &impl IsA<ShortcutAction>) -> Self {
263 Self {
264 builder: self.builder.property("action", action.clone().upcast()),
265 }
266 }
267
268 pub fn arguments(self, arguments: &glib::Variant) -> Self {
270 Self {
271 builder: self.builder.property("arguments", arguments.clone()),
272 }
273 }
274
275 pub fn trigger(self, trigger: &impl IsA<ShortcutTrigger>) -> Self {
277 Self {
278 builder: self.builder.property("trigger", trigger.clone().upcast()),
279 }
280 }
281
282 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
285 pub fn build(self) -> Shortcut {
286 assert_initialized_main_thread!();
287 self.builder.build()
288 }
289}