Skip to content

Commit bf803b5

Browse files
HowTo app: make text focusable
to improve navigation
1 parent ca52112 commit bf803b5

1 file changed

Lines changed: 45 additions & 28 deletions

File tree

  • internal_filesystem/builtin/apps/com.micropythonos.howto/assets

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

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,27 @@ class HowTo(Activity):
1111
def onCreate(self):
1212
screen = lv.obj()
1313
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
14-
'''
15-
welcome_label = lv.label(screen)
16-
welcome_label.set_width(lv.pct(100))
17-
welcome_label.set_text("Welcome!")
18-
welcome_label.set_style_text_font(lv.font_montserrat_34, lv.PART.MAIN)
19-
welcome_label.set_style_margin_bottom(2, lv.PART.MAIN)
20-
'''
14+
# Make the screen focusable so it can be scrolled with the arrow keys
15+
focusgroup = lv.group_get_default()
16+
if focusgroup:
17+
focusgroup.add_obj(screen)
2118
preamble = "How to Navigate"
22-
title_label = lv.label(screen)
23-
title_label.set_width(lv.pct(100))
24-
title_label.set_text(preamble)
25-
title_label.set_style_text_font(lv.font_montserrat_24, lv.PART.MAIN)
26-
label = lv.label(screen)
27-
label.set_width(lv.pct(100))
28-
label.set_style_text_font(lv.font_montserrat_14, lv.PART.MAIN)
29-
buttonhelp = '''As you don't have a touch screen, you need to use the buttons to navigate:
30-
31-
- If you have a joystick and at least 2 buttons, then use the joystick to move around. Use one of the buttons to ENTER and another to go BACK.
32-
33-
- If you have 3 buttons, then one is PREVIOUS, one is ENTER and one is NEXT. To go back, press PREVIOUS and NEXT together.
19+
self._add_label(screen, preamble, is_header=True)
3420

35-
- If you have just 2 buttons, then one is PREVIOUS, the other is NEXT. To ENTER, press both at the same time. To go back, long-press the PREVIOUS button.
36-
'''
21+
buttonhelp_intro = "As you don't have a touch screen, you need to use the buttons to navigate:"
22+
buttonhelp_items = [
23+
"If you have a joystick and at least 2 buttons, then use the joystick to move around. Use one of the buttons to ENTER and another to go BACK.",
24+
"If you have 3 buttons, then one is PREVIOUS, one is ENTER and one is NEXT. To go back, press PREVIOUS and NEXT together.",
25+
"If you have just 2 buttons, then one is PREVIOUS, the other is NEXT. To ENTER, press both at the same time. To go back, long-press the PREVIOUS button.",
26+
]
3727
touchhelp = "Swipe from the left edge to go back and from the top edge to open the menu."
3828
from mpos import InputManager
3929
if InputManager.has_pointer():
40-
label.set_text(f'''
41-
{touchhelp}
42-
''')
30+
self._add_label(screen, touchhelp)
4331
else:
44-
label.set_text(f'''
45-
{buttonhelp}
46-
''')
47-
label.set_long_mode(lv.label.LONG_MODE.WRAP)
32+
self._add_label(screen, buttonhelp_intro)
33+
for item in buttonhelp_items:
34+
self._add_label(screen, f"• {item}")
4835

4936
self.dontshow_checkbox = lv.checkbox(screen)
5037
self.dontshow_checkbox.set_text("Don't show again")
@@ -56,6 +43,36 @@ def onCreate(self):
5643

5744
self.setContentView(screen)
5845

46+
@staticmethod
47+
def _focus_obj(event):
48+
target = event.get_target_obj()
49+
target.set_style_border_color(lv.theme_get_color_primary(None), lv.PART.MAIN)
50+
target.set_style_border_width(1, lv.PART.MAIN)
51+
target.scroll_to_view(True)
52+
53+
@staticmethod
54+
def _defocus_obj(event):
55+
target = event.get_target_obj()
56+
target.set_style_border_width(0, lv.PART.MAIN)
57+
58+
def _add_label(self, parent, text, is_header=False):
59+
label = lv.label(parent)
60+
label.set_width(lv.pct(100))
61+
label.set_text(text)
62+
label.set_long_mode(lv.label.LONG_MODE.WRAP)
63+
label.add_event_cb(self._focus_obj, lv.EVENT.FOCUSED, None)
64+
label.add_event_cb(self._defocus_obj, lv.EVENT.DEFOCUSED, None)
65+
focusgroup = lv.group_get_default()
66+
if focusgroup:
67+
focusgroup.add_obj(label)
68+
if is_header:
69+
label.set_style_text_font(lv.font_montserrat_24, lv.PART.MAIN)
70+
label.set_style_margin_bottom(4, lv.PART.MAIN)
71+
else:
72+
label.set_style_text_font(lv.font_montserrat_14, lv.PART.MAIN)
73+
label.set_style_margin_bottom(2, lv.PART.MAIN)
74+
return label
75+
5976
def onResume(self, screen):
6077
# Autostart can only be disabled if nothing was enabled or if this app was enabled
6178
self.prefs = SharedPreferences("com.micropythonos.settings")

0 commit comments

Comments
 (0)