From 05809b0723057283fafb39ad8a0f5e12a74dbfd3 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 7 Jun 2019 08:43:12 +0000 Subject: [PATCH] Test interactions for scrolling --- cheuph/cursor_tree_widget.py | 44 +++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/cheuph/cursor_tree_widget.py b/cheuph/cursor_tree_widget.py index de4ed34..1caa3c8 100644 --- a/cheuph/cursor_tree_widget.py +++ b/cheuph/cursor_tree_widget.py @@ -1,6 +1,8 @@ +# TODO remove testing code and clean up + import datetime import time -from typing import Tuple, TypeVar +from typing import Optional, Tuple, TypeVar import urwid @@ -19,30 +21,50 @@ class CursorTreeWidget(urwid.WidgetWrap): def __init__(self, tree: CursorTreeRenderer[E]) -> None: self._tree = tree - self._lines_widget = AttributedLinesWidget() - super().__init__(self._lines_widget) + self._lines = AttributedLinesWidget() + super().__init__(self._lines) + + self._tree._cursor_id = "->3->2->3" def render(self, size: Tuple[int, int], focus: bool) -> None: width, height = size self._tree.render(width, height) - self._lines_widget.set_lines(self._tree.lines) + self._lines.set_lines(self._tree.lines) return super().render(size, focus) - def selectable(self): + def selectable(self) -> bool: return True - def keypress(self, size: Tuple[int, int], key: str): + def keypress(self, size: Tuple[int, int], key: str) -> Optional[str]: if key == "shift up": - self._tree.scroll(-1) - elif key == "shift down": self._tree.scroll(1) + self._invalidate() + elif key == "shift down": + self._tree.scroll(-1) + self._invalidate() elif key == "shift right": - self._tree.scroll_horizontally(1) + self.scroll_horizontally(1) elif key == "shift left": - self._tree.scroll_horizontally(-1) + self.scroll_horizontally(-1) + elif key in {"home", "shift home"}: + self._lines.horizontal_offset = 0 + self._invalidate() else: t = datetime.datetime(2019,5,7,13,25,6) - self._tree._supply.add(Message(str(time.time()), None, t, "key", key)) + self._tree._supply.add(Message( + str(time.time()), + self._tree._cursor_id, + t, + "key", + key, + )) self._invalidate() + + return None + + def scroll_horizontally(self, offset: int) -> None: + self._lines.horizontal_offset = max(0, + self._lines.horizontal_offset + offset) + self._invalidate()