Action
Object Hierarchy:
Description:
public interface Action : Object
`GAction` represents a single named action.
The main interface to an action is that it can be activated with [method@Gio.Action.activate]. This results in the 'activate' signal being emitted. An activation has a `GVariant` parameter (which may be `NULL`). The correct type for the parameter is determined by a static parameter type (which is given at construction time).
An action may optionally have a state, in which case the state may be set with [method@Gio.Action.change_state]. This call takes a Variant. The correct type for the state is determined by a static state type (which is given at construction time).
The state may have a hint associated with it, specifying its valid range.
`GAction` is merely the interface to the concept of an action, as described above. Various implementations of actions exist, including [ class@Gio.SimpleAction].
In all cases, the implementing class is responsible for storing the name of the action, the parameter type, the enabled state, the optional state type and the state and emitting the appropriate signals when these change. The implementor is responsible for filtering calls to [ method@Gio.Action.activate] and [method@Gio.Action.change_state] for type safety and for the state being enabled.
Probably the only useful thing to do with a `GAction` is to put it inside of a [class@Gio.SimpleActionGroup].
Example: Application with actions::
public class MyApplication : Application {
private MyApplication () {
Object (application_id: "org.example.application", flags: 0);
set_inactivity_timeout (10000);
add_actions ();
}
private void add_actions () {
SimpleAction simple_action = new SimpleAction ("simple-action", null);
simple_action.activate.connect (() => {
this.hold ();
print ("Simple action %s activated\n", simple_action.get_name ());
this.release ();
});
this.add_action (simple_action);
SimpleAction stateful_action = new SimpleAction.stateful ("toggle-action", null, new Variant.boolean (false));
stateful_action.activate.connect (() => {
print ("Action %s activated\n", stateful_action.get_name ());
this.hold ();
Variant state = stateful_action.get_state ();
bool b = state.get_boolean ();
stateful_action.set_state (new Variant.boolean (!b));
print (@"State change $b -> $(!b)\n");
this.release ();
});
this.add_action (stateful_action);
}
public override void activate () {
this.hold ();
print ("Activated\n");
this.release ();
}
private void describe_and_activate_action (string name) {
VariantType param_type = this.get_action_parameter_type (name);
Variant state = this.get_action_state (name);
bool enabled = this.get_action_enabled (name);
print (@"Action name: $name\n");
string? type = (param_type != null) ? param_type.dup_string () : "<none>";
print (@"Parameter type: $type\n");
print ("State type: %s\n", (state != null) ? state.get_type_string () : "<none>");
string state_val = (state != null) ? state.print (false) : "<none>";
print (@"State: $state_val\n");
print (@"Enabled: $enabled\n");
this.activate_action (name, null);
}
public static int main (string[] args) {
try {
MyApplication app = new MyApplication ();
if (args.length > 1) {
if (args[1] == "--simple-action") {
app.register (null);
app.describe_and_activate_action ("simple-action");
Process.exit (0);
} else if (args[1] == "--toggle-action") {
app.register (null);
app.describe_and_activate_action ("toggle-action");
Process.exit (0);
}
}
int status = app.run (args);
return status;
} catch (Error e) {
print ("Error: %s\n", e.message);
return 0;
}
}
}
valac --pkg gio-2.0 GLib.Application.1.vala