Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll to center when tab is clicked. #2276

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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