Skip to content

Commit

Permalink
Pass the index to selected_handler
Browse files Browse the repository at this point in the history
Also make items, selected and selected_items accessible via the
widget.
  • Loading branch information
lpenz committed Nov 3, 2023
1 parent 8976044 commit a69669f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bin/ptvertmenu-man
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def manmenu() -> None:
manloader_queue: asyncio.Queue[Optional[tuple[int, str]]] = asyncio.Queue()
manloader_task = asyncio.create_task(man_loader(contents, manloader_queue))

def selected_handler(item: Optional[ManItem]) -> None:
def selected_handler(item: Optional[ManItem], index: int) -> None:
if item is not None:
manloader_queue.put_nowait(item[1])
else:
Expand Down
28 changes: 26 additions & 2 deletions src/ptvertmenu/vertmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(
self,
items: Iterable[Item],
selected_item: Optional[Item] = None,
selected_handler: Optional[Callable[[Optional[Item]], None]] = None,
selected_handler: Optional[Callable[[Optional[Item], int], None]] = None,
accept_handler: Optional[Callable[[Item], None]] = None,
focusable: bool = True,
):
Expand Down Expand Up @@ -100,12 +100,36 @@ def get_style(self) -> str:

def handle_selected(self) -> None:
if self.selected_handler is not None:
self.selected_handler(self.control.selected_item)
self.selected_handler(self.control.selected_item, self.control.selected)

def handle_accept(self) -> None:
if self.accept_handler is not None and self.control.selected_item is not None:
self.accept_handler(self.control.selected_item)

@property
def items(self) -> tuple[Item, ...]:
return self.control.items

@items.setter
def items(self, items: Iterable[Item]) -> None:
self.control.items = tuple(items)

@property
def selected(self) -> int:
return self.control.selected

@selected.setter
def selected(self, selected: int) -> None:
self.control.selected = selected

@property
def selected_item(self) -> Optional[Item]:
return self.control.selected_item

@selected_item.setter
def selected_item(self, item: Item) -> None:
self.control.selected_item = item

def __pt_container__(self) -> Container:
return self.window

Expand Down

0 comments on commit a69669f

Please sign in to comment.