Menu now filters items and sorts using priority, improving UX. (#3967)

* Menu now filters and sorts using priority, improving UX.

* Refactor: improve logic, removed redundancy

* Refactor: improve logic, removed redundancy

* Improved logic when getting view items in menuItems

* Improved logic when getting view items in menuItems
This commit is contained in:
Gabriel A Hernandez 2025-12-06 01:10:01 -08:00 committed by GitHub
parent 7398e2785d
commit 07ab6bf4a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 5 deletions

View File

@ -231,9 +231,15 @@ class MenuItemGroup:
def items(self) -> list[MenuItem]:
pattern = self._filter_pattern.lower()
items = filter(lambda item: item.is_empty() or pattern in item.text.lower(), self._menu_items)
l_items = list(items)
l_items = sorted(items, key=self._items_score)
return l_items
def _items_score(self, item: MenuItem) -> int:
pattern = self._filter_pattern.lower()
if item.text.lower().startswith(pattern):
return 0
return 1
@property
def filter_pattern(self) -> str:
return self._filter_pattern
@ -244,17 +250,17 @@ class MenuItemGroup:
def set_filter_pattern(self, pattern: str) -> None:
self._filter_pattern = pattern
delattr(self, 'items') # resetting the cache
self._reload_focus_item()
self.focus_first()
def append_filter(self, pattern: str) -> None:
self._filter_pattern += pattern
delattr(self, 'items') # resetting the cache
self._reload_focus_item()
self.focus_first()
def reduce_filter(self) -> None:
self._filter_pattern = self._filter_pattern[:-1]
delattr(self, 'items') # resetting the cache
self._reload_focus_item()
self.focus_first()
def _reload_focus_item(self) -> None:
if len(self.items) > 0:
@ -413,7 +419,7 @@ class MenuItemsState:
start, end = 0, 0
if len(self._view_items) == 0 or self._prev_row_idx == -1 or self._item_group.has_filter(): # initial setup or filter
if len(self._view_items) == 0 or self._prev_row_idx == -1 or focus_row_idx == 0: # initial setup
if focus_row_idx < self._total_rows:
start = 0
end = self._total_rows