Skip to content

Commit

Permalink
Merge pull request #2276 from Textualize/tabs-scroll-to-center
Browse files Browse the repository at this point in the history
Scroll to center when tab is clicked.
  • Loading branch information
rodrigogiraoserrao authored Apr 13, 2023
2 parents c249548 + 5104cbe commit e32cdbb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 3 additions & 2 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,7 @@ async def _scroll_to_center_of(

def scroll_to_center(
self,
widget: Widget,
animate: bool = True,
*,
speed: float | None = None,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widgets/_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshot_tests/snapshot_apps/scroll_to_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit e32cdbb

Please sign in to comment.