SimpleAction

class SimpleAction(**properties: Any)

Superclasses: Object

Implemented Interfaces: Action

A GSimpleAction is the obvious simple implementation of the Action interface. This is the easiest way to create an action for purposes of adding it to a SimpleActionGroup.

Constructors

class SimpleAction
classmethod new(name: str, parameter_type: VariantType | None = None) SimpleAction

Creates a new action.

The created action is stateless. See new_stateful() to create an action that has state.

Added in version 2.28.

Parameters:
  • name – the name of the action

  • parameter_type – the type of parameter that will be passed to handlers for the SimpleAction::activate signal, or None for no parameter

classmethod new_stateful(name: str, parameter_type: VariantType | None, state: Variant) SimpleAction

Creates a new stateful action.

All future state values must have the same VariantType as the initial state.

If the state Variant is floating, it is consumed.

Added in version 2.28.

Parameters:
  • name – the name of the action

  • parameter_type – the type of the parameter that will be passed to handlers for the SimpleAction::activate signal, or None for no parameter

  • state – the initial state of the action

Methods

class SimpleAction
set_enabled(enabled: bool) None

Sets the action as enabled or not.

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

This should only be called by the implementor of the action. Users of the action should not attempt to modify its enabled flag.

Added in version 2.28.

Parameters:

enabled – whether the action is enabled

set_state(value: Variant) None

Sets the state of the action.

This directly updates the ‘state’ property to the given value.

This should only be called by the implementor of the action. Users of the action should not attempt to directly modify the ‘state’ property. Instead, they should call change_state() to request the change.

If the value GVariant is floating, it is consumed.

Added in version 2.30.

Parameters:

value – the new Variant for the state

set_state_hint(state_hint: Variant | None = None) None

Sets the state hint for the action.

See get_state_hint() for more information about action state hints.

Added in version 2.44.

Parameters:

state_hint – a Variant representing the state hint

Properties

class SimpleAction
props.enabled: bool

If action is currently enabled.

If the action is disabled then calls to activate() and change_state() have no effect.

Added in version 2.28.

props.name: str

The name of the action. This is mostly meaningful for identifying the action once it has been added to a SimpleActionGroup.

Added in version 2.28.

props.parameter_type: VariantType

The type of the parameter that must be given when activating the action.

Added in version 2.28.

props.state: Variant

The state of the action, or None if the action is stateless.

Added in version 2.28.

props.state_type: VariantType

The VariantType of the state that the action has, or None if the action is stateless.

Added in version 2.28.

Signals

class SimpleAction.signals
activate(parameter: Variant | None = None) None

Indicates that the action was just activated.

parameter will always be of the expected type, i.e. the parameter type specified when the action was created. If an incorrect type is given when activating the action, this signal is not emitted.

Since GLib 2.40, if no handler is connected to this signal then the default behaviour for boolean-stated actions with a None parameter type is to toggle them via the SimpleAction::change-state signal. For stateful actions where the state type is equal to the parameter type, the default is to forward them directly to SimpleAction::change-state. This should allow almost all users of SimpleAction to connect only one handler or the other.

Added in version 2.28.

Parameters:

parameter – the parameter to the activation, or None if it has no parameter

change_state(value: Variant | None = None) None

Indicates that the action just received a request to change its state.

value will always be of the correct state type, i.e. the type of the initial state passed to new_stateful(). If an incorrect type is given when requesting to change the state, this signal is not emitted.

If no handler is connected to this signal then the default behaviour is to call set_state() to set the state to the requested value. If you connect a signal handler then no default action is taken. If the state should change then you must call set_state() from the handler.

An example of a ‘change-state’ handler:

static void
change_volume_state (GSimpleAction *action,
                     GVariant      *value,
                     gpointer       user_data)
{
  gint requested;

  requested = g_variant_get_int32 (value);

  // Volume only goes from 0 to 10
  if (0 <= requested && requested <= 10)
    g_simple_action_set_state (action, value);
}

The handler need not set the state to the requested value. It could set it to any value at all, or take some other action.

Added in version 2.30.

Parameters:

value – the requested value for the state