Skip to main content

gtk4/auto/
shortcut_controller.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::{Buildable, EventController, Shortcut, ShortcutScope, ffi};
6use glib::{
7    prelude::*,
8    signal::{SignalHandlerId, connect_raw},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    ///
15    /// ```text
16    ///
17    /// This example creates a [`ActivateAction`][crate::ActivateAction] for triggering the
18    /// `activate` signal of the [`Button`][crate::Button]. See [`ShortcutAction::parse_string()`][crate::ShortcutAction::parse_string()]
19    /// for the syntax for other kinds of [`ShortcutAction`][crate::ShortcutAction]. See
20    /// [`ShortcutTrigger::parse_string()`][crate::ShortcutTrigger::parse_string()] to learn more about the syntax
21    /// for triggers.
22    ///
23    /// ## Properties
24    ///
25    ///
26    /// #### `item-type`
27    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
28    ///
29    /// Readable
30    ///
31    ///
32    /// #### `mnemonic-modifiers`
33    ///  The modifiers that need to be pressed to allow mnemonics activation.
34    ///
35    /// Readable | Writable
36    ///
37    ///
38    /// #### `model`
39    ///  A list model to take shortcuts from.
40    ///
41    /// Writable | Construct Only
42    ///
43    ///
44    /// #### `n-items`
45    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
46    ///
47    /// Readable
48    ///
49    ///
50    /// #### `scope`
51    ///  What scope the shortcuts will be handled in.
52    ///
53    /// Readable | Writable
54    /// <details><summary><h4>EventController</h4></summary>
55    ///
56    ///
57    /// #### `name`
58    ///  The name for this controller, typically used for debugging purposes.
59    ///
60    /// Readable | Writable
61    ///
62    ///
63    /// #### `propagation-limit`
64    ///  The limit for which events this controller will handle.
65    ///
66    /// Readable | Writable
67    ///
68    ///
69    /// #### `propagation-phase`
70    ///  The propagation phase at which this controller will handle events.
71    ///
72    /// Readable | Writable
73    ///
74    ///
75    /// #### `widget`
76    ///  The widget receiving the `GdkEvents` that the controller will handle.
77    ///
78    /// Readable
79    /// </details>
80    ///
81    /// # Implements
82    ///
83    /// [`EventControllerExt`][trait@crate::prelude::EventControllerExt], [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`EventControllerExtManual`][trait@crate::prelude::EventControllerExtManual]
84    #[doc(alias = "GtkShortcutController")]
85    pub struct ShortcutController(Object<ffi::GtkShortcutController, ffi::GtkShortcutControllerClass>) @extends EventController, @implements gio::ListModel, Buildable;
86
87    match fn {
88        type_ => || ffi::gtk_shortcut_controller_get_type(),
89    }
90}
91
92impl ShortcutController {
93    /// Creates a new shortcut controller.
94    ///
95    /// # Returns
96    ///
97    /// a newly created shortcut controller
98    #[doc(alias = "gtk_shortcut_controller_new")]
99    pub fn new() -> ShortcutController {
100        assert_initialized_main_thread!();
101        unsafe { EventController::from_glib_full(ffi::gtk_shortcut_controller_new()).unsafe_cast() }
102    }
103
104    /// Creates a new shortcut controller that takes its shortcuts from
105    /// the given list model.
106    ///
107    /// A controller created by this function does not let you add or
108    /// remove individual shortcuts using the shortcut controller api,
109    /// but you can change the contents of the model.
110    /// ## `model`
111    /// a `GListModel` containing shortcuts
112    ///
113    /// # Returns
114    ///
115    /// a newly created shortcut controller
116    #[doc(alias = "gtk_shortcut_controller_new_for_model")]
117    #[doc(alias = "new_for_model")]
118    pub fn for_model(model: &impl IsA<gio::ListModel>) -> ShortcutController {
119        assert_initialized_main_thread!();
120        unsafe {
121            EventController::from_glib_full(ffi::gtk_shortcut_controller_new_for_model(
122                model.as_ref().to_glib_none().0,
123            ))
124            .unsafe_cast()
125        }
126    }
127
128    /// Adds @shortcut to the list of shortcuts handled by @self.
129    ///
130    /// If this controller uses an external shortcut list, this
131    /// function does nothing.
132    /// ## `shortcut`
133    /// a [`Shortcut`][crate::Shortcut]
134    #[doc(alias = "gtk_shortcut_controller_add_shortcut")]
135    pub fn add_shortcut(&self, shortcut: Shortcut) {
136        unsafe {
137            ffi::gtk_shortcut_controller_add_shortcut(
138                self.to_glib_none().0,
139                shortcut.into_glib_ptr(),
140            );
141        }
142    }
143
144    /// Gets the mnemonics modifiers for when this controller activates its shortcuts.
145    ///
146    /// # Returns
147    ///
148    /// the controller's mnemonics modifiers
149    #[doc(alias = "gtk_shortcut_controller_get_mnemonics_modifiers")]
150    #[doc(alias = "get_mnemonics_modifiers")]
151    #[doc(alias = "mnemonic-modifiers")]
152    pub fn mnemonics_modifiers(&self) -> gdk::ModifierType {
153        unsafe {
154            from_glib(ffi::gtk_shortcut_controller_get_mnemonics_modifiers(
155                self.to_glib_none().0,
156            ))
157        }
158    }
159
160    /// Gets the scope for when this controller activates its shortcuts.
161    ///
162    /// See [`set_scope()`][Self::set_scope()] for details.
163    ///
164    /// # Returns
165    ///
166    /// the controller's scope
167    #[doc(alias = "gtk_shortcut_controller_get_scope")]
168    #[doc(alias = "get_scope")]
169    pub fn scope(&self) -> ShortcutScope {
170        unsafe {
171            from_glib(ffi::gtk_shortcut_controller_get_scope(
172                self.to_glib_none().0,
173            ))
174        }
175    }
176
177    /// Removes @shortcut from the list of shortcuts handled by @self.
178    ///
179    /// If @shortcut had not been added to @controller or this controller
180    /// uses an external shortcut list, this function does nothing.
181    /// ## `shortcut`
182    /// a [`Shortcut`][crate::Shortcut]
183    #[doc(alias = "gtk_shortcut_controller_remove_shortcut")]
184    pub fn remove_shortcut(&self, shortcut: &Shortcut) {
185        unsafe {
186            ffi::gtk_shortcut_controller_remove_shortcut(
187                self.to_glib_none().0,
188                shortcut.to_glib_none().0,
189            );
190        }
191    }
192
193    /// Sets the controller to use the given modifier for mnemonics.
194    ///
195    /// The mnemonics modifiers determines which modifiers need to be pressed to allow
196    /// activation of shortcuts with mnemonics triggers.
197    ///
198    /// GTK normally uses the Alt modifier for mnemonics, except in [`PopoverMenu`][crate::PopoverMenu]s,
199    /// where mnemonics can be triggered without any modifiers. It should be very
200    /// rarely necessary to change this, and doing so is likely to interfere with
201    /// other shortcuts.
202    ///
203    /// This value is only relevant for local shortcut controllers. Global and managed
204    /// shortcut controllers will have their shortcuts activated from other places which
205    /// have their own modifiers for activating mnemonics.
206    /// ## `modifiers`
207    /// the new mnemonics_modifiers to use
208    #[doc(alias = "gtk_shortcut_controller_set_mnemonics_modifiers")]
209    #[doc(alias = "mnemonic-modifiers")]
210    pub fn set_mnemonics_modifiers(&self, modifiers: gdk::ModifierType) {
211        unsafe {
212            ffi::gtk_shortcut_controller_set_mnemonics_modifiers(
213                self.to_glib_none().0,
214                modifiers.into_glib(),
215            );
216        }
217    }
218
219    /// Sets the controller to have the given @scope.
220    ///
221    /// The scope allows shortcuts to be activated outside of the normal
222    /// event propagation. In particular, it allows installing global
223    /// keyboard shortcuts that can be activated even when a widget does
224    /// not have focus.
225    ///
226    /// With [`ShortcutScope::Local`][crate::ShortcutScope::Local], shortcuts will only be activated
227    /// when the widget has focus.
228    /// ## `scope`
229    /// the new scope to use
230    #[doc(alias = "gtk_shortcut_controller_set_scope")]
231    #[doc(alias = "scope")]
232    pub fn set_scope(&self, scope: ShortcutScope) {
233        unsafe {
234            ffi::gtk_shortcut_controller_set_scope(self.to_glib_none().0, scope.into_glib());
235        }
236    }
237
238    #[doc(alias = "mnemonic-modifiers")]
239    pub fn connect_mnemonic_modifiers_notify<F: Fn(&Self) + 'static>(
240        &self,
241        f: F,
242    ) -> SignalHandlerId {
243        unsafe extern "C" fn notify_mnemonic_modifiers_trampoline<
244            F: Fn(&ShortcutController) + 'static,
245        >(
246            this: *mut ffi::GtkShortcutController,
247            _param_spec: glib::ffi::gpointer,
248            f: glib::ffi::gpointer,
249        ) {
250            unsafe {
251                let f: &F = &*(f as *const F);
252                f(&from_glib_borrow(this))
253            }
254        }
255        unsafe {
256            let f: Box_<F> = Box_::new(f);
257            connect_raw(
258                self.as_ptr() as *mut _,
259                c"notify::mnemonic-modifiers".as_ptr(),
260                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
261                    notify_mnemonic_modifiers_trampoline::<F> as *const (),
262                )),
263                Box_::into_raw(f),
264            )
265        }
266    }
267
268    #[doc(alias = "scope")]
269    pub fn connect_scope_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
270        unsafe extern "C" fn notify_scope_trampoline<F: Fn(&ShortcutController) + 'static>(
271            this: *mut ffi::GtkShortcutController,
272            _param_spec: glib::ffi::gpointer,
273            f: glib::ffi::gpointer,
274        ) {
275            unsafe {
276                let f: &F = &*(f as *const F);
277                f(&from_glib_borrow(this))
278            }
279        }
280        unsafe {
281            let f: Box_<F> = Box_::new(f);
282            connect_raw(
283                self.as_ptr() as *mut _,
284                c"notify::scope".as_ptr(),
285                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
286                    notify_scope_trampoline::<F> as *const (),
287                )),
288                Box_::into_raw(f),
289            )
290        }
291    }
292}
293
294impl Default for ShortcutController {
295    fn default() -> Self {
296        Self::new()
297    }
298}