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::{ffi, Action};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
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 | Writeable
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 | Writeable | 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 | Writeable | Construct Only
43    ///
44    ///
45    /// #### `state`
46    ///  The state of the action, or [`None`] if the action is stateless.
47    ///
48    /// Readable | Writeable | 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    ///  Indicates that the action just received a request to change its
119    /// state.
120    ///
121    /// @value will always be of the correct state type, i.e. the type of the
122    /// initial state passed to g_simple_action_new_stateful(). If an incorrect
123    /// type is given when requesting to change the state, this signal is not
124    /// emitted.
125    ///
126    /// If no handler is connected to this signal then the default
127    /// behaviour is to call g_simple_action_set_state() to set the state
128    /// to the requested value. If you connect a signal handler then no
129    /// default action is taken. If the state should change then you must
130    /// call g_simple_action_set_state() from the handler.
131    ///
132    /// An example of a 'change-state' handler:
133    ///
134    ///
135    /// **⚠️ The following code is in C ⚠️**
136    ///
137    /// ```C
138    /// static void
139    /// change_volume_state (GSimpleAction *action,
140    ///                      GVariant      *value,
141    ///                      gpointer       user_data)
142    /// {
143    ///   gint requested;
144    ///
145    ///   requested = g_variant_get_int32 (value);
146    ///
147    ///   // Volume only goes from 0 to 10
148    ///   if (0 <= requested && requested <= 10)
149    ///     g_simple_action_set_state (action, value);
150    /// }
151    /// ```
152    ///
153    /// The handler need not set the state to the requested value.
154    /// It could set it to any value at all, or take some other action.
155    ///
156    ///
157    ///
158    /// # Implements
159    ///
160    /// [`trait@glib::ObjectExt`], [`ActionExt`][trait@crate::prelude::ActionExt]
161    #[doc(alias = "GSimpleAction")]
162    pub struct SimpleAction(Object<ffi::GSimpleAction>) @implements Action;
163
164    match fn {
165        type_ => || ffi::g_simple_action_get_type(),
166    }
167}
168
169impl SimpleAction {
170    /// Creates a new action.
171    ///
172    /// The created action is stateless. See g_simple_action_new_stateful() to create
173    /// an action that has state.
174    /// ## `name`
175    /// the name of the action
176    /// ## `parameter_type`
177    /// the type of parameter that will be passed to
178    ///   handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
179    ///
180    /// # Returns
181    ///
182    /// a new #GSimpleAction
183    #[doc(alias = "g_simple_action_new")]
184    pub fn new(name: &str, parameter_type: Option<&glib::VariantTy>) -> SimpleAction {
185        unsafe {
186            from_glib_full(ffi::g_simple_action_new(
187                name.to_glib_none().0,
188                parameter_type.to_glib_none().0,
189            ))
190        }
191    }
192
193    /// Creates a new stateful action.
194    ///
195    /// All future state values must have the same #GVariantType as the initial
196    /// @state.
197    ///
198    /// If the @state #GVariant is floating, it is consumed.
199    /// ## `name`
200    /// the name of the action
201    /// ## `parameter_type`
202    /// the type of the parameter that will be passed to
203    ///   handlers for the #GSimpleAction::activate signal, or [`None`] for no parameter
204    /// ## `state`
205    /// the initial state of the action
206    ///
207    /// # Returns
208    ///
209    /// a new #GSimpleAction
210    #[doc(alias = "g_simple_action_new_stateful")]
211    pub fn new_stateful(
212        name: &str,
213        parameter_type: Option<&glib::VariantTy>,
214        state: &glib::Variant,
215    ) -> SimpleAction {
216        unsafe {
217            from_glib_full(ffi::g_simple_action_new_stateful(
218                name.to_glib_none().0,
219                parameter_type.to_glib_none().0,
220                state.to_glib_none().0,
221            ))
222        }
223    }
224
225    /// Sets the action as enabled or not.
226    ///
227    /// An action must be enabled in order to be activated or in order to
228    /// have its state changed from outside callers.
229    ///
230    /// This should only be called by the implementor of the action.  Users
231    /// of the action should not attempt to modify its enabled flag.
232    /// ## `enabled`
233    /// whether the action is enabled
234    #[doc(alias = "g_simple_action_set_enabled")]
235    #[doc(alias = "enabled")]
236    pub fn set_enabled(&self, enabled: bool) {
237        unsafe {
238            ffi::g_simple_action_set_enabled(self.to_glib_none().0, enabled.into_glib());
239        }
240    }
241
242    /// Sets the state of the action.
243    ///
244    /// This directly updates the 'state' property to the given value.
245    ///
246    /// This should only be called by the implementor of the action.  Users
247    /// of the action should not attempt to directly modify the 'state'
248    /// property.  Instead, they should call g_action_change_state() to
249    /// request the change.
250    ///
251    /// If the @value GVariant is floating, it is consumed.
252    /// ## `value`
253    /// the new #GVariant for the state
254    #[doc(alias = "g_simple_action_set_state")]
255    #[doc(alias = "state")]
256    pub fn set_state(&self, value: &glib::Variant) {
257        unsafe {
258            ffi::g_simple_action_set_state(self.to_glib_none().0, value.to_glib_none().0);
259        }
260    }
261
262    /// Sets the state hint for the action.
263    ///
264    /// See g_action_get_state_hint() for more information about
265    /// action state hints.
266    /// ## `state_hint`
267    /// a #GVariant representing the state hint
268    #[doc(alias = "g_simple_action_set_state_hint")]
269    pub fn set_state_hint(&self, state_hint: Option<&glib::Variant>) {
270        unsafe {
271            ffi::g_simple_action_set_state_hint(self.to_glib_none().0, state_hint.to_glib_none().0);
272        }
273    }
274
275    /// Indicates that the action was just activated.
276    ///
277    /// @parameter will always be of the expected type, i.e. the parameter type
278    /// specified when the action was created. If an incorrect type is given when
279    /// activating the action, this signal is not emitted.
280    ///
281    /// Since GLib 2.40, if no handler is connected to this signal then the
282    /// default behaviour for boolean-stated actions with a [`None`] parameter
283    /// type is to toggle them via the #GSimpleAction::change-state signal.
284    /// For stateful actions where the state type is equal to the parameter
285    /// type, the default is to forward them directly to
286    /// #GSimpleAction::change-state.  This should allow almost all users
287    /// of #GSimpleAction to connect only one handler or the other.
288    /// ## `parameter`
289    /// the parameter to the activation, or [`None`] if it has
290    ///   no parameter
291    #[doc(alias = "activate")]
292    pub fn connect_activate<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
293        &self,
294        f: F,
295    ) -> SignalHandlerId {
296        unsafe extern "C" fn activate_trampoline<
297            F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
298        >(
299            this: *mut ffi::GSimpleAction,
300            parameter: *mut glib::ffi::GVariant,
301            f: glib::ffi::gpointer,
302        ) {
303            let f: &F = &*(f as *const F);
304            f(
305                &from_glib_borrow(this),
306                Option::<glib::Variant>::from_glib_borrow(parameter)
307                    .as_ref()
308                    .as_ref(),
309            )
310        }
311        unsafe {
312            let f: Box_<F> = Box_::new(f);
313            connect_raw(
314                self.as_ptr() as *mut _,
315                b"activate\0".as_ptr() as *const _,
316                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
317                    activate_trampoline::<F> as *const (),
318                )),
319                Box_::into_raw(f),
320            )
321        }
322    }
323
324    /// Indicates that the action just received a request to change its
325    /// state.
326    ///
327    /// @value will always be of the correct state type, i.e. the type of the
328    /// initial state passed to g_simple_action_new_stateful(). If an incorrect
329    /// type is given when requesting to change the state, this signal is not
330    /// emitted.
331    ///
332    /// If no handler is connected to this signal then the default
333    /// behaviour is to call g_simple_action_set_state() to set the state
334    /// to the requested value. If you connect a signal handler then no
335    /// default action is taken. If the state should change then you must
336    /// call g_simple_action_set_state() from the handler.
337    ///
338    /// An example of a 'change-state' handler:
339    ///
340    ///
341    /// **⚠️ The following code is in C ⚠️**
342    ///
343    /// ```C
344    /// static void
345    /// change_volume_state (GSimpleAction *action,
346    ///                      GVariant      *value,
347    ///                      gpointer       user_data)
348    /// {
349    ///   gint requested;
350    ///
351    ///   requested = g_variant_get_int32 (value);
352    ///
353    ///   // Volume only goes from 0 to 10
354    ///   if (0 <= requested && requested <= 10)
355    ///     g_simple_action_set_state (action, value);
356    /// }
357    /// ```
358    ///
359    /// The handler need not set the state to the requested value.
360    /// It could set it to any value at all, or take some other action.
361    /// ## `value`
362    /// the requested value for the state
363    #[doc(alias = "change-state")]
364    pub fn connect_change_state<F: Fn(&Self, Option<&glib::Variant>) + 'static>(
365        &self,
366        f: F,
367    ) -> SignalHandlerId {
368        unsafe extern "C" fn change_state_trampoline<
369            F: Fn(&SimpleAction, Option<&glib::Variant>) + 'static,
370        >(
371            this: *mut ffi::GSimpleAction,
372            value: *mut glib::ffi::GVariant,
373            f: glib::ffi::gpointer,
374        ) {
375            let f: &F = &*(f as *const F);
376            f(
377                &from_glib_borrow(this),
378                Option::<glib::Variant>::from_glib_borrow(value)
379                    .as_ref()
380                    .as_ref(),
381            )
382        }
383        unsafe {
384            let f: Box_<F> = Box_::new(f);
385            connect_raw(
386                self.as_ptr() as *mut _,
387                b"change-state\0".as_ptr() as *const _,
388                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
389                    change_state_trampoline::<F> as *const (),
390                )),
391                Box_::into_raw(f),
392            )
393        }
394    }
395}