Skip to content

Gio.SimpleAction

class — extends GObject.Object, 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

new

@classmethod
def new(cls, name: str, parameter_type: GLib.VariantType | None = ...) -> SimpleAction

Creates a new action.

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

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

new_stateful

@classmethod
def new_stateful(cls, name: str, parameter_type: GLib.VariantType | None, state: GLib.Variant) -> SimpleAction

Creates a new stateful action.

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

If the state GLib.Variant is floating, it is consumed.

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

set_enabled

def set_enabled(self, 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.

Parameters:

  • enabled — whether the action is enabled

set_state

def set_state(self, value: GLib.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 Action.change_state to request the change.

If the value GVariant is floating, it is consumed.

Parameters:

set_state_hint

def set_state_hint(self, state_hint: GLib.Variant | None = ...) -> None

Sets the state hint for the action.

See Action.get_state_hint for more information about action state hints.

Parameters:

Properties

enabled

enabled: bool  # read/write

If action is currently enabled.

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

name

name: str  # read/write

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

parameter_type

parameter_type: GLib.VariantType  # read/write

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

state

state: GLib.Variant  # read/write

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

state_type

state_type: GLib.VariantType  # read-only

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

Signals

activate

def on_activate(self, parameter: GLib.Variant | 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.

change-state

def on_change_state(self, value: GLib.Variant | 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 SimpleAction.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 SimpleAction.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 SimpleAction.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.