gtk4/auto/
shortcut_action.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, ShortcutActionFlags, Widget};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// [`ShortcutAction`][crate::ShortcutAction] encodes an action that can be triggered by a
10    /// keyboard shortcut.
11    ///
12    /// `GtkShortcutActions` contain functions that allow easy presentation
13    /// to end users as well as being printed for debugging.
14    ///
15    /// All `GtkShortcutActions` are immutable, you can only specify their
16    /// properties during construction. If you want to change a action, you
17    /// have to replace it with a new one. If you need to pass arguments to
18    /// an action, these are specified by the higher-level [`Shortcut`][crate::Shortcut] object.
19    ///
20    /// To activate a [`ShortcutAction`][crate::ShortcutAction] manually, [`ShortcutActionExt::activate()`][crate::prelude::ShortcutActionExt::activate()]
21    /// can be called.
22    ///
23    /// GTK provides various actions:
24    ///
25    ///  - [`MnemonicAction`][crate::MnemonicAction]: a shortcut action that calls
26    ///    gtk_widget_mnemonic_activate()
27    ///  - [`CallbackAction`][crate::CallbackAction]: a shortcut action that invokes
28    ///    a given callback
29    ///  - [`SignalAction`][crate::SignalAction]: a shortcut action that emits a
30    ///    given signal
31    ///  - [`ActivateAction`][crate::ActivateAction]: a shortcut action that calls
32    ///    gtk_widget_activate()
33    ///  - [`NamedAction`][crate::NamedAction]: a shortcut action that calls
34    ///    gtk_widget_activate_action()
35    ///  - [`NothingAction`][crate::NothingAction]: a shortcut action that does nothing
36    ///
37    /// This is an Abstract Base Class, you cannot instantiate it.
38    ///
39    /// # Implements
40    ///
41    /// [`ShortcutActionExt`][trait@crate::prelude::ShortcutActionExt], [`trait@glib::ObjectExt`]
42    #[doc(alias = "GtkShortcutAction")]
43    pub struct ShortcutAction(Object<ffi::GtkShortcutAction, ffi::GtkShortcutActionClass>);
44
45    match fn {
46        type_ => || ffi::gtk_shortcut_action_get_type(),
47    }
48}
49
50impl ShortcutAction {
51    pub const NONE: Option<&'static ShortcutAction> = None;
52
53    /// Tries to parse the given string into an action.
54    ///
55    /// On success, the parsed action is returned. When parsing
56    /// failed, [`None`] is returned.
57    ///
58    /// The accepted strings are:
59    ///
60    /// - `nothing`, for [`NothingAction`][crate::NothingAction]
61    /// - `activate`, for [`ActivateAction`][crate::ActivateAction]
62    /// - `mnemonic-activate`, for [`MnemonicAction`][crate::MnemonicAction]
63    /// - `action(NAME)`, for a [`NamedAction`][crate::NamedAction] for the action named `NAME`
64    /// - `signal(NAME)`, for a [`SignalAction`][crate::SignalAction] for the signal `NAME`
65    /// ## `string`
66    /// the string to parse
67    ///
68    /// # Returns
69    ///
70    /// a new [`ShortcutAction`][crate::ShortcutAction]
71    #[doc(alias = "gtk_shortcut_action_parse_string")]
72    pub fn parse_string(string: &str) -> Option<ShortcutAction> {
73        assert_initialized_main_thread!();
74        unsafe {
75            from_glib_full(ffi::gtk_shortcut_action_parse_string(
76                string.to_glib_none().0,
77            ))
78        }
79    }
80}
81
82impl std::fmt::Display for ShortcutAction {
83    #[inline]
84    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
85        f.write_str(&ShortcutActionExt::to_str(self))
86    }
87}
88
89mod sealed {
90    pub trait Sealed {}
91    impl<T: super::IsA<super::ShortcutAction>> Sealed for T {}
92}
93
94/// Trait containing all [`struct@ShortcutAction`] methods.
95///
96/// # Implementors
97///
98/// [`ActivateAction`][struct@crate::ActivateAction], [`CallbackAction`][struct@crate::CallbackAction], [`MnemonicAction`][struct@crate::MnemonicAction], [`NamedAction`][struct@crate::NamedAction], [`NothingAction`][struct@crate::NothingAction], [`ShortcutAction`][struct@crate::ShortcutAction], [`SignalAction`][struct@crate::SignalAction]
99pub trait ShortcutActionExt: IsA<ShortcutAction> + sealed::Sealed + 'static {
100    /// Activates the action on the @widget with the given @args.
101    ///
102    /// Note that some actions ignore the passed in @flags, @widget or @args.
103    ///
104    /// Activation of an action can fail for various reasons. If the action
105    /// is not supported by the @widget, if the @args don't match the action
106    /// or if the activation otherwise had no effect, [`false`] will be returned.
107    /// ## `flags`
108    /// flags to activate with
109    /// ## `widget`
110    /// Target of the activation
111    /// ## `args`
112    /// arguments to pass
113    ///
114    /// # Returns
115    ///
116    /// [`true`] if this action was activated successfully
117    #[doc(alias = "gtk_shortcut_action_activate")]
118    fn activate(
119        &self,
120        flags: ShortcutActionFlags,
121        widget: &impl IsA<Widget>,
122        args: Option<&glib::Variant>,
123    ) -> bool {
124        unsafe {
125            from_glib(ffi::gtk_shortcut_action_activate(
126                self.as_ref().to_glib_none().0,
127                flags.into_glib(),
128                widget.as_ref().to_glib_none().0,
129                args.to_glib_none().0,
130            ))
131        }
132    }
133
134    /// Prints the given action into a human-readable string.
135    ///
136    /// This is a small wrapper around `Gtk::ShortcutAction::print()`
137    /// to help when debugging.
138    ///
139    /// # Returns
140    ///
141    /// a new string
142    #[doc(alias = "gtk_shortcut_action_to_string")]
143    #[doc(alias = "to_string")]
144    fn to_str(&self) -> glib::GString {
145        unsafe {
146            from_glib_full(ffi::gtk_shortcut_action_to_string(
147                self.as_ref().to_glib_none().0,
148            ))
149        }
150    }
151}
152
153impl<O: IsA<ShortcutAction>> ShortcutActionExt for O {}