Skip to main content

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::{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    /// 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            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// rustdoc-stripper-ignore-next
252/// A [builder-pattern] type to construct [`Shortcut`] objects.
253///
254/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
255#[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    /// The action that gets activated by this shortcut.
268    pub fn action(self, action: &impl IsA<ShortcutAction>) -> Self {
269        Self {
270            builder: self.builder.property("action", action.clone().upcast()),
271        }
272    }
273
274    /// Arguments passed to activation.
275    pub fn arguments(self, arguments: &glib::Variant) -> Self {
276        Self {
277            builder: self.builder.property("arguments", arguments.clone()),
278        }
279    }
280
281    /// The trigger that triggers this shortcut.
282    pub fn trigger(self, trigger: &impl IsA<ShortcutTrigger>) -> Self {
283        Self {
284            builder: self.builder.property("trigger", trigger.clone().upcast()),
285        }
286    }
287
288    // rustdoc-stripper-ignore-next
289    /// Build the [`Shortcut`].
290    #[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}