Skip to content

Commit 43d960c

Browse files
wifi.py: improve focus handling for keyboard
1 parent bd1afb4 commit 43d960c

File tree

1 file changed

+44
-5
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.wifi/assets

1 file changed

+44
-5
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WiFi(Activity):
4040
scan_button_label = None
4141

4242
def onCreate(self):
43+
print("wifi.py onCreate")
4344
main_screen = lv.obj()
4445
main_screen.set_style_pad_all(15, 0)
4546
print("create_ui: Creating list widget")
@@ -62,6 +63,7 @@ def onCreate(self):
6263
self.setContentView(main_screen)
6364

6465
def onResume(self, screen):
66+
print("wifi.py onResume")
6567
global access_points
6668
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
6769
self.keep_running = True
@@ -224,6 +226,27 @@ def attempt_connecting_thread(self, ssid, password):
224226
lv.async_call(lambda l: self.refresh_list(), None)
225227

226228

229+
def print_events(event):
230+
event_code=event.get_code()
231+
#print(f"got event {event_code}")
232+
# Ignore:
233+
# =======
234+
# 19: HIT_TEST
235+
# COVER_CHECK
236+
# DRAW_MAIN
237+
# DRAW_MAIN_BEGIN
238+
# DRAW_MAIN_END
239+
# DRAW_POST
240+
# DRAW_POST_BEGIN
241+
# DRAW_POST_END
242+
# 39: CHILD_CHANGED
243+
# GET_SELF_SIZE
244+
if event_code not in [19,23,25,26,27,28,29,30,39,49]:
245+
name = mpos.ui.get_event_name(event_code)
246+
print(f"lv_event_t: code={event_code}, name={name}")
247+
target=event.get_target()
248+
print(f"target: {target}")
249+
227250

228251

229252
class PasswordPage(Activity):
@@ -250,8 +273,9 @@ def onCreate(self):
250273
self.password_ta.set_size(200,30)
251274
self.password_ta.set_one_line(True)
252275
self.password_ta.align_to(label, lv.ALIGN.OUT_BOTTOM_MID, 5, 0)
253-
self.password_ta.add_event_cb(lambda *args: mpos.ui.anim.smooth_show(self.keyboard), lv.EVENT.CLICKED, None) # it might be focused, but keyboard hidden (because ready/cancel clicked)
254-
self.password_ta.add_event_cb(lambda *args: mpos.ui.anim.smooth_hide(self.keyboard), lv.EVENT.DEFOCUSED, None)
276+
self.password_ta.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None) # it might be focused, but keyboard hidden (because ready/cancel clicked)
277+
self.password_ta.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.FOCUSED, None)
278+
#self.password_ta.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.DEFOCUSED, None) # doesn't work for non-touchscreen (Keypad) control because then focus needs to go to the lv_keyboard widget
255279
print("PasswordPage: Creating Connect button")
256280
self.connect_button=lv.button(password_page)
257281
self.connect_button.set_size(100,40)
@@ -277,14 +301,15 @@ def onCreate(self):
277301
self.keyboard.align(lv.ALIGN.BOTTOM_MID,0,0)
278302
self.keyboard.set_textarea(self.password_ta)
279303
self.keyboard.set_style_min_height(160, 0)
280-
self.keyboard.add_event_cb(lambda *args: mpos.ui.anim.smooth_hide(self.keyboard), lv.EVENT.READY, None)
281-
self.keyboard.add_event_cb(lambda *args: mpos.ui.anim.smooth_hide(self.keyboard), lv.EVENT.CANCEL, None)
304+
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.READY, None)
305+
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.CANCEL, None)
282306
self.keyboard.add_flag(lv.obj.FLAG.HIDDEN)
307+
self.keyboard.add_event_cb(print_events, lv.EVENT.ALL, None)
283308
print("PasswordPage: Loading password page")
284309
self.setContentView(password_page)
285310

286311
def onStop(self, screen):
287-
mpos.ui.anim.smooth_hide(self.keyboard)
312+
self.hide_keyboard()
288313

289314
def connect_cb(self, event):
290315
global access_points
@@ -304,6 +329,20 @@ def cancel_cb(self, event):
304329
print("cancel_cb: Cancel button clicked")
305330
self.finish()
306331

332+
def show_keyboard(self):
333+
self.connect_button.add_flag(lv.obj.FLAG.HIDDEN)
334+
self.cancel_button.add_flag(lv.obj.FLAG.HIDDEN)
335+
mpos.ui.anim.smooth_show(self.keyboard)
336+
focusgroup = lv.group_get_default()
337+
focusgroup.focus_next() # move the focus to the keypad
338+
339+
def hide_keyboard(self):
340+
self.connect_button.remove_flag(lv.obj.FLAG.HIDDEN)
341+
self.cancel_button.remove_flag(lv.obj.FLAG.HIDDEN)
342+
focusgroup = lv.group_get_default()
343+
focusgroup.focus_prev() # move the focus to the close button, otherwise it goes back to the textarea, which opens the keyboard again
344+
mpos.ui.anim.smooth_hide(self.keyboard)
345+
307346
@staticmethod
308347
def setPassword(ssid, password):
309348
global access_points

0 commit comments

Comments
 (0)