gtk4/auto/
shortcut.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, ShortcutAction, ShortcutTrigger};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// A [`Shortcut`][crate::Shortcut] describes a keyboard shortcut.
15    ///
16    /// It contains a description of how to trigger the shortcut via a
17    /// [`ShortcutTrigger`][crate::ShortcutTrigger] and a way to activate the shortcut
18    /// on a widget via a [`ShortcutAction`][crate::ShortcutAction].
19    ///
20    /// The actual work is usually done via [`ShortcutController`][crate::ShortcutController],
21    /// which decides if and when to activate a shortcut. Using that controller
22    /// directly however is rarely necessary as various higher level
23    /// convenience APIs exist on [`Widget`][crate::Widget]s that make it easier to use
24    /// shortcuts in GTK.
25    ///
26    /// [`Shortcut`][crate::Shortcut] does provide functionality to make it easy for users
27    /// to work with shortcuts, either by providing informational strings
28    /// for display purposes or by allowing shortcuts to be configured.
29    ///
30    /// ## Properties
31    ///
32    ///
33    /// #### `action`
34    ///  The action that gets activated by this shortcut.
35    ///
36    /// Readable | Writeable
37    ///
38    ///
39    /// #### `arguments`
40    ///  Arguments passed to activation.
41    ///
42    /// Readable | Writeable
43    ///
44    ///
45    /// #### `trigger`
46    ///  The trigger that triggers this shortcut.
47    ///
48    /// Readable | Writeable
49    ///
50    /// # Implements
51    ///
52    /// [`trait@glib::ObjectExt`]
53    #[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    /// Creates a new [`Shortcut`][crate::Shortcut] that is triggered by
63    /// @trigger and then activates @action.
64    /// ## `trigger`
65    /// The trigger that will trigger the shortcut
66    /// ## `action`
67    /// The action that will be activated upon
68    ///    triggering
69    ///
70    /// # Returns
71    ///
72    /// a new [`Shortcut`][crate::Shortcut]
73    #[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    // rustdoc-stripper-ignore-next
88    /// Creates a new builder-pattern struct instance to construct [`Shortcut`] objects.
89    ///
90    /// This method returns an instance of [`ShortcutBuilder`](crate::builders::ShortcutBuilder) which can be used to create [`Shortcut`] objects.
91    pub fn builder() -> ShortcutBuilder {
92        ShortcutBuilder::new()
93    }
94
95    /// Gets the action that is activated by this shortcut.
96    ///
97    /// # Returns
98    ///
99    /// the action
100    #[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    /// Gets the arguments that are passed when activating the shortcut.
107    ///
108    /// # Returns
109    ///
110    /// the arguments
111    #[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    /// Gets the trigger used to trigger @self.
118    ///
119    /// # Returns
120    ///
121    /// the trigger used
122    #[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    /// Sets the new action for @self to be @action.
129    /// ## `action`
130    /// The new action.
131    ///   If the @action is [`None`], the nothing action will be used.
132    #[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    /// Sets the arguments to pass when activating the shortcut.
144    /// ## `args`
145    /// arguments to pass when activating @self
146    #[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    /// Sets the new trigger for @self to be @trigger.
155    /// ## `trigger`
156    /// The new trigger.
157    ///   If the @trigger is [`None`], the never trigger will be used.
158    #[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// rustdoc-stripper-ignore-next
246/// A [builder-pattern] type to construct [`Shortcut`] objects.
247///
248/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
249#[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    /// The action that gets activated by this shortcut.
262    pub fn action(self, action: &impl IsA<ShortcutAction>) -> Self {
263        Self {
264            builder: self.builder.property("action", action.clone().upcast()),
265        }
266    }
267
268    /// Arguments passed to activation.
269    pub fn arguments(self, arguments: &glib::Variant) -> Self {
270        Self {
271            builder: self.builder.property("arguments", arguments.clone()),
272        }
273    }
274
275    /// The trigger that triggers this shortcut.
276    pub fn trigger(self, trigger: &impl IsA<ShortcutTrigger>) -> Self {
277        Self {
278            builder: self.builder.property("trigger", trigger.clone().upcast()),
279        }
280    }
281
282    // rustdoc-stripper-ignore-next
283    /// Build the [`Shortcut`].
284    #[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}