-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.py
47 lines (35 loc) · 1.26 KB
/
buttons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk
STATE_ON = "on"
STATE_OFF = "off"
class SwitchWidget:
def __init__(self, label, action):
self.pending = False
self.action = action
button = gtk.Switch()
button.connect("state-set", self.on_state_set)
self.container = gtk.VBox(False, 0)
self.container.pack_start(gtk.Label(label), False, True, 5)
self.container.pack_start(button, False, True, 5)
self.container.show_all()
def widget(self):
return self.container
def on_state_set(self, switch, state):
if not self.action or self.pending:
self.pending = False
switch.set_state(state)
return True
self.invoke_action(state, switch)
return True
def invoke_action(self, active, switch):
state = STATE_ON if active else STATE_OFF
self.action(state,
callback=lambda success: self.on_callback(switch, active, success))
def on_callback(self, switch, active, success):
if success:
switch.set_state(active)
else:
self.pending = True
switch.set_state(not active)
switch.set_active(not active)