Skip to content

Commit 82f55e0

Browse files
Wifi app: simplify keyboard handling code
1 parent 2b4e57b commit 82f55e0

File tree

3 files changed

+15
-41
lines changed

3 files changed

+15
-41
lines changed

internal_filesystem/apps/com.micropythonos.camera/assets/camera_settings.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import lvgl as lv
2-
from mpos.ui.keyboard import MposKeyboard
32

43
import mpos.ui
54
from mpos.apps import Activity
@@ -233,22 +232,14 @@ def create_textarea(self, parent, label_text, min_val, max_val, default_val, pre
233232
textarea.align(lv.ALIGN.TOP_RIGHT, 0, 0)
234233

235234
# Initialize keyboard (hidden initially)
235+
from mpos.ui.keyboard import MposKeyboard
236236
keyboard = MposKeyboard(parent)
237237
keyboard.align(lv.ALIGN.BOTTOM_MID, 0, 0)
238238
keyboard.add_flag(lv.obj.FLAG.HIDDEN)
239239
keyboard.set_textarea(textarea)
240-
keyboard.add_event_cb(lambda e, kbd=keyboard: self.hide_keyboard(kbd), lv.EVENT.READY, None)
241-
keyboard.add_event_cb(lambda e, kbd=keyboard: self.hide_keyboard(kbd), lv.EVENT.CANCEL, None)
242-
textarea.add_event_cb(lambda e, kbd=keyboard: self.show_keyboard(kbd), lv.EVENT.CLICKED, None)
243240

244241
return textarea, cont
245242

246-
def show_keyboard(self, kbd):
247-
mpos.ui.anim.smooth_show(kbd)
248-
249-
def hide_keyboard(self, kbd):
250-
mpos.ui.anim.smooth_hide(kbd)
251-
252243
def add_buttons(self, parent):
253244
# Save/Cancel buttons at bottom
254245
button_cont = lv.obj(parent)

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def onCreate(self):
237237
self.password_ta.set_width(lv.pct(90))
238238
self.password_ta.set_one_line(True)
239239
self.password_ta.align_to(label, lv.ALIGN.OUT_BOTTOM_MID, 0, 5)
240-
self.password_ta.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None)
241240
print("PasswordPage: Creating Connect button")
242241
self.connect_button=lv.button(password_page)
243242
self.connect_button.set_size(100,40)
@@ -262,16 +261,10 @@ def onCreate(self):
262261
self.keyboard=MposKeyboard(password_page)
263262
self.keyboard.align(lv.ALIGN.BOTTOM_MID,0,0)
264263
self.keyboard.set_textarea(self.password_ta)
265-
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.READY, None)
266-
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.CANCEL, None)
267264
self.keyboard.add_flag(lv.obj.FLAG.HIDDEN)
268-
self.keyboard.add_event_cb(self.handle_keyboard_events, lv.EVENT.VALUE_CHANGED, None)
269265
print("PasswordPage: Loading password page")
270266
self.setContentView(password_page)
271267

272-
def onStop(self, screen):
273-
self.hide_keyboard()
274-
275268
def connect_cb(self, event):
276269
global access_points
277270
print("connect_cb: Connect button clicked")
@@ -290,28 +283,6 @@ def cancel_cb(self, event):
290283
print("cancel_cb: Cancel button clicked")
291284
self.finish()
292285

293-
def show_keyboard(self):
294-
self.connect_button.add_flag(lv.obj.FLAG.HIDDEN)
295-
self.cancel_button.add_flag(lv.obj.FLAG.HIDDEN)
296-
mpos.ui.anim.smooth_show(self.keyboard)
297-
focusgroup = lv.group_get_default()
298-
if focusgroup:
299-
focusgroup.focus_next() # move the focus to the keyboard to save the user a "next" button press (optional but nice)
300-
301-
def hide_keyboard(self):
302-
mpos.ui.anim.smooth_hide(self.keyboard)
303-
self.connect_button.remove_flag(lv.obj.FLAG.HIDDEN)
304-
self.cancel_button.remove_flag(lv.obj.FLAG.HIDDEN)
305-
306-
def handle_keyboard_events(self, event):
307-
target_obj=event.get_target_obj() # keyboard
308-
button = target_obj.get_selected_button()
309-
text = target_obj.get_button_text(button)
310-
#print(f"button {button} and text {text}")
311-
if text == lv.SYMBOL.NEW_LINE:
312-
print("Newline pressed, closing the keyboard...")
313-
self.hide_keyboard()
314-
315286
@staticmethod
316287
def setPassword(ssid, password):
317288
global access_points

internal_filesystem/lib/mpos/ui/keyboard.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@ def __init__(self, parent):
125125
self._keyboard.set_style_min_height(175, 0)
126126

127127
def _handle_events(self, event):
128-
# Only process VALUE_CHANGED events for actual typing
129-
if event.get_code() != lv.EVENT.VALUE_CHANGED:
128+
code = event.get_code()
129+
#print(f"keyboard event code = {code}")
130+
if code == lv.EVENT.READY or code == lv.EVENT.CANCEL:
131+
self.hide_keyboard()
132+
return
133+
# Process VALUE_CHANGED events for actual typing
134+
if code != lv.EVENT.VALUE_CHANGED:
130135
return
131136

132137
# Get the pressed button and its text
@@ -207,6 +212,7 @@ def set_textarea(self, textarea):
207212
self._textarea = textarea
208213
# NOTE: We deliberately DO NOT call self._keyboard.set_textarea()
209214
# to avoid LVGL's automatic character insertion
215+
self._textarea.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None)
210216

211217
def get_textarea(self):
212218
"""
@@ -243,3 +249,9 @@ def __getattr__(self, name):
243249
"""
244250
# Forward to the underlying keyboard object
245251
return getattr(self._keyboard, name)
252+
253+
def show_keyboard(self):
254+
mpos.ui.anim.smooth_show(self._keyboard)
255+
256+
def hide_keyboard(self):
257+
mpos.ui.anim.smooth_hide(self._keyboard)

0 commit comments

Comments
 (0)