@@ -15,12 +15,14 @@ def notice(text, title="SHA2017", close_text="Close", width = 296, height = 128,
15
15
"""
16
16
prompt_boolean (text , title = title , true_text = close_text , false_text = None , width = width , height = height , font = font )
17
17
18
- def prompt_boolean (text , title = "SHA2017" , true_text = "Yes" , false_text = "No" , width = 296 , height = 128 , font = "Roboto_Regular12" ):
18
+ def prompt_boolean (text , title = "SHA2017" , true_text = "Yes" , false_text = "No" , width = 296 , height = 128 , font = "Roboto_Regular12" , cb = None ):
19
19
"""A simple one and two-options dialog
20
20
21
21
if 'false_text' is set to None only one button is displayed.
22
22
If both 'false_text' and 'true_text' are given a boolean is returned, press B for false, A for true.
23
23
24
+ Pass along a 'cb' callback to make the dialog async, which is needed to make input work when used from a callback
25
+
24
26
The caller is responsible for flushing the display after processing the response.
25
27
"""
26
28
global wait_for_interrupt , button_pushed
@@ -35,6 +37,32 @@ def prompt_boolean(text, title="SHA2017", true_text="Yes", false_text="No", widt
35
37
false_text = "B: " + false_text
36
38
true_text = "A: " + true_text
37
39
40
+ def done (result ):
41
+ window .destroy ()
42
+ if cb :
43
+ cb (result )
44
+ return result
45
+
46
+ def syncSuccess (evt ):
47
+ if evt :
48
+ # We'd like promises here, but for now this should do
49
+ global wait_for_interrupt , button_pushed
50
+ button_pushed = "A"
51
+ wait_for_interrupt = False
52
+ def syncCancel (evt ):
53
+ if evt :
54
+ # We'd like promises here, but for now this should do
55
+ global wait_for_interrupt , button_pushed
56
+ button_pushed = "B"
57
+ wait_for_interrupt = False
58
+
59
+ def asyncSuccess (evt ):
60
+ if evt :
61
+ done (True )
62
+ def asyncCancel (evt ):
63
+ if evt :
64
+ done (False )
65
+
38
66
label = ugfx .Label (5 , 30 , width - 10 , height - 80 , text = text , parent = window )
39
67
button_no = ugfx .Button (5 , height - 40 , width // 2 - 15 , 30 , false_text , parent = window ) if false_text else None
40
68
button_yes = ugfx .Button (width // 2 + 5 if true_text else 5 , height - 40 , width // 2 - 15 if false_text else width - 10 , 30 , true_text , parent = window )
@@ -46,28 +74,24 @@ def prompt_boolean(text, title="SHA2017", true_text="Yes", false_text="No", widt
46
74
ugfx .set_lut (ugfx .LUT_NORMAL )
47
75
ugfx .flush ()
48
76
49
- def done (value ):
50
- window .hide ()
51
- window .destroy ()
52
- button_yes .destroy ()
53
- if button_no : button_no .destroy ()
54
- label .destroy ()
55
- return value
77
+ if button_no : ugfx .input_attach (ugfx .BTN_B , asyncCancel if cb else syncCancel )
78
+ ugfx .input_attach (ugfx .BTN_A , asyncSuccess if cb else syncSuccess )
56
79
57
- if button_no : ugfx . input_attach ( ugfx . BTN_B , pressed_b )
58
- ugfx . input_attach ( ugfx . BTN_A , pressed_a )
59
-
60
- wait_for_interrupt = True
61
- while wait_for_interrupt :
62
- time .sleep (0.2 )
80
+ if cb :
81
+ return
82
+ else :
83
+ wait_for_interrupt = True
84
+ while wait_for_interrupt :
85
+ time .sleep (0.2 )
63
86
64
- if button_pushed == "B" : return done (False )
65
- if button_pushed == "A" : return done (True )
87
+ if button_pushed == "B" : return done (False )
88
+ return done (True )
66
89
67
- def prompt_text (description , init_text = "" , true_text = "OK" , false_text = "Back" , width = 300 , height = 200 , font = "Roboto_BlackItalic24" ):
90
+ def prompt_text (description , init_text = "" , true_text = "OK" , false_text = "Back" , width = 300 , height = 200 , font = "Roboto_BlackItalic24" , cb = None ):
68
91
"""Shows a dialog and keyboard that allows the user to input/change a string
69
92
70
- Returns None if user aborts with button B
93
+ Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not
94
+ possible to process events inside an event callback.
71
95
72
96
The caller is responsible for flushing the display after processing the response.
73
97
"""
@@ -86,13 +110,33 @@ def prompt_text(description, init_text = "", true_text="OK", false_text="Back",
86
110
ugfx .set_default_font ("Roboto_Regular12" )
87
111
button_height = 25
88
112
89
- def okay (evt ):
90
- # We'd like promises here, but for now this should do
91
- global wait_for_interrupt
92
- button_pushed = "A"
93
- wait_for_interrupt = False
94
-
95
- button_yes = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height , int (width * 1 / 5 )- 3 , button_height , true_text , parent = window , cb = okay )
113
+ def done (result ):
114
+ window .destroy ()
115
+ if cb :
116
+ cb (result )
117
+ return result
118
+
119
+ def syncSuccess (evt ):
120
+ if evt :
121
+ # We'd like promises here, but for now this should do
122
+ global wait_for_interrupt , button_pushed
123
+ button_pushed = "A"
124
+ wait_for_interrupt = False
125
+ def syncCancel (evt ):
126
+ if evt :
127
+ # We'd like promises here, but for now this should do
128
+ global wait_for_interrupt , button_pushed
129
+ button_pushed = "B"
130
+ wait_for_interrupt = False
131
+
132
+ def asyncSuccess (evt ):
133
+ if evt :
134
+ done (edit .text ())
135
+ def asyncCancel (evt ):
136
+ if evt :
137
+ done (None )
138
+
139
+ button_yes = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height , int (width * 1 / 5 )- 3 , button_height , true_text , parent = window , cb = asyncSuccess if cb else syncSuccess )
96
140
button_no = ugfx .Button (int (width * 4 / 5 ), height - kb_height - button_height - button_height , int (width / 5 )- 3 , button_height , false_text , parent = window ) if false_text else None
97
141
ugfx .set_default_font (font )
98
142
label = ugfx .Label (5 , 1 , int (width * 4 / 5 ), height - kb_height - 5 - edit_height - 5 , description , parent = window )
@@ -114,14 +158,14 @@ def toggle_focus(pressed):
114
158
elif focus == 1 or not button_no :
115
159
button_yes .set_focus ()
116
160
kb .enabled (0 )
117
- ugfx .input_attach (ugfx .BTN_A , pressed_a )
118
- ugfx .input_attach (ugfx .BTN_B , pressed_b )
161
+ ugfx .input_attach (ugfx .BTN_A , asyncSuccess if cb else syncSuccess )
162
+ ugfx .input_attach (ugfx .BTN_B , asyncCancel if cb else syncCancel )
119
163
focus = (2 if button_no else 0 )
120
164
else :
121
165
button_no .set_focus ()
122
166
kb .enabled (0 )
123
- ugfx .input_attach (ugfx .BTN_A , pressed_a )
124
- ugfx .input_attach (ugfx .BTN_B , pressed_b )
167
+ ugfx .input_attach (ugfx .BTN_A , asyncCancel if cb else syncCancel )
168
+ ugfx .input_attach (ugfx .BTN_B , asyncCancel if cb else syncCancel )
125
169
focus = 0
126
170
ugfx .flush ()
127
171
@@ -138,21 +182,14 @@ def toggle_focus(pressed):
138
182
ugfx .flush ()
139
183
140
184
wait_for_interrupt = True
141
- while wait_for_interrupt :
142
- time .sleep (0.2 )
185
+ if cb :
186
+ return
187
+ else :
188
+ while wait_for_interrupt :
189
+ time .sleep (0.2 )
143
190
144
- def done (value ):
145
- window .hide ()
146
- window .destroy ()
147
- button_yes .destroy ()
148
- if button_no : button_no .destroy ()
149
- label .destroy ()
150
- kb .destroy ()
151
- edit .destroy ()
152
- return value
153
-
154
- if (focus == 0 and no_button ) or button_pushed == "B" : return done (False )
155
- return done (edit .text ())
191
+ if (focus == 0 and no_button ) or button_pushed == "B" : return done (False )
192
+ return done (edit .text ())
156
193
157
194
def prompt_option (options , index = 0 , text = "Please select one of the following:" , title = None , select_text = "OK" , none_text = None ):
158
195
"""Shows a dialog prompting for one of multiple options
0 commit comments