Skip to content

Commit ebdcca7

Browse files
committed
Highlight the touched menu item
1 parent 97c743d commit ebdcca7

1 file changed

Lines changed: 45 additions & 17 deletions

File tree

src/krux/pages/__init__.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
BASE_HEX_SUFFIX = "HEX"
8888
BASE_OCT_SUFFIX = "OCT"
8989

90+
TOUCH_HIGHLIGHT_MS = 100
91+
9092

9193
class Page:
9294
"""Represents a page in the app, with helper methods for common display and
@@ -760,6 +762,19 @@ def draw_vertical_bar(self):
760762
color,
761763
)
762764

765+
def _clear_menu_display(self):
766+
if self.menu_offset > STATUS_BAR_HEIGHT:
767+
# Clear only the menu area
768+
self.ctx.display.fill_rectangle(
769+
0,
770+
self.menu_offset,
771+
self.ctx.display.width(),
772+
self.ctx.display.height() - self.menu_offset,
773+
theme.bg_color,
774+
)
775+
else:
776+
self.ctx.display.clear()
777+
763778
def run_loop(self, start_from_index=None, swipe_up_fnc=None, swipe_down_fnc=None):
764779
"""Runs the menu loop until one of the menu items returns either a MENU_EXIT
765780
or MENU_SHUTDOWN status
@@ -771,19 +786,11 @@ def run_loop(self, start_from_index=None, swipe_up_fnc=None, swipe_down_fnc=None
771786
selected_item_index = start_from_index
772787
while True:
773788
gc.collect()
774-
if self.menu_offset > STATUS_BAR_HEIGHT:
775-
# Clear only the menu area
776-
self.ctx.display.fill_rectangle(
777-
0,
778-
self.menu_offset,
779-
self.ctx.display.width(),
780-
self.ctx.display.height() - self.menu_offset,
781-
theme.bg_color,
782-
)
783-
else:
784-
self.ctx.display.clear()
789+
self._clear_menu_display()
785790
if kboard.has_touchscreen:
786-
self._draw_touch_menu(selected_item_index)
791+
self._draw_touch_menu(
792+
selected_item_index, draw_dividers=not self.ctx.input.buttons_active
793+
)
787794
else:
788795
self._draw_menu(selected_item_index)
789796
self.draw_status_bar()
@@ -796,6 +803,7 @@ def run_loop(self, start_from_index=None, swipe_up_fnc=None, swipe_down_fnc=None
796803
start_from_submenu = False
797804
else:
798805
screensaver_time = Settings().appearance.screensaver_time
806+
was_btn_active = self.ctx.input.buttons_active
799807
btn = self.ctx.input.wait_for_fastnav_button(
800808
# Block if screen saver not active
801809
screensaver_time == 0,
@@ -804,6 +812,20 @@ def run_loop(self, start_from_index=None, swipe_up_fnc=None, swipe_down_fnc=None
804812
if kboard.has_touchscreen:
805813
if btn == BUTTON_TOUCH:
806814
selected_item_index = self.ctx.input.touch.current_index()
815+
816+
# highlight selected index before continue
817+
if was_btn_active:
818+
# need to clear screen if button was used just before
819+
# because an item will be highlighted already
820+
self._clear_menu_display()
821+
self._draw_touch_menu(
822+
selected_item_index,
823+
draw_dividers=not was_btn_active,
824+
highlight=True,
825+
)
826+
time.sleep_ms(
827+
TOUCH_HIGHLIGHT_MS
828+
) # wait a little to see item highlighted
807829
btn = BUTTON_ENTER
808830
self.ctx.input.touch.clear_regions()
809831
if btn == BUTTON_ENTER:
@@ -972,7 +994,9 @@ def _get_menu_item_color(self, menu_item):
972994
return menu_item[2]
973995
return theme.fg_color
974996

975-
def _draw_touch_menu(self, selected_item_index):
997+
def _draw_touch_menu(
998+
self, selected_item_index, draw_dividers=True, highlight=False
999+
):
9761000
# map regions with dynamic height to fill screen
9771001
self.ctx.input.touch.clear_regions()
9781002
offset_y = 0
@@ -994,8 +1018,8 @@ def _draw_touch_menu(self, selected_item_index):
9941018
self.ctx.input.touch.y_regions = y_keypad_map
9951019

9961020
# Draw dividers
997-
for i, y in enumerate(y_keypad_map[:-1]):
998-
if i and not self.ctx.input.buttons_active:
1021+
if draw_dividers:
1022+
for y in y_keypad_map[1:-1]: # skip the first and last entries
9991023
self.ctx.display.draw_line(
10001024
0, y, self.ctx.display.width(), y, theme.frame_color
10011025
)
@@ -1008,7 +1032,9 @@ def _draw_touch_menu(self, selected_item_index):
10081032
region_height - len(menu_item_lines) * FONT_HEIGHT
10091033
) // 2 + y_keypad_map[i]
10101034
fg_color = self._get_menu_item_color(menu_item)
1011-
if selected_item_index == i and self.ctx.input.buttons_active:
1035+
if selected_item_index == i and (
1036+
self.ctx.input.buttons_active or highlight
1037+
):
10121038
self.ctx.display.fill_rectangle(
10131039
0,
10141040
offset_y_item + 1 - FONT_HEIGHT // 2,
@@ -1017,7 +1043,9 @@ def _draw_touch_menu(self, selected_item_index):
10171043
fg_color,
10181044
)
10191045
for j, text in enumerate(menu_item_lines):
1020-
if selected_item_index == i and self.ctx.input.buttons_active:
1046+
if selected_item_index == i and (
1047+
self.ctx.input.buttons_active or highlight
1048+
):
10211049
self.ctx.display.draw_hcentered_text(
10221050
text, offset_y_item + FONT_HEIGHT * j, theme.bg_color, fg_color
10231051
)

0 commit comments

Comments
 (0)