Commit 1887124d authored by schneider's avatar schneider

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

parent d5b7b3f7
......@@ -141,6 +141,18 @@ class Menu:
"""
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):
"""
Hook when an item as selected.
......@@ -238,9 +250,7 @@ class Menu:
offset + 20,
col=self.color_1 if index % 2 == 0 else self.color_2,
)
self.disp.print(
string, posx=14, posy=offset, fg=self.color_text, bg=None,
)
self.disp.print(string, posx=14, posy=offset, fg=self.color_text, bg=None)
def draw_menu(self, offset=0):
"""
......@@ -318,8 +328,18 @@ class Menu:
print("Exception during menu.on_scroll():")
sys.print_exception(e)
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:
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()
except _ExitMenuException:
raise
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment