From 59feafc3094ff8cdd6d5f6112e5b3646db436383 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Wed, 6 Sep 2023 02:05:57 -0400 Subject: [PATCH] WIP: bindings for gallery app Why are the bindings not working? Ctrl+Q doesn't work, left/right don't even show up! --- src/textual_paint/gallery.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/textual_paint/gallery.py b/src/textual_paint/gallery.py index 66bd372..20e9e5e 100644 --- a/src/textual_paint/gallery.py +++ b/src/textual_paint/gallery.py @@ -44,12 +44,15 @@ class GalleryItem(Vertical): class GalleryApp(App[None]): """ANSI art gallery TUI""" - TITLE = f"ANSI art gallery v{__version__}" + TITLE = "ANSI art gallery" CSS_PATH = "gallery.css" BINDINGS = [ - Binding("ctrl+q", "exit", _("Quit")), + Binding("ctrl+q", "exit", _("Quit"), priority=True), + Binding("ctrl+d", "toggle_dark", _("Toggle Dark Mode")), + Binding("left", "previous", _("Previous"), priority=True), + Binding("right", "next", _("Next"), priority=True), # dev helper # f5 would be more traditional, but I need something not bound to anything # in the context of the terminal in VS Code, and not used by this app, like Ctrl+R, and detectable in the terminal. @@ -106,6 +109,29 @@ class GalleryApp(App[None]): self.scroll.mount(GalleryItem(image, caption=filename)) + def _scroll_to_adjacent_item(self, delta_index: int = 0) -> None: + """Scroll to the next/previous item.""" + # try: + # index = self.scroll.children.index(self.app.focused) + # except ValueError: + # return + widget, _ = self.app.get_widget_at(self.screen.region.width // 2, self.screen.region.height // 2) + while widget is not None and not isinstance(widget, GalleryItem): + widget = widget.parent + if widget is None: + index = 0 + else: + index = self.scroll.children.index(widget) + self.scroll.children[index + delta_index].focus() + + def action_next(self) -> None: + """Scroll to the next item.""" + self._scroll_to_adjacent_item(1) + + def action_previous(self) -> None: + """Scroll to the previous item.""" + self._scroll_to_adjacent_item(-1) + def action_reload(self) -> None: """Reload the program.""" restart_program()