Merge pull request #2276 from Textualize/tabs-scroll-to-center

Scroll to center when tab is clicked.
This commit is contained in:
Rodrigo Girão Serrão
2023-04-13 10:48:15 +01:00
committed by GitHub
4 changed files with 8 additions and 6 deletions

View File

@@ -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

View File

@@ -2475,6 +2475,7 @@ class Widget(DOMNode):
def scroll_to_center(
self,
widget: Widget,
animate: bool = True,
*,
speed: float | None = None,
@@ -2493,8 +2494,8 @@ class Widget(DOMNode):
"""
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,

View File

@@ -449,7 +449,7 @@ class Tabs(Widget, can_focus=True):
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 @@ class Tabs(Widget, can_focus=True):
"""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:

View File

@@ -33,7 +33,7 @@ class MyApp(App[None]):
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__":