You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Wifi app: simplify keyboard handling code
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import lvgl as lv
|
||||
from mpos.ui.keyboard import MposKeyboard
|
||||
|
||||
import mpos.ui
|
||||
from mpos.apps import Activity
|
||||
@@ -233,22 +232,14 @@ class CameraSettingsActivity(Activity):
|
||||
textarea.align(lv.ALIGN.TOP_RIGHT, 0, 0)
|
||||
|
||||
# Initialize keyboard (hidden initially)
|
||||
from mpos.ui.keyboard import MposKeyboard
|
||||
keyboard = MposKeyboard(parent)
|
||||
keyboard.align(lv.ALIGN.BOTTOM_MID, 0, 0)
|
||||
keyboard.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
keyboard.set_textarea(textarea)
|
||||
keyboard.add_event_cb(lambda e, kbd=keyboard: self.hide_keyboard(kbd), lv.EVENT.READY, None)
|
||||
keyboard.add_event_cb(lambda e, kbd=keyboard: self.hide_keyboard(kbd), lv.EVENT.CANCEL, None)
|
||||
textarea.add_event_cb(lambda e, kbd=keyboard: self.show_keyboard(kbd), lv.EVENT.CLICKED, None)
|
||||
|
||||
return textarea, cont
|
||||
|
||||
def show_keyboard(self, kbd):
|
||||
mpos.ui.anim.smooth_show(kbd)
|
||||
|
||||
def hide_keyboard(self, kbd):
|
||||
mpos.ui.anim.smooth_hide(kbd)
|
||||
|
||||
def add_buttons(self, parent):
|
||||
# Save/Cancel buttons at bottom
|
||||
button_cont = lv.obj(parent)
|
||||
|
||||
@@ -237,7 +237,6 @@ class PasswordPage(Activity):
|
||||
self.password_ta.set_width(lv.pct(90))
|
||||
self.password_ta.set_one_line(True)
|
||||
self.password_ta.align_to(label, lv.ALIGN.OUT_BOTTOM_MID, 0, 5)
|
||||
self.password_ta.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None)
|
||||
print("PasswordPage: Creating Connect button")
|
||||
self.connect_button=lv.button(password_page)
|
||||
self.connect_button.set_size(100,40)
|
||||
@@ -262,16 +261,10 @@ class PasswordPage(Activity):
|
||||
self.keyboard=MposKeyboard(password_page)
|
||||
self.keyboard.align(lv.ALIGN.BOTTOM_MID,0,0)
|
||||
self.keyboard.set_textarea(self.password_ta)
|
||||
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.READY, None)
|
||||
self.keyboard.add_event_cb(lambda *args: self.hide_keyboard(), lv.EVENT.CANCEL, None)
|
||||
self.keyboard.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
self.keyboard.add_event_cb(self.handle_keyboard_events, lv.EVENT.VALUE_CHANGED, None)
|
||||
print("PasswordPage: Loading password page")
|
||||
self.setContentView(password_page)
|
||||
|
||||
def onStop(self, screen):
|
||||
self.hide_keyboard()
|
||||
|
||||
def connect_cb(self, event):
|
||||
global access_points
|
||||
print("connect_cb: Connect button clicked")
|
||||
@@ -290,28 +283,6 @@ class PasswordPage(Activity):
|
||||
print("cancel_cb: Cancel button clicked")
|
||||
self.finish()
|
||||
|
||||
def show_keyboard(self):
|
||||
self.connect_button.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
self.cancel_button.add_flag(lv.obj.FLAG.HIDDEN)
|
||||
mpos.ui.anim.smooth_show(self.keyboard)
|
||||
focusgroup = lv.group_get_default()
|
||||
if focusgroup:
|
||||
focusgroup.focus_next() # move the focus to the keyboard to save the user a "next" button press (optional but nice)
|
||||
|
||||
def hide_keyboard(self):
|
||||
mpos.ui.anim.smooth_hide(self.keyboard)
|
||||
self.connect_button.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
self.cancel_button.remove_flag(lv.obj.FLAG.HIDDEN)
|
||||
|
||||
def handle_keyboard_events(self, event):
|
||||
target_obj=event.get_target_obj() # keyboard
|
||||
button = target_obj.get_selected_button()
|
||||
text = target_obj.get_button_text(button)
|
||||
#print(f"button {button} and text {text}")
|
||||
if text == lv.SYMBOL.NEW_LINE:
|
||||
print("Newline pressed, closing the keyboard...")
|
||||
self.hide_keyboard()
|
||||
|
||||
@staticmethod
|
||||
def setPassword(ssid, password):
|
||||
global access_points
|
||||
|
||||
@@ -125,8 +125,13 @@ class MposKeyboard:
|
||||
self._keyboard.set_style_min_height(175, 0)
|
||||
|
||||
def _handle_events(self, event):
|
||||
# Only process VALUE_CHANGED events for actual typing
|
||||
if event.get_code() != lv.EVENT.VALUE_CHANGED:
|
||||
code = event.get_code()
|
||||
#print(f"keyboard event code = {code}")
|
||||
if code == lv.EVENT.READY or code == lv.EVENT.CANCEL:
|
||||
self.hide_keyboard()
|
||||
return
|
||||
# Process VALUE_CHANGED events for actual typing
|
||||
if code != lv.EVENT.VALUE_CHANGED:
|
||||
return
|
||||
|
||||
# Get the pressed button and its text
|
||||
@@ -207,6 +212,7 @@ class MposKeyboard:
|
||||
self._textarea = textarea
|
||||
# NOTE: We deliberately DO NOT call self._keyboard.set_textarea()
|
||||
# to avoid LVGL's automatic character insertion
|
||||
self._textarea.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None)
|
||||
|
||||
def get_textarea(self):
|
||||
"""
|
||||
@@ -243,3 +249,9 @@ class MposKeyboard:
|
||||
"""
|
||||
# Forward to the underlying keyboard object
|
||||
return getattr(self._keyboard, name)
|
||||
|
||||
def show_keyboard(self):
|
||||
mpos.ui.anim.smooth_show(self._keyboard)
|
||||
|
||||
def hide_keyboard(self):
|
||||
mpos.ui.anim.smooth_hide(self._keyboard)
|
||||
|
||||
Reference in New Issue
Block a user