Skip to content

Commit d972db1

Browse files
committed
Dialogs: async API for text prompt
So you can use the keyboard from a callback, too :)
1 parent 0faba7f commit d972db1

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

esp32/modules/dialogs.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ def done(value):
6464
if button_pushed == "B": return done(False)
6565
if button_pushed == "A": return done(True)
6666

67-
def prompt_text(description, init_text = "", true_text="OK", false_text="Back", width = 300, height = 200, font="Roboto_BlackItalic24"):
67+
def prompt_text(description, init_text = "", true_text="OK", false_text="Back", width = 300, height = 200, font="Roboto_BlackItalic24", cb=None):
6868
"""Shows a dialog and keyboard that allows the user to input/change a string
6969
70-
Returns None if user aborts with button B
70+
Calls the 'cb' callback or return None if user aborts with button B. Using a callback is highly recommended as it's not
71+
possible to process events inside an event callback.
7172
7273
The caller is responsible for flushing the display after processing the response.
7374
"""
@@ -86,13 +87,33 @@ def prompt_text(description, init_text = "", true_text="OK", false_text="Back",
8687
ugfx.set_default_font("Roboto_Regular12")
8788
button_height = 25
8889

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)
90+
def done(result):
91+
window.destroy()
92+
if cb:
93+
cb(result)
94+
return result
95+
96+
def syncSuccess(evt):
97+
if evt:
98+
# We'd like promises here, but for now this should do
99+
global wait_for_interrupt
100+
button_pushed = "A"
101+
wait_for_interrupt = False
102+
def syncCancel(evt):
103+
if evt:
104+
# We'd like promises here, but for now this should do
105+
global wait_for_interrupt
106+
button_pushed = "B"
107+
wait_for_interrupt = False
108+
109+
def asyncSuccess(evt):
110+
if evt:
111+
done(edit.text())
112+
def asyncCancel(evt):
113+
if evt:
114+
done(False)
115+
116+
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)
96117
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
97118
ugfx.set_default_font(font)
98119
label = ugfx.Label(5, 1, int(width*4/5), height-kb_height-5-edit_height-5, description, parent=window)
@@ -114,14 +135,14 @@ def toggle_focus(pressed):
114135
elif focus == 1 or not button_no:
115136
button_yes.set_focus()
116137
kb.enabled(0)
117-
ugfx.input_attach(ugfx.BTN_A, pressed_a)
118-
ugfx.input_attach(ugfx.BTN_B, pressed_b)
138+
ugfx.input_attach(ugfx.BTN_A, asyncSuccess if cb else syncSuccess)
139+
ugfx.input_attach(ugfx.BTN_B, asyncCancel if cb else syncCancel)
119140
focus = (2 if button_no else 0)
120141
else:
121142
button_no.set_focus()
122143
kb.enabled(0)
123-
ugfx.input_attach(ugfx.BTN_A, pressed_a)
124-
ugfx.input_attach(ugfx.BTN_B, pressed_b)
144+
ugfx.input_attach(ugfx.BTN_A, asyncCancel if cb else syncCancel)
145+
ugfx.input_attach(ugfx.BTN_B, asyncCancel if cb else syncCancel)
125146
focus = 0
126147
ugfx.flush()
127148

@@ -138,21 +159,14 @@ def toggle_focus(pressed):
138159
ugfx.flush()
139160

140161
wait_for_interrupt = True
141-
while wait_for_interrupt:
142-
time.sleep(0.2)
143-
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
162+
if cb:
163+
return
164+
else:
165+
while wait_for_interrupt:
166+
time.sleep(0.2)
153167

154-
if (focus == 0 and no_button) or button_pushed == "B": return done(False)
155-
return done(edit.text())
168+
if (focus == 0 and no_button) or button_pushed == "B": return done(False)
169+
return done(edit.text())
156170

157171
def prompt_option(options, index=0, text = "Please select one of the following:", title=None, select_text="OK", none_text=None):
158172
"""Shows a dialog prompting for one of multiple options

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy