From 663b09736e3e00fca38a91ccb95eafb8294423cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Wed, 12 Apr 2023 22:25:33 +0100 Subject: [PATCH 1/2] Scroll to center when tab is clicked. Related comment: https://github.com/Textualize/textual/issues/2256#issuecomment-1505551728 --- src/textual/widget.py | 5 +++-- src/textual/widgets/_tabs.py | 4 ++-- tests/snapshot_tests/snapshot_apps/scroll_to_center.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/textual/widget.py b/src/textual/widget.py index a5a422cb1a..b0cf8af97d 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -2475,6 +2475,7 @@ async def _scroll_to_center_of( def scroll_to_center( self, + widget: Widget, animate: bool = True, *, speed: float | None = None, @@ -2493,8 +2494,8 @@ def scroll_to_center( """ self.call_after_refresh( - self.screen._scroll_to_center_of, - widget=self, + self._scroll_to_center_of, + widget=widget, animate=animate, speed=speed, duration=duration, diff --git a/src/textual/widgets/_tabs.py b/src/textual/widgets/_tabs.py index 3191b1e47c..cdfaf2ac11 100644 --- a/src/textual/widgets/_tabs.py +++ b/src/textual/widgets/_tabs.py @@ -449,7 +449,7 @@ def _activate_tab(self, tab: Tab) -> None: self.query("#tabs-list Tab.-active").remove_class("-active") tab.add_class("-active") self.active = tab.id or "" - self.query_one("#tabs-scroll").scroll_to_widget(tab, force=True) + self.query_one("#tabs-scroll").scroll_to_center(tab, force=True) def _on_underline_clicked(self, event: Underline.Clicked) -> None: """The underline was clicked. @@ -471,7 +471,7 @@ def _scroll_active_tab(self) -> None: """Scroll the active tab into view.""" if self.active_tab: try: - self.query_one("#tabs-scroll").scroll_to_widget( + self.query_one("#tabs-scroll").scroll_to_center( self.active_tab, force=True ) except NoMatches: diff --git a/tests/snapshot_tests/snapshot_apps/scroll_to_center.py b/tests/snapshot_tests/snapshot_apps/scroll_to_center.py index fdd616cb6d..269775fed3 100644 --- a/tests/snapshot_tests/snapshot_apps/scroll_to_center.py +++ b/tests/snapshot_tests/snapshot_apps/scroll_to_center.py @@ -33,7 +33,7 @@ def compose(self) -> ComposeResult: yield Label(("SPAM\n" * 59)[:-1]) def key_s(self) -> None: - self.query_one("#bullseye").scroll_to_center() + self.screen.scroll_to_center(self.query_one("#bullseye")) if __name__ == "__main__": From 5104cbe501da144ab9e696f6a14936fb1b947c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Wed, 12 Apr 2023 22:28:48 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dbacc28a0..aa2529ba84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,11 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Changed signature of Driver. Technically a breaking change, but unlikely to affect anyone. - Breaking change: Timer.start is now private, and returns No +- A clicked tab will now be scrolled to the center of its tab container https://github.com/Textualize/textual/pull/2276 ### Added - Added `DataTable.remove_row` method https://github.com/Textualize/textual/pull/2253 -- `Widget.scroll_to_center` now scrolls the widget to the center of the screen https://github.com/Textualize/textual/pull/2255 +- `Widget.scroll_to_center` method to scroll children to the center of container widget https://github.com/Textualize/textual/pull/2255 and https://github.com/Textualize/textual/pull/2276 - Added `TabActivated` message to `TabbedContent` https://github.com/Textualize/textual/pull/2260