From f5a9d1f29c65f0a32acd3471fc1dd8eccbfb2c7d Mon Sep 17 00:00:00 2001 From: Josiah Outram Halstead Date: Sat, 17 Dec 2022 10:36:32 +0000 Subject: [PATCH 1/2] Allow nested scrolling of Windows These changes mean that Windows will return NotImplemented when attempting to scroll beyond their scrollable limits, allowing mouse scroll events to bubble up to a parent container when a window in a scrollable container is scrolled. --- src/prompt_toolkit/layout/containers.py | 22 ++++++++++++---------- src/prompt_toolkit/layout/controls.py | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/prompt_toolkit/layout/containers.py b/src/prompt_toolkit/layout/containers.py index 100d4aaeb..ee7140179 100644 --- a/src/prompt_toolkit/layout/containers.py +++ b/src/prompt_toolkit/layout/containers.py @@ -2553,33 +2553,33 @@ def _mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone: key binding (no UI invalidate required in that case). """ if mouse_event.event_type == MouseEventType.SCROLL_DOWN: - self._scroll_down() - return None + return self._scroll_down() elif mouse_event.event_type == MouseEventType.SCROLL_UP: - self._scroll_up() - return None + return self._scroll_up() return NotImplemented - def _scroll_down(self) -> None: + def _scroll_down(self) -> "NotImplementedOrNone": "Scroll window down." info = self.render_info if info is None: - return + return NotImplemented if self.vertical_scroll < info.content_height - info.window_height: if info.cursor_position.y <= info.configured_scroll_offsets.top: self.content.move_cursor_down() - self.vertical_scroll += 1 + return None + + return NotImplemented - def _scroll_up(self) -> None: + def _scroll_up(self) -> "NotImplementedOrNone": "Scroll window up." info = self.render_info if info is None: - return + return NotImplemented if info.vertical_scroll > 0: # TODO: not entirely correct yet in case of line wrapping and long lines. @@ -2588,8 +2588,10 @@ def _scroll_up(self) -> None: >= info.window_height - 1 - info.configured_scroll_offsets.bottom ): self.content.move_cursor_up() - self.vertical_scroll -= 1 + return None + + return NotImplemented def get_key_bindings(self) -> KeyBindingsBase | None: return self.content.get_key_bindings() diff --git a/src/prompt_toolkit/layout/controls.py b/src/prompt_toolkit/layout/controls.py index c30c0effa..954c4bab6 100644 --- a/src/prompt_toolkit/layout/controls.py +++ b/src/prompt_toolkit/layout/controls.py @@ -477,7 +477,7 @@ def create_content(self, width: int, height: int) -> UIContent: def get_line(i: int) -> StyleAndTextTuples: return [] - return UIContent(get_line=get_line, line_count=100**100) # Something very big. + return UIContent(get_line=get_line, line_count=1) def is_focusable(self) -> bool: return False From 6f7b22b96bacb65c2866370de7319e5f7b191579 Mon Sep 17 00:00:00 2001 From: Josiah Outram Halstead Date: Wed, 17 Jan 2024 19:01:23 +0000 Subject: [PATCH 2/2] Fix type annotations --- src/prompt_toolkit/layout/containers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/prompt_toolkit/layout/containers.py b/src/prompt_toolkit/layout/containers.py index ee7140179..a0d1e0058 100644 --- a/src/prompt_toolkit/layout/containers.py +++ b/src/prompt_toolkit/layout/containers.py @@ -2559,7 +2559,7 @@ def _mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone: return NotImplemented - def _scroll_down(self) -> "NotImplementedOrNone": + def _scroll_down(self) -> NotImplementedOrNone: "Scroll window down." info = self.render_info @@ -2574,7 +2574,7 @@ def _scroll_down(self) -> "NotImplementedOrNone": return NotImplemented - def _scroll_up(self) -> "NotImplementedOrNone": + def _scroll_up(self) -> NotImplementedOrNone: "Scroll window up." info = self.render_info