Skip to content
Snippets Groups Projects
Commit 1887124d authored by schneider's avatar schneider
Browse files

feat(simple_menu): Add a method to detect a long (>1 second) press

parent d5b7b3f7
No related branches found
No related tags found
No related merge requests found
...@@ -141,6 +141,18 @@ class Menu: ...@@ -141,6 +141,18 @@ class Menu:
""" """
pass pass
def on_long_select(self, item, index):
"""
Hook when an item as selected using a long press.
The given ``index`` was selected with a long SELECT button press. Overwrite
this function in your menu to perform an action on select.
:param item: The item which was selected.
:param int index: Index into the ``entries`` list of the ``item``.
"""
pass
def on_select(self, item, index): def on_select(self, item, index):
""" """
Hook when an item as selected. Hook when an item as selected.
...@@ -238,9 +250,7 @@ class Menu: ...@@ -238,9 +250,7 @@ class Menu:
offset + 20, offset + 20,
col=self.color_1 if index % 2 == 0 else self.color_2, col=self.color_1 if index % 2 == 0 else self.color_2,
) )
self.disp.print( self.disp.print(string, posx=14, posy=offset, fg=self.color_text, bg=None)
string, posx=14, posy=offset, fg=self.color_text, bg=None,
)
def draw_menu(self, offset=0): def draw_menu(self, offset=0):
""" """
...@@ -318,8 +328,18 @@ class Menu: ...@@ -318,8 +328,18 @@ class Menu:
print("Exception during menu.on_scroll():") print("Exception during menu.on_scroll():")
sys.print_exception(e) sys.print_exception(e)
elif ev == self.button_select: elif ev == self.button_select:
t0 = utime.time()
long_press = False
while buttons.read(buttons.TOP_RIGHT) > 0:
if utime.time() - t0 > 1:
long_press = True
break
try: try:
self.on_select(self.entries[self.idx], self.idx) if long_press:
self.on_long_select(self.entries[self.idx], self.idx)
else:
self.on_select(self.entries[self.idx], self.idx)
self.select_time = utime.time_ms() self.select_time = utime.time_ms()
except _ExitMenuException: except _ExitMenuException:
raise raise
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment