4

I am trying to create a Gio.SimpleAction and connecting to its change-state signal, for taking an specific action when the state is changed, but I have failed to come up with a working code.

Here is an example of what I have tried (and expected to work), without success:

from gi.repository import Gio, GLib

call_count = 0
def _state_changed(action, state):
    action.set_state(state)
    global call_count
    call_count += 1
    print(state)

a = Gio.SimpleAction.new_stateful('foo', None, GLib.Variant.new_boolean(False))
a.connect('change-state', _state_changed)

print a.get_state().get_boolean()
a.set_state(GLib.Variant.new_boolean(True))
print a.get_state().get_boolean()
print call_count

The output of running this code is:

False
True
0

As we can see, the state is indeed changed, but no change-state was emitted!

Is there something wrong with this code sample? How do I properly detect a state change in a Gio.SimpleAction?

romaia
  • 413
  • 3
  • 6

1 Answers1

3

The set_state() method effects a state change. The change-state signal is only emitted in response to a state change request, from Gio.Action.change_state().

I believe the signal is mainly intended to allow subclasses of Gio.SimpleAction to inject code into the handling of state change requests.

Philip Withnall
  • 5,293
  • 14
  • 28
  • 1
    Yes. Thankyou. Changing a.set_state(GLib.Variant.new_boolean(True)) to a.change_state(...) fixes the problem. – romaia Feb 12 '18 at 00:19