Skip to content

Commit 2a70e32

Browse files
Add InputManager framework
1 parent a017db8 commit 2a70e32

File tree

8 files changed

+61
-40
lines changed

8 files changed

+61
-40
lines changed

internal_filesystem/apps/com.micropythonos.draw/assets/draw.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import lvgl as lv
2-
from mpos import Activity, DisplayMetrics
2+
from mpos import Activity, DisplayMetrics, InputManager
33

44
indev_error_x = 160
55
indev_error_y = 120
@@ -35,11 +35,8 @@ def onCreate(self):
3535
def touch_cb(self, event):
3636
event_code=event.get_code()
3737
if event_code not in [19,23,25,26,27,28,29,30,49]:
38-
#name = ui.get_event_name(event_code)
39-
#print(f"lv_event_t: code={event_code}, name={name}") # target={event.get_target()}, user_data={event.get_user_data()}, param={event.get_param()}
4038
if event_code == lv.EVENT.PRESSING: # this is probably enough
41-
#if event_code in [lv.EVENT.PRESSED, lv.EVENT.PRESSING, lv.EVENT.LONG_PRESSED, lv.EVENT.LONG_PRESSED_REPEAT]:
42-
x, y = DisplayMetrics.pointer_xy()
39+
x, y = InputManager.pointer_xy()
4340
#canvas.set_px(x,y,lv.color_black(),lv.OPA.COVER) # draw a tiny point
4441
self.draw_rect(x,y)
4542
#self.draw_line(x,y)

internal_filesystem/lib/mpos/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
# UI utility functions
3737
from .ui.display_metrics import DisplayMetrics
38+
from .ui.input_manager import InputManager
3839
from .ui.appearance_manager import AppearanceManager
3940
from .ui.event import get_event_name, print_event
4041
from .ui.view import setContentView, back_screen
@@ -73,8 +74,9 @@
7374
"SettingActivity", "SettingsActivity", "CameraActivity",
7475
# UI components
7576
"MposKeyboard",
76-
# UI utility - DisplayMetrics and AppearanceManager
77+
# UI utility - DisplayMetrics, InputManager and AppearanceManager
7778
"DisplayMetrics",
79+
"InputManager",
7880
"AppearanceManager",
7981
"get_event_name", "print_event",
8082
"setContentView", "back_screen",

internal_filesystem/lib/mpos/ui/display_metrics.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,3 @@ def max_dimension(cls):
6969
"""Get maximum dimension (width or height)."""
7070
return max(cls._width, cls._height)
7171

72-
@classmethod
73-
def pointer_xy(cls):
74-
"""Get current pointer/touch coordinates."""
75-
import lvgl as lv
76-
indev = lv.indev_active()
77-
if indev:
78-
p = lv.point_t()
79-
indev.get_point(p)
80-
return p.x, p.y
81-
return -1, -1

internal_filesystem/lib/mpos/ui/focus_direction.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,8 @@ def process_object(obj, depth=0):
144144

145145
return closest_obj
146146

147-
# This function is missing so emulate it using focus_next():
148-
def emulate_focus_obj(focusgroup, target):
149-
if not focusgroup:
150-
print("emulate_focus_obj needs a focusgroup, returning...")
151-
return
152-
if not target:
153-
print("emulate_focus_obj needs a target, returning...")
154-
return
155-
for objnr in range(focusgroup.get_obj_count()):
156-
currently_focused = focusgroup.get_focused()
157-
#print ("emulate_focus_obj: currently focused:") ; mpos.util.print_lvgl_widget(currently_focused)
158-
if currently_focused is target:
159-
#print("emulate_focus_obj: found target, stopping")
160-
return
161-
else:
162-
focusgroup.focus_next()
163-
print("WARNING: emulate_focus_obj failed to find target")
164-
165147
def move_focus_direction(angle):
148+
from .input_manager import InputManager
166149
focus_group = lv.group_get_default()
167150
if not focus_group:
168151
print("move_focus_direction: no default focus_group found, returning...")
@@ -191,4 +174,4 @@ def move_focus_direction(angle):
191174
if o:
192175
#print("move_focus_direction: moving focus to:")
193176
#mpos.util.print_lvgl_widget(o)
194-
emulate_focus_obj(focus_group, o)
177+
InputManager.emulate_focus_obj(focus_group, o)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# lib/mpos/ui/input_manager.py
2+
"""
3+
InputManager - Framework for managing input device interactions.
4+
5+
Provides a clean API for accessing input device data like pointer/touch coordinates
6+
and focus management.
7+
All methods are class methods, so no instance creation is needed.
8+
"""
9+
10+
11+
class InputManager:
12+
"""
13+
Input manager singleton for handling input device interactions.
14+
15+
Provides static/class methods for accessing input device properties and data.
16+
"""
17+
18+
@classmethod
19+
def pointer_xy(cls):
20+
"""Get current pointer/touch coordinates."""
21+
import lvgl as lv
22+
indev = lv.indev_active()
23+
if indev:
24+
p = lv.point_t()
25+
indev.get_point(p)
26+
return p.x, p.y
27+
return -1, -1
28+
29+
@classmethod
30+
def emulate_focus_obj(cls, focusgroup, target):
31+
"""
32+
Emulate setting focus to a specific object in the focus group.
33+
This function is needed because LVGL doesn't have a direct set_focus method.
34+
"""
35+
if not focusgroup:
36+
print("emulate_focus_obj needs a focusgroup, returning...")
37+
return
38+
if not target:
39+
print("emulate_focus_obj needs a target, returning...")
40+
return
41+
for objnr in range(focusgroup.get_obj_count()):
42+
currently_focused = focusgroup.get_focused()
43+
if currently_focused is target:
44+
return
45+
else:
46+
focusgroup.focus_next()
47+
print("WARNING: emulate_focus_obj failed to find target")

internal_filesystem/lib/mpos/ui/keyboard.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ def scroll_after_show(self, timer):
240240
def focus_on_keyboard(self, timer=None):
241241
default_group = lv.group_get_default()
242242
if default_group:
243-
from .focus_direction import emulate_focus_obj, move_focus_direction
244-
emulate_focus_obj(default_group, self._keyboard)
243+
from .input_manager import InputManager
244+
from .focus_direction import move_focus_direction
245+
InputManager.emulate_focus_obj(default_group, self._keyboard)
245246

246247
def scroll_back_after_hide(self, timer):
247248
self._parent.scroll_to_y(self._saved_scroll_y, True)

internal_filesystem/lib/mpos/ui/topmenu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .display_metrics import DisplayMetrics
66
from .appearance_manager import AppearanceManager
77
from .util import (get_foreground_app)
8+
from .input_manager import InputManager
89
from . import focus_direction
910
from .widget_animator import WidgetAnimator
1011
from mpos.content.app_manager import AppManager
@@ -372,7 +373,7 @@ def poweroff_cb(e):
372373
def drawer_scroll_callback(event):
373374
global scroll_start_y
374375
event_code=event.get_code()
375-
x, y = DisplayMetrics.pointer_xy()
376+
x, y = InputManager.pointer_xy()
376377
#name = mpos.ui.get_event_name(event_code)
377378
#print(f"drawer_scroll: code={event_code}, name={name}, ({x},{y})")
378379
if event_code == lv.EVENT.SCROLL_BEGIN and scroll_start_y == None:

internal_filesystem/lib/mpos/ui/view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def back_screen():
8686
if default_group:
8787
from .focus import move_focusgroup_objects
8888
move_focusgroup_objects(prev_focusgroup, default_group)
89-
from .focus_direction import emulate_focus_obj
90-
emulate_focus_obj(default_group, prev_focused)
89+
from .input_manager import InputManager
90+
InputManager.emulate_focus_obj(default_group, prev_focused)
9191

9292
if prev_activity:
9393
prev_activity.onResume(prev_screen)

0 commit comments

Comments
 (0)