Skip to main content

gio/auto/
simple_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::{Action, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// A `GSimpleAction` is the obvious simple implementation of the
16    /// [`Action`][crate::Action] interface. This is the easiest way to create an action for
17    /// purposes of adding it to a [`SimpleActionGroup`][crate::SimpleActionGroup].
18    ///
19    /// ## Properties
20    ///
21    ///
22    /// #### `enabled`
23    ///  If @action is currently enabled.
24    ///
25    /// If the action is disabled then calls to g_action_activate() and
26    /// g_action_change_state() have no effect.
27    ///
28    /// Readable | Writable
29    ///
30    ///
31    /// #### `name`
32    ///  The name of the action. This is mostly meaningful for identifying
33    /// the action once it has been added to a #GSimpleActionGroup.
34    ///
35    /// Readable | Writable | Construct Only
36    ///
37    ///
38    /// #### `parameter-type`
39    ///  The type of the parameter that must be given when activating the
40    /// action.
41    ///
42    /// Readable | Writable | Construct Only
43    ///
44    ///
45    /// #### `state`
46    ///  The state of the action, or [`None`] if the action is stateless.
47    ///
48    /// Readable | Writable | Construct
49    ///
50    ///
51    /// #### `state-type`
52    ///  The #GVariantType of the state that the action has, or [`None`] if the
53    /// action is stateless.
54    ///
55    /// Readable
56    /// <details><summary><h4>Action</h4></summary>
57    ///
58    ///
59    /// #### `enabled`
60    ///  If @action is currently enabled.
61    ///
62    /// If the action is disabled then calls to [`ActionExt::activate()`][crate::prelude::ActionExt::activate()] and
63    /// [`ActionExt::change_state()`][crate::prelude::ActionExt::change_state()] have no effect.
64    ///
65    /// Readable
66    ///
67    ///
68    /// #### `name`
69    ///  The name of the action.  This is mostly meaningful for identifying
70    /// the action once it has been added to a [type@Gio.ActionGroup]. It is immutable.
71    ///
72    /// Readable
73    ///
74    ///
75    /// #### `parameter-type`
76    ///  The type of the parameter that must be given when activating the
77    /// action. This is immutable, and may be `NULL` if no parameter is needed when
78    /// activating the action.
79    ///
80    /// Readable
81    ///
82    ///
83    /// #### `state`
84    ///  The state of the action, or `NULL` if the action is stateless.
85    ///
86    /// Readable
87    ///
88    ///
89    /// #### `state-type`
90    ///  The [type@GLib.VariantType] of the state that the action has, or `NULL` if the
91    /// action is stateless. This is immutable.
92    ///
93    /// Readable
94    /// </details>
95    ///
96    /// ## Signals
97    ///
98    ///
99    /// #### `activate`
100    ///  Indicates that the action was just activated.
101    ///
102    /// @parameter will always be of the expected type, i.e. the parameter type
103    /// specified when the action was created. If an incorrect type is given when
104    /// activating the action, this signal is not emitted.
105    ///
106    /// Since GLib 2.40, if no handler is connected to this signal then the
107    /// default behaviour for boolean-stated actions with a [`None`] parameter
108    /// type is to toggle them via the #GSimpleAction::change-state signal.
109    /// For stateful actions where the state type is equal to the parameter
110    /// type, the default is to forward them directly to
111    /// #GSimpleAction::change-state.  This should allow almost all users
112    /// of #GSimpleAction to connect only one handler or the other.
113    ///
114    ///
115    ///
116    ///
117    /// #### `change-state`
118    ///  = 10)
119    ///     g_simple_action_set_state (action, value);
120    /// }
121    /// ]|
122    ///
123    /// The handler need not set the state to the requested value.
124    /// It could set it to any value at all, or take some other action.
125    ///
126    ///
127    ///
128    /// # Implements
129    ///
130    /// [`trait@glib::ObjectExt`], [`ActionExt`][trait@crate::prelude::ActionExt]
131    #[doc(alias = "GSimpleAction")]
132    pub struct SimpleAction(Object<ffi::GSimpleAction>) @implements Action;
133
134    match fn {
135        type_ => || ffi::g_simple_action_get_type(),
136    }
137}
138
139impl SimpleAction {
140    /// Creates a new action.
141    ///
142    /// The created action is stateless. See g_simple_action_new_stateful() to create
143    /// an action that has state.
144    /// ## `name`
145    /// the name of the action
146    /// ## `parameter_type`
147    /// the type of parameter that will be passed to
148    ///   handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
149    ///
150    /// # Returns
151    ///
152    /// a new #GSimpleAction
153    #[doc(alias = "g_simple_action_new")]
154    pub fn new(name: &str, parameter_type: Option<&glib::VariantTy>) -> SimpleAction {
155        unsafe {
156            from_glib_full(ffi::g_simple_action_new(
157                name.to_glib_none().0,
158                parameter_type.to_glib_none().0,
159            ))
160        }
161    }
162
163    /// Creates a new stateful action.
164    ///
165    /// All future state values must have the same #GVariantType as the initial
166    /// @state.
167    ///
168    /// If the @state #GVariant is floating, it is consumed.
169    /// ## `name`
170    /// the name of the action
171    /// ## `parameter_type`
172    /// the type of the parameter that will be passed to
173    ///   handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
174    /// ## `state`
175    /// the initial state of the action
176    ///
177    /// # Returns
178    ///
179    /// a new #GSimpleAction
180    #[doc(alias = "g_simple_action_new_stateful")]
181    pub fn new_stateful(
182        name: &str,
183        parameter_type: Option<&glib::VariantTy>,
184        state: &glib::Variant,
185    ) -> SimpleAction {
186        unsafe {
187            from_glib_full(ffi::g_simple_action_new_stateful(
188                name.to_glib_none().0,
189                parameter_type.to_glib_none().0,
190                state.to_glib_none().0,
191            ))
192        }
193    }
194
195    /// Sets the action as enabled or not.
196    ///
197    /// An action must be enabled in order to be activated or in order to
198    /// have its state changed from outside callers.
199    ///
200    /// This should only be called by the implementor of the action.  Users
201    /// of the action should not attempt to modify its enabled flag.
202    /// ## `enabled`
203    /// whether the action is enabled
204    #[doc(alias = "g_simple_action_set_enabled")]
205    #[doc(alias = "enabled")]
206    pub fn set_enabled(&self, enabled: bool) {
207        unsafe {
208            ffi::g_simple_action_set_enabled(self.to_glib_none().0, enabled.into_glib());
209        }
210    }
211
212    /// Sets the state of the action.
213    ///
214    /// This directly updates the 'state' property to the given value.
215    ///
216    /// This should only be called by the implementor of the action.  Users
217    /// of the action should not attempt to directly modify the 'state'
218    /// property.  Instead, they should call g_action_change_state() to
219    /// request the change.
220    ///
221    /// If the @value GVariant is floating, it is consumed.
222    /// ## `value`
223    /// the new #GVariant for the state
224    #[doc(alias = "g_simple_action_set_state")]
225    #[doc(alias = "state")]
226    pub fn set_state(&self, value: &glib::Variant) {
227        unsafe {
228            ffi::g_simple_action_set_state(self.to_glib_none().0, value.to_glib_none().0);
229        }
230    }
231
232    /// Sets the state hint for the action.
233    ///
234    /// See g_action_get_state_hint() for more information about
235    /// action state hints.
236    /// ## `state_hint`
237    /// a #GVariant representing the state hint
238    #[doc(alias = "g_simple_action_set_state_hint")]
239    pub fn set_state_hint(&self, state_hint: Option<&glib::Variant>) {
240        unsafe {
241            ffi::g_simple_action_set_state_hint(self.to_glib_none().0, state_hint.to_glib_none().0);
242        }
243    }
244
245    /// Indicates that the action was just activated.
246    ///
247    /// @parameter will always be of the expected type, i.e. the parameter type
248    /// specified when the action was created. If an incorrect type is given when
249    /// activating the action, this signal is not emitted.
250    ///
251    /// Since GLib 2.40, if no handler is connected to this signal then the
252    /// default behaviour for boolean-stated actions with a [`None`] parameter
253    /// type is to toggle them via the #GSimpleAction::change-state signal.
254    /// For stateful actions where the state type is equal to the parameter
255    /// type, the default is to forward them directly to
256    /// #GSimpleAction::change-state.  This should allow almost all users
257    /// of #GSimpleAction to connect only one handler or the other.
258    /// ## `parameter`
259    /// the parameter to the activation, or [`None`] if it has
260    ///   no parameter
261    #[doc(alias = "activate")]
262    pub fn connect_activate<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
263        &self,
264        f: F,
265    ) -> SignalHandlerId {
266        unsafe extern "C" fn activate_trampoline<
267            F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
268        >(
269            this: *mut ffi::GSimpleAction,
270            parameter: *mut glib::ffi::GVariant,
271            f: glib::ffi::gpointer,
272        ) {
273            unsafe {
274                let f: &F = &*(f as *const F);
275                f(
276                    &from_glib_borrow(this),
277                    Option::<glib::Variant>::from_glib_borrow(parameter)
278                        .as_ref()
279                        .as_ref(),
280                )
281            }
282        }
283        unsafe {
284            let f: Box_<F> = Box_::new(f);
285            connect_raw(
286                self.as_ptr() as *mut _,
287                c"activate".as_ptr(),
288                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
289                    activate_trampoline::<F> as *const (),
290                )),
291                Box_::into_raw(f),
292            )
293        }
294    }
295
296    /// = 10)
297    ///     g_simple_action_set_state (action, value);
298    /// }
299    /// ]|
300    ///
301    /// The handler need not set the state to the requested value.
302    /// It could set it to any value at all, or take some other action.
303    /// ## `value`
304    /// the requested value for the state
305    #[doc(alias = "change-state")]
306    pub fn connect_change_state<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
307        &self,
308        f: F,
309    ) -> SignalHandlerId {
310        unsafe extern "C" fn change_state_trampoline<
311            F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
312        >(
313            this: *mut ffi::GSimpleAction,
314            value: *mut glib::ffi::GVariant,
315            f: glib::ffi::gpointer,
316        ) {
317            unsafe {
318                let f: &F = &*(f as *const F);
319                f(
320                    &from_glib_borrow(this),
321                    Option::<glib::Variant>::from_glib_borrow(value)
322                        .as_ref()
323                        .as_ref(),
324                )
325            }
326        }
327        unsafe {
328            let f: Box_<F> = Box_::new(f);
329            connect_raw(
330                self.as_ptr() as *mut _,
331                c"change-state".as_ptr(),
332                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
333                    change_state_trampoline::<F> as *const (),
334                )),
335                Box_::into_raw(f),
336            )
337        }
338    }
339}