1use crate::{ShortcutAction, ShortcutTrigger, ffi};
6use glib::{
7 prelude::*,
8 signal::{SignalHandlerId, connect_raw},
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 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"notify::action".as_ptr() as *const _,
186 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
187 notify_action_trampoline::<F> as *const (),
188 )),
189 Box_::into_raw(f),
190 )
191 }
192 }
193
194 #[doc(alias = "arguments")]
195 pub fn connect_arguments_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
196 unsafe extern "C" fn notify_arguments_trampoline<F: Fn(&Shortcut) + 'static>(
197 this: *mut ffi::GtkShortcut,
198 _param_spec: glib::ffi::gpointer,
199 f: glib::ffi::gpointer,
200 ) {
201 unsafe {
202 let f: &F = &*(f as *const F);
203 f(&from_glib_borrow(this))
204 }
205 }
206 unsafe {
207 let f: Box_<F> = Box_::new(f);
208 connect_raw(
209 self.as_ptr() as *mut _,
210 c"notify::arguments".as_ptr() as *const _,
211 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
212 notify_arguments_trampoline::<F> as *const (),
213 )),
214 Box_::into_raw(f),
215 )
216 }
217 }
218
219 #[doc(alias = "trigger")]
220 pub fn connect_trigger_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
221 unsafe extern "C" fn notify_trigger_trampoline<F: Fn(&Shortcut) + 'static>(
222 this: *mut ffi::GtkShortcut,
223 _param_spec: glib::ffi::gpointer,
224 f: glib::ffi::gpointer,
225 ) {
226 unsafe {
227 let f: &F = &*(f as *const F);
228 f(&from_glib_borrow(this))
229 }
230 }
231 unsafe {
232 let f: Box_<F> = Box_::new(f);
233 connect_raw(
234 self.as_ptr() as *mut _,
235 c"notify::trigger".as_ptr() as *const _,
236 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
237 notify_trigger_trampoline::<F> as *const (),
238 )),
239 Box_::into_raw(f),
240 )
241 }
242 }
243}
244
245impl Default for Shortcut {
246 fn default() -> Self {
247 glib::object::Object::new::<Self>()
248 }
249}
250
251#[must_use = "The builder must be built to be used"]
256pub struct ShortcutBuilder {
257 builder: glib::object::ObjectBuilder<'static, Shortcut>,
258}
259
260impl ShortcutBuilder {
261 fn new() -> Self {
262 Self {
263 builder: glib::object::Object::builder(),
264 }
265 }
266
267 pub fn action(self, action: &impl IsA<ShortcutAction>) -> Self {
269 Self {
270 builder: self.builder.property("action", action.clone().upcast()),
271 }
272 }
273
274 pub fn arguments(self, arguments: &glib::Variant) -> Self {
276 Self {
277 builder: self.builder.property("arguments", arguments.clone()),
278 }
279 }
280
281 pub fn trigger(self, trigger: &impl IsA<ShortcutTrigger>) -> Self {
283 Self {
284 builder: self.builder.property("trigger", trigger.clone().upcast()),
285 }
286 }
287
288 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
291 pub fn build(self) -> Shortcut {
292 assert_initialized_main_thread!();
293 self.builder.build()
294 }
295}