Module: Gio::ActionGroup

Defined in:
(unknown)

Overview

GAction::Group represents a group of actions. Actions can be used to expose functionality in a structured way, either from one part of a program to another, or to the outside world. Action groups are often used together with a GMenu::Model that provides additional representation data for displaying the actions to the user, e.g. in a menu.

The main way to interact with the actions in a GActionGroup is to activate them with g_action_group_activate_action(). Activating an action may require a #GVariant parameter. The required type of the parameter can be inquired with g_action_group_get_action_parameter_type(). Actions may be disabled, see g_action_group_get_action_enabled(). Activating a disabled action has no effect.

Actions may optionally have a state in the form of a #GVariant. The current state of an action can be inquired with g_action_group_get_action_state(). Activating a stateful action may change its state, but it is also possible to set the state by calling g_action_group_change_action_state().

As typical example, consider a text editing application which has an option to change the current font to ‘bold’. A good way to represent this would be a stateful action, with a boolean state. Activating the action would toggle the state.

Each action in the group has a unique name (which is a string). All method calls, except g_action_group_list_actions() take the name of an action as an argument.

The GAction::Group API is meant to be the ‘public’ API to the action group. The calls here are exactly the interaction that ‘external forces’ (eg: UI, incoming D-Bus messages, etc.) are supposed to have with actions. ‘Internal’ APIs (ie: ones meant only to be accessed by the action group implementation) are found on subclasses. This is why you will find - for example - g_action_group_get_action_enabled() but not an equivalent set() call.

Signals are emitted on the action group in response to state changes on individual actions.

Implementations of GAction::Group should provide implementations for the virtual functions g_action_group_list_actions() and g_action_group_query_action(). The other virtual functions should not be implemented - their “wrappers” are actually implemented with calls to g_action_group_query_action().

Instance Method Summary collapse

Instance Method Details

#action_added(action_name) ⇒ nil

Emits the GAction::Group::action-added signal on action_group.

This function should only be called by GAction::Group implementations.

Parameters:

  • action_name (String)

    the name of an action in the group

Returns:

  • (nil)

#action_enabled_changed(action_name, enabled) ⇒ nil

Emits the GAction::Group::action-enabled-changed signal on action_group.

This function should only be called by GAction::Group implementations.

Parameters:

  • action_name (String)

    the name of an action in the group

  • enabled (TrueClass)

    whether or not the action is now enabled

Returns:

  • (nil)

#action_removed(action_name) ⇒ nil

Emits the GAction::Group::action-removed signal on action_group.

This function should only be called by GAction::Group implementations.

Parameters:

  • action_name (String)

    the name of an action in the group

Returns:

  • (nil)

#action_state_changed(action_name, state) ⇒ nil

Emits the GAction::Group::action-state-changed signal on action_group.

This function should only be called by GAction::Group implementations.

Parameters:

  • action_name (String)

    the name of an action in the group

  • state (GLib::Variant)

    the new state of the named action

Returns:

  • (nil)

#activate_action(action_name, parameter) ⇒ nil

Activate the named action within action_group.

If the action is expecting a parameter, then the correct type of parameter must be given as parameter. If the action is expecting no parameters then parameter must be nil. See g_action_group_get_action_parameter_type().

Parameters:

  • action_name (String)

    the name of the action to activate

  • parameter (GLib::Variant)

    parameters to the activation

Returns:

  • (nil)

#change_action_state(action_name, value) ⇒ nil

Request for the state of the named action within action_group to be changed to value.

The action must be stateful and value must be of the correct type. See g_action_group_get_action_state_type().

This call merely requests a change. The action may refuse to change its state or may change its state to something other than value. See g_action_group_get_action_state_hint().

If the value GVariant is floating, it is consumed.

Parameters:

  • action_name (String)

    the name of the action to request the change on

  • value (GLib::Variant)

    the new state

Returns:

  • (nil)

#get_action_enabled(action_name) ⇒ TrueClass

Checks if the named action within action_group is currently enabled.

An action must be enabled in order to be activated or in order to have its state changed from outside callers.

Parameters:

  • action_name (String)

    the name of the action to query

Returns:

  • (TrueClass)

    whether or not the action is currently enabled

#get_action_parameter_type(action_name) ⇒ GLib::VariantType

Queries the type of the parameter that must be given when activating the named action within action_group.

When activating the action using g_action_group_activate_action(), the #GVariant given to that function must be of the type returned by this function.

In the case that this function returns nil, you must not give any #GVariant, but nil instead.

The parameter type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different parameter type.

Parameters:

  • action_name (String)

    the name of the action to query

Returns:

  • (GLib::VariantType)

    the parameter type

#get_action_state(action_name) ⇒ GLib::Variant

Queries the current state of the named action within action_group.

If the action is not stateful then nil will be returned. If the action is stateful then the type of the return value is the type given by g_action_group_get_action_state_type().

The return value (if non-nil) should be freed with g_variant_unref() when it is no longer required.

Parameters:

  • action_name (String)

    the name of the action to query

Returns:

  • (GLib::Variant)

    the current state of the action

#get_action_state_hint(action_name) ⇒ GLib::Variant

Requests a hint about the valid range of values for the state of the named action within action_group.

If nil is returned it either means that the action is not stateful or that there is no hint about the valid range of values for the state of the action.

If a #GVariant array is returned then each item in the array is a possible value for the state. If a #GVariant pair (ie: two-tuple) is returned then the tuple specifies the inclusive lower and upper bound of valid values for the state.

In any case, the information is merely a hint. It may be possible to have a state value outside of the hinted range and setting a value within the range may fail.

The return value (if non-nil) should be freed with g_variant_unref() when it is no longer required.

Parameters:

  • action_name (String)

    the name of the action to query

Returns:

  • (GLib::Variant)

    the state range hint

#get_action_state_type(action_name) ⇒ GLib::VariantType

Queries the type of the state of the named action within action_group.

If the action is stateful then this function returns the GVariant::Type of the state. All calls to g_action_group_change_action_state() must give a #GVariant of this type and g_action_group_get_action_state() will return a #GVariant of the same type.

If the action is not stateful then this function will return nil. In that case, g_action_group_get_action_state() will return nil and you must not call g_action_group_change_action_state().

The state type of a particular action will never change but it is possible for an action to be removed and for a new action to be added with the same name but a different state type.

Parameters:

  • action_name (String)

    the name of the action to query

Returns:

  • (GLib::VariantType)

    the state type, if the action is stateful

#has_action(action_name) ⇒ TrueClass

Checks if the named action exists within action_group.

Parameters:

  • action_name (String)

    the name of the action to check for

Returns:

  • (TrueClass)

    whether the named action exists

#list_actionsArray<String>

Lists the actions contained within action_group.

The caller is responsible for freeing the list with g_strfreev() when it is no longer required. actions in the group

Returns:

  • (Array<String>)

    an array of the names of the

#query_action(action_name, enabled, parameter_type, state_type, state_hint, state) ⇒ TrueClass

Queries all aspects of the named action within an action_group.

This function acquires the information available from g_action_group_has_action(), g_action_group_get_action_enabled(), g_action_group_get_action_parameter_type(), g_action_group_get_action_state_type(), g_action_group_get_action_state_hint() and g_action_group_get_action_state() with a single function call.

This provides two main benefits.

The first is the improvement in efficiency that comes with not having to perform repeated lookups of the action in order to discover different things about it. The second is that implementing GAction::Group can now be done by only overriding this one virtual function.

The interface provides a default implementation of this function that calls the individual functions, as required, to fetch the information. The interface also provides default implementations of those functions that call this function. All implementations, therefore, must override either this function or all of the others.

If the action exists, true is returned and any of the requested fields (as indicated by having a non-nil reference passed in) are filled. If the action doesn’t exist, false is returned and the fields may or may not have been modified.

Parameters:

  • action_name (String)

    the name of an action in the group

  • enabled (TrueClass)

    if the action is presently enabled

  • parameter_type (GLib::VariantType)

    the parameter type, or nil if none needed

  • state_type (GLib::VariantType)

    the state type, or nil if stateless

  • state_hint (GLib::Variant)

    the state hint, or nil if none

  • state (GLib::Variant)

    the current state, or nil if stateless

Returns:

  • (TrueClass)

    true if the action exists, else false