gio/auto/
action_group.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;
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    /// `GActionGroup` represents a group of actions.
16    ///
17    /// Actions can be used to expose functionality in a structured way, either
18    /// from one part of a program to another, or to the outside world. Action
19    /// groups are often used together with a [type@Gio.MenuModel] that provides additional
20    /// representation data for displaying the actions to the user, e.g. in a menu.
21    ///
22    /// The main way to interact with the actions in a `GActionGroup` is to
23    /// activate them with [`ActionGroupExt::activate_action()`][crate::prelude::ActionGroupExt::activate_action()]. Activating an
24    /// action may require a [type@GLib.Variant] parameter. The required type of the
25    /// parameter can be inquired with [`ActionGroupExt::action_parameter_type()`][crate::prelude::ActionGroupExt::action_parameter_type()].
26    /// Actions may be disabled, see [`ActionGroupExt::is_action_enabled()`][crate::prelude::ActionGroupExt::is_action_enabled()].
27    /// Activating a disabled action has no effect.
28    ///
29    /// Actions may optionally have a state in the form of a [type@GLib.Variant]. The current
30    /// state of an action can be inquired with [`ActionGroupExt::action_state()`][crate::prelude::ActionGroupExt::action_state()].
31    /// Activating a stateful action may change its state, but it is also possible to
32    /// set the state by calling [`ActionGroupExt::change_action_state()`][crate::prelude::ActionGroupExt::change_action_state()].
33    ///
34    /// As typical example, consider a text editing application which has an
35    /// option to change the current font to ‘bold’. A good way to represent
36    /// this would be a stateful action, with a boolean state. Activating the
37    /// action would toggle the state.
38    ///
39    /// Each action in the group has a unique name (which is a string).  All
40    /// method calls, except [`ActionGroupExt::list_actions()`][crate::prelude::ActionGroupExt::list_actions()] take the name of
41    /// an action as an argument.
42    ///
43    /// The `GActionGroup` API is meant to be the ‘public’ API to the action
44    /// group. The calls here are exactly the interaction that ‘external
45    /// forces’ (eg: UI, incoming D-Bus messages, etc.) are supposed to have
46    /// with actions. ‘Internal’ APIs (ie: ones meant only to be accessed by
47    /// the action group implementation) are found on subclasses. This is
48    /// why you will find – for example – [`ActionGroupExt::is_action_enabled()`][crate::prelude::ActionGroupExt::is_action_enabled()]
49    /// but not an equivalent `set_action_enabled()` method.
50    ///
51    /// Signals are emitted on the action group in response to state changes
52    /// on individual actions.
53    ///
54    /// Implementations of `GActionGroup` should provide implementations for
55    /// the virtual functions [`ActionGroupExt::list_actions()`][crate::prelude::ActionGroupExt::list_actions()] and
56    /// `Gio::ActionGroup::query_action()`. The other virtual functions should
57    /// not be implemented — their ‘wrappers’ are actually implemented with
58    /// calls to `Gio::ActionGroup::query_action()`.
59    ///
60    /// ## Signals
61    ///
62    ///
63    /// #### `action-added`
64    ///  Signals that a new action was just added to the group.
65    ///
66    /// This signal is emitted after the action has been added
67    /// and is now visible.
68    ///
69    /// Detailed
70    ///
71    ///
72    /// #### `action-enabled-changed`
73    ///  Signals that the enabled status of the named action has changed.
74    ///
75    /// Detailed
76    ///
77    ///
78    /// #### `action-removed`
79    ///  Signals that an action is just about to be removed from the group.
80    ///
81    /// This signal is emitted before the action is removed, so the action
82    /// is still visible and can be queried from the signal handler.
83    ///
84    /// Detailed
85    ///
86    ///
87    /// #### `action-state-changed`
88    ///  Signals that the state of the named action has changed.
89    ///
90    /// Detailed
91    ///
92    /// # Implements
93    ///
94    /// [`ActionGroupExt`][trait@crate::prelude::ActionGroupExt]
95    #[doc(alias = "GActionGroup")]
96    pub struct ActionGroup(Interface<ffi::GActionGroup, ffi::GActionGroupInterface>);
97
98    match fn {
99        type_ => || ffi::g_action_group_get_type(),
100    }
101}
102
103impl ActionGroup {
104    pub const NONE: Option<&'static ActionGroup> = None;
105}
106
107/// Trait containing all [`struct@ActionGroup`] methods.
108///
109/// # Implementors
110///
111/// [`ActionGroup`][struct@crate::ActionGroup], [`Application`][struct@crate::Application], [`DBusActionGroup`][struct@crate::DBusActionGroup], [`RemoteActionGroup`][struct@crate::RemoteActionGroup], [`SimpleActionGroup`][struct@crate::SimpleActionGroup]
112pub trait ActionGroupExt: IsA<ActionGroup> + 'static {
113    /// Emits the [`action-added`][struct@crate::ActionGroup#action-added] signal on @self.
114    ///
115    /// This function should only be called by [type@Gio.ActionGroup] implementations.
116    /// ## `action_name`
117    /// the name of an action in the group
118    #[doc(alias = "g_action_group_action_added")]
119    fn action_added(&self, action_name: &str) {
120        unsafe {
121            ffi::g_action_group_action_added(
122                self.as_ref().to_glib_none().0,
123                action_name.to_glib_none().0,
124            );
125        }
126    }
127
128    /// Emits the [`action-enabled-changed`][struct@crate::ActionGroup#action-enabled-changed] signal on @self.
129    ///
130    /// This function should only be called by [type@Gio.ActionGroup] implementations.
131    /// ## `action_name`
132    /// the name of an action in the group
133    /// ## `enabled`
134    /// whether the action is now enabled
135    #[doc(alias = "g_action_group_action_enabled_changed")]
136    fn action_enabled_changed(&self, action_name: &str, enabled: bool) {
137        unsafe {
138            ffi::g_action_group_action_enabled_changed(
139                self.as_ref().to_glib_none().0,
140                action_name.to_glib_none().0,
141                enabled.into_glib(),
142            );
143        }
144    }
145
146    /// Emits the [`action-removed`][struct@crate::ActionGroup#action-removed] signal on @self.
147    ///
148    /// This function should only be called by [type@Gio.ActionGroup] implementations.
149    /// ## `action_name`
150    /// the name of an action in the group
151    #[doc(alias = "g_action_group_action_removed")]
152    fn action_removed(&self, action_name: &str) {
153        unsafe {
154            ffi::g_action_group_action_removed(
155                self.as_ref().to_glib_none().0,
156                action_name.to_glib_none().0,
157            );
158        }
159    }
160
161    /// Emits the [`action-state-changed`][struct@crate::ActionGroup#action-state-changed] signal on @self.
162    ///
163    /// This function should only be called by [type@Gio.ActionGroup] implementations.
164    /// ## `action_name`
165    /// the name of an action in the group
166    /// ## `state`
167    /// the new state of the named action
168    #[doc(alias = "g_action_group_action_state_changed")]
169    fn action_state_changed(&self, action_name: &str, state: &glib::Variant) {
170        unsafe {
171            ffi::g_action_group_action_state_changed(
172                self.as_ref().to_glib_none().0,
173                action_name.to_glib_none().0,
174                state.to_glib_none().0,
175            );
176        }
177    }
178
179    /// Activate the named action within @self.
180    ///
181    /// If the action is expecting a parameter, then the correct type of
182    /// parameter must be given as @parameter.  If the action is expecting no
183    /// parameters then @parameter must be `NULL`.  See
184    /// [`action_parameter_type()`][Self::action_parameter_type()].
185    ///
186    /// If the [type@Gio.ActionGroup] implementation supports asynchronous remote
187    /// activation over D-Bus, this call may return before the relevant
188    /// D-Bus traffic has been sent, or any replies have been received. In
189    /// order to block on such asynchronous activation calls,
190    /// [`DBusConnection::flush()`][crate::DBusConnection::flush()] should be called prior to the code, which
191    /// depends on the result of the action activation. Without flushing
192    /// the D-Bus connection, there is no guarantee that the action would
193    /// have been activated.
194    ///
195    /// The following code which runs in a remote app instance, shows an
196    /// example of a ‘quit’ action being activated on the primary app
197    /// instance over D-Bus. Here [`DBusConnection::flush()`][crate::DBusConnection::flush()] is called
198    /// before `exit()`. Without `g_dbus_connection_flush()`, the ‘quit’ action
199    /// may fail to be activated on the primary instance.
200    ///
201    /// **⚠️ The following code is in c ⚠️**
202    ///
203    /// ```c
204    /// // call ‘quit’ action on primary instance
205    /// g_action_group_activate_action (G_ACTION_GROUP (app), "quit", NULL);
206    ///
207    /// // make sure the action is activated now
208    /// g_dbus_connection_flush (…);
209    ///
210    /// g_debug ("Application has been terminated. Exiting.");
211    ///
212    /// exit (0);
213    /// ```
214    /// ## `action_name`
215    /// the name of the action to activate
216    /// ## `parameter`
217    /// parameters to the activation
218    #[doc(alias = "g_action_group_activate_action")]
219    fn activate_action(&self, action_name: &str, parameter: Option<&glib::Variant>) {
220        unsafe {
221            ffi::g_action_group_activate_action(
222                self.as_ref().to_glib_none().0,
223                action_name.to_glib_none().0,
224                parameter.to_glib_none().0,
225            );
226        }
227    }
228
229    /// Request for the state of the named action within @self to be
230    /// changed to @value.
231    ///
232    /// The action must be stateful and @value must be of the correct type.
233    /// See [`action_state_type()`][Self::action_state_type()].
234    ///
235    /// This call merely requests a change.  The action may refuse to change
236    /// its state or may change its state to something other than @value.
237    /// See [`action_state_hint()`][Self::action_state_hint()].
238    ///
239    /// If the @value GVariant is floating, it is consumed.
240    /// ## `action_name`
241    /// the name of the action to request the change on
242    /// ## `value`
243    /// the new state
244    #[doc(alias = "g_action_group_change_action_state")]
245    fn change_action_state(&self, action_name: &str, value: &glib::Variant) {
246        unsafe {
247            ffi::g_action_group_change_action_state(
248                self.as_ref().to_glib_none().0,
249                action_name.to_glib_none().0,
250                value.to_glib_none().0,
251            );
252        }
253    }
254
255    /// Checks if the named action within @self is currently enabled.
256    ///
257    /// An action must be enabled in order to be activated or in order to
258    /// have its state changed from outside callers.
259    /// ## `action_name`
260    /// the name of the action to query
261    ///
262    /// # Returns
263    ///
264    /// whether the action is currently enabled
265    #[doc(alias = "g_action_group_get_action_enabled")]
266    #[doc(alias = "get_action_enabled")]
267    fn is_action_enabled(&self, action_name: &str) -> bool {
268        unsafe {
269            from_glib(ffi::g_action_group_get_action_enabled(
270                self.as_ref().to_glib_none().0,
271                action_name.to_glib_none().0,
272            ))
273        }
274    }
275
276    /// Queries the type of the parameter that must be given when activating
277    /// the named action within @self.
278    ///
279    /// When activating the action using [`activate_action()`][Self::activate_action()],
280    /// the [type@GLib.Variant] given to that function must be of the type returned
281    /// by this function.
282    ///
283    /// In the case that this function returns `NULL`, you must not give any
284    /// [type@GLib.Variant], but `NULL` instead.
285    ///
286    /// The parameter type of a particular action will never change but it is
287    /// possible for an action to be removed and for a new action to be added
288    /// with the same name but a different parameter type.
289    /// ## `action_name`
290    /// the name of the action to query
291    ///
292    /// # Returns
293    ///
294    /// the parameter type
295    #[doc(alias = "g_action_group_get_action_parameter_type")]
296    #[doc(alias = "get_action_parameter_type")]
297    fn action_parameter_type(&self, action_name: &str) -> Option<glib::VariantType> {
298        unsafe {
299            from_glib_none(ffi::g_action_group_get_action_parameter_type(
300                self.as_ref().to_glib_none().0,
301                action_name.to_glib_none().0,
302            ))
303        }
304    }
305
306    /// Queries the current state of the named action within @self.
307    ///
308    /// If the action is not stateful then `NULL` will be returned.  If the
309    /// action is stateful then the type of the return value is the type
310    /// given by [`action_state_type()`][Self::action_state_type()].
311    ///
312    /// The return value (if non-`NULL`) should be freed with
313    /// `GLib::Variant::unref()` when it is no longer required.
314    /// ## `action_name`
315    /// the name of the action to query
316    ///
317    /// # Returns
318    ///
319    /// the current state of the action
320    #[doc(alias = "g_action_group_get_action_state")]
321    #[doc(alias = "get_action_state")]
322    fn action_state(&self, action_name: &str) -> Option<glib::Variant> {
323        unsafe {
324            from_glib_full(ffi::g_action_group_get_action_state(
325                self.as_ref().to_glib_none().0,
326                action_name.to_glib_none().0,
327            ))
328        }
329    }
330
331    /// Requests a hint about the valid range of values for the state of the
332    /// named action within @self.
333    ///
334    /// If `NULL` is returned it either means that the action is not stateful
335    /// or that there is no hint about the valid range of values for the
336    /// state of the action.
337    ///
338    /// If a [type@GLib.Variant] array is returned then each item in the array is a
339    /// possible value for the state.  If a [type@GLib.Variant] pair (ie: two-tuple) is
340    /// returned then the tuple specifies the inclusive lower and upper bound
341    /// of valid values for the state.
342    ///
343    /// In any case, the information is merely a hint.  It may be possible to
344    /// have a state value outside of the hinted range and setting a value
345    /// within the range may fail.
346    ///
347    /// The return value (if non-`NULL`) should be freed with
348    /// `GLib::Variant::unref()` when it is no longer required.
349    /// ## `action_name`
350    /// the name of the action to query
351    ///
352    /// # Returns
353    ///
354    /// the state range hint
355    #[doc(alias = "g_action_group_get_action_state_hint")]
356    #[doc(alias = "get_action_state_hint")]
357    fn action_state_hint(&self, action_name: &str) -> Option<glib::Variant> {
358        unsafe {
359            from_glib_full(ffi::g_action_group_get_action_state_hint(
360                self.as_ref().to_glib_none().0,
361                action_name.to_glib_none().0,
362            ))
363        }
364    }
365
366    /// Queries the type of the state of the named action within
367    /// @self.
368    ///
369    /// If the action is stateful then this function returns the
370    /// [type@GLib.VariantType] of the state.  All calls to
371    /// [`change_action_state()`][Self::change_action_state()] must give a [type@GLib.Variant] of this
372    /// type and [`action_state()`][Self::action_state()] will return a [type@GLib.Variant]
373    /// of the same type.
374    ///
375    /// If the action is not stateful then this function will return `NULL`.
376    /// In that case, [`action_state()`][Self::action_state()] will return `NULL`
377    /// and you must not call [`change_action_state()`][Self::change_action_state()].
378    ///
379    /// The state type of a particular action will never change but it is
380    /// possible for an action to be removed and for a new action to be added
381    /// with the same name but a different state type.
382    /// ## `action_name`
383    /// the name of the action to query
384    ///
385    /// # Returns
386    ///
387    /// the state type, if the action is stateful
388    #[doc(alias = "g_action_group_get_action_state_type")]
389    #[doc(alias = "get_action_state_type")]
390    fn action_state_type(&self, action_name: &str) -> Option<glib::VariantType> {
391        unsafe {
392            from_glib_none(ffi::g_action_group_get_action_state_type(
393                self.as_ref().to_glib_none().0,
394                action_name.to_glib_none().0,
395            ))
396        }
397    }
398
399    /// Checks if the named action exists within @self.
400    /// ## `action_name`
401    /// the name of the action to check for
402    ///
403    /// # Returns
404    ///
405    /// whether the named action exists
406    #[doc(alias = "g_action_group_has_action")]
407    fn has_action(&self, action_name: &str) -> bool {
408        unsafe {
409            from_glib(ffi::g_action_group_has_action(
410                self.as_ref().to_glib_none().0,
411                action_name.to_glib_none().0,
412            ))
413        }
414    }
415
416    /// Lists the actions contained within @self.
417    ///
418    /// The caller is responsible for freeing the list with `strfreev()` when
419    /// it is no longer required.
420    ///
421    /// # Returns
422    ///
423    /// a `NULL`-terminated array
424    ///   of the names of the actions in the group
425    #[doc(alias = "g_action_group_list_actions")]
426    fn list_actions(&self) -> Vec<glib::GString> {
427        unsafe {
428            FromGlibPtrContainer::from_glib_full(ffi::g_action_group_list_actions(
429                self.as_ref().to_glib_none().0,
430            ))
431        }
432    }
433
434    /// Signals that a new action was just added to the group.
435    ///
436    /// This signal is emitted after the action has been added
437    /// and is now visible.
438    /// ## `action_name`
439    /// the name of the action in @action_group
440    #[doc(alias = "action-added")]
441    fn connect_action_added<F: Fn(&Self, &str) + 'static>(
442        &self,
443        detail: Option<&str>,
444        f: F,
445    ) -> SignalHandlerId {
446        unsafe extern "C" fn action_added_trampoline<
447            P: IsA<ActionGroup>,
448            F: Fn(&P, &str) + 'static,
449        >(
450            this: *mut ffi::GActionGroup,
451            action_name: *mut std::ffi::c_char,
452            f: glib::ffi::gpointer,
453        ) {
454            let f: &F = &*(f as *const F);
455            f(
456                ActionGroup::from_glib_borrow(this).unsafe_cast_ref(),
457                &glib::GString::from_glib_borrow(action_name),
458            )
459        }
460        unsafe {
461            let f: Box_<F> = Box_::new(f);
462            let detailed_signal_name = detail.map(|name| format!("action-added::{name}\0"));
463            let signal_name: &[u8] = detailed_signal_name
464                .as_ref()
465                .map_or(c"action-added".to_bytes(), |n| n.as_bytes());
466            connect_raw(
467                self.as_ptr() as *mut _,
468                signal_name.as_ptr() as *const _,
469                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
470                    action_added_trampoline::<Self, F> as *const (),
471                )),
472                Box_::into_raw(f),
473            )
474        }
475    }
476
477    /// Signals that the enabled status of the named action has changed.
478    /// ## `action_name`
479    /// the name of the action in @action_group
480    /// ## `enabled`
481    /// whether the action is enabled
482    #[doc(alias = "action-enabled-changed")]
483    fn connect_action_enabled_changed<F: Fn(&Self, &str, bool) + 'static>(
484        &self,
485        detail: Option<&str>,
486        f: F,
487    ) -> SignalHandlerId {
488        unsafe extern "C" fn action_enabled_changed_trampoline<
489            P: IsA<ActionGroup>,
490            F: Fn(&P, &str, bool) + 'static,
491        >(
492            this: *mut ffi::GActionGroup,
493            action_name: *mut std::ffi::c_char,
494            enabled: glib::ffi::gboolean,
495            f: glib::ffi::gpointer,
496        ) {
497            let f: &F = &*(f as *const F);
498            f(
499                ActionGroup::from_glib_borrow(this).unsafe_cast_ref(),
500                &glib::GString::from_glib_borrow(action_name),
501                from_glib(enabled),
502            )
503        }
504        unsafe {
505            let f: Box_<F> = Box_::new(f);
506            let detailed_signal_name =
507                detail.map(|name| format!("action-enabled-changed::{name}\0"));
508            let signal_name: &[u8] = detailed_signal_name
509                .as_ref()
510                .map_or(c"action-enabled-changed".to_bytes(), |n| n.as_bytes());
511            connect_raw(
512                self.as_ptr() as *mut _,
513                signal_name.as_ptr() as *const _,
514                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
515                    action_enabled_changed_trampoline::<Self, F> as *const (),
516                )),
517                Box_::into_raw(f),
518            )
519        }
520    }
521
522    /// Signals that an action is just about to be removed from the group.
523    ///
524    /// This signal is emitted before the action is removed, so the action
525    /// is still visible and can be queried from the signal handler.
526    /// ## `action_name`
527    /// the name of the action in @action_group
528    #[doc(alias = "action-removed")]
529    fn connect_action_removed<F: Fn(&Self, &str) + 'static>(
530        &self,
531        detail: Option<&str>,
532        f: F,
533    ) -> SignalHandlerId {
534        unsafe extern "C" fn action_removed_trampoline<
535            P: IsA<ActionGroup>,
536            F: Fn(&P, &str) + 'static,
537        >(
538            this: *mut ffi::GActionGroup,
539            action_name: *mut std::ffi::c_char,
540            f: glib::ffi::gpointer,
541        ) {
542            let f: &F = &*(f as *const F);
543            f(
544                ActionGroup::from_glib_borrow(this).unsafe_cast_ref(),
545                &glib::GString::from_glib_borrow(action_name),
546            )
547        }
548        unsafe {
549            let f: Box_<F> = Box_::new(f);
550            let detailed_signal_name = detail.map(|name| format!("action-removed::{name}\0"));
551            let signal_name: &[u8] = detailed_signal_name
552                .as_ref()
553                .map_or(c"action-removed".to_bytes(), |n| n.as_bytes());
554            connect_raw(
555                self.as_ptr() as *mut _,
556                signal_name.as_ptr() as *const _,
557                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
558                    action_removed_trampoline::<Self, F> as *const (),
559                )),
560                Box_::into_raw(f),
561            )
562        }
563    }
564
565    /// Signals that the state of the named action has changed.
566    /// ## `action_name`
567    /// the name of the action in @action_group
568    /// ## `value`
569    /// the new value of the state
570    #[doc(alias = "action-state-changed")]
571    fn connect_action_state_changed<F: Fn(&Self, &str, &glib::Variant) + 'static>(
572        &self,
573        detail: Option<&str>,
574        f: F,
575    ) -> SignalHandlerId {
576        unsafe extern "C" fn action_state_changed_trampoline<
577            P: IsA<ActionGroup>,
578            F: Fn(&P, &str, &glib::Variant) + 'static,
579        >(
580            this: *mut ffi::GActionGroup,
581            action_name: *mut std::ffi::c_char,
582            value: *mut glib::ffi::GVariant,
583            f: glib::ffi::gpointer,
584        ) {
585            let f: &F = &*(f as *const F);
586            f(
587                ActionGroup::from_glib_borrow(this).unsafe_cast_ref(),
588                &glib::GString::from_glib_borrow(action_name),
589                &from_glib_borrow(value),
590            )
591        }
592        unsafe {
593            let f: Box_<F> = Box_::new(f);
594            let detailed_signal_name = detail.map(|name| format!("action-state-changed::{name}\0"));
595            let signal_name: &[u8] = detailed_signal_name
596                .as_ref()
597                .map_or(c"action-state-changed".to_bytes(), |n| n.as_bytes());
598            connect_raw(
599                self.as_ptr() as *mut _,
600                signal_name.as_ptr() as *const _,
601                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
602                    action_state_changed_trampoline::<Self, F> as *const (),
603                )),
604                Box_::into_raw(f),
605            )
606        }
607    }
608}
609
610impl<O: IsA<ActionGroup>> ActionGroupExt for O {}