Skip to content

Commit

Permalink
feat: Made command palette scrollable and some UI improvements #447
Browse files Browse the repository at this point in the history
 from tomlin7/scrollable-palette
  • Loading branch information
tomlin7 authored Nov 2, 2024
2 parents 03710d9 + 05ee229 commit 2a5c79a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/biscuit/common/palette/palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def __init__(self, master: App, width=80, *args, **kwargs) -> None:

self.row = 1
self.selected = 0
self.start_index = 0

self.shown_items = []

self.actionsets = []
self.active_set = None
self.active_items = None

self.searchbar = SearchBar(self)
self.searchbar.grid(row=0, sticky=tk.EW, in_=self.container, pady=(0, 2))
Expand Down Expand Up @@ -121,6 +123,19 @@ def configure_bindings(self) -> None:
self.row += 1
self.refresh_selected()

def reset_start_index(self) -> None:
self.start_index = 0

def on_mousewheel(self, event) -> str:
if not self.active_items:
return "break"

# start_index must be between 0 and len(active_items) - 10
self.start_index = min(
max(0, self.start_index - event.delta // 120), len(self.active_items) - 10
)
self.show_items(self.active_items)

def pick_actionset(self, actionset: ActionSet) -> None:
"""Picks an actionset to display in the palette
Expand Down Expand Up @@ -159,6 +174,8 @@ def hide(self, *args) -> None:
self.withdraw()
self.reset()

self.container.unbind_all("<MouseWheel>")

def hide_all_items(self) -> None:
"""Hides all items in the palette"""

Expand Down Expand Up @@ -226,8 +243,9 @@ def show_items(self, items: list[PaletteItem]) -> None:
items (list[PaletteItem]): The items to display in the palette"""

self.hide_all_items()
self.active_items = items

for i in items[:10]:
for i in self.active_items[self.start_index : self.start_index + 10]:
item = self.add_item(*i)
item.mark_term(self.searchbar.term)

Expand Down Expand Up @@ -256,3 +274,5 @@ def show(self, prefix: str = None, default: str = None) -> None:

if default:
self.searchbar.set_search_term(default)

self.container.bind_all("<MouseWheel>", self.on_mousewheel)
1 change: 1 addition & 0 deletions src/biscuit/common/palette/searchbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def filter(self, *args) -> None:
includes.append(i)

new = list(chain(actionset.get_pinned(term), exact, starts, includes))
self.master.reset_start_index()
if any(new):
self.master.show_items(new)
else:
Expand Down

0 comments on commit 2a5c79a

Please sign in to comment.