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

Option to ensure scroll_to_center doesn't scroll so as to hide the top left corner of the widget #2682

Merged
merged 5 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `work` decorator accepts `description` parameter to add debug string https://github.com/Textualize/textual/issues/2597
- Added `SelectionList` widget https://github.com/Textualize/textual/pull/2652
- `App.AUTO_FOCUS` to set auto focus on all screens https://github.com/Textualize/textual/issues/2594
- Option to `scroll_to_center` to ensure we don't scroll such that the top left corner of the widget is not visible https://github.com/Textualize/textual/pull/2682
- `Suggester` API to compose with widgets for automatic suggestions https://github.com/Textualize/textual/issues/2330
- `SuggestFromList` class to let widgets get completions from a fixed set of options https://github.com/Textualize/textual/pull/2604
- `Input` has a new component class `input--suggestion` https://github.com/Textualize/textual/pull/2604
Expand Down
2 changes: 1 addition & 1 deletion src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
def scroll_to_center(widget: Widget) -> None:
"""Scroll to center (after a refresh)."""
if widget.has_focus and not self.screen.can_view(widget):
self.screen.scroll_to_center(widget)
self.screen.scroll_to_center(widget, origin_visible=True)

self.call_after_refresh(scroll_to_center, widget)
widget.post_message(events.Focus())
Expand Down
40 changes: 27 additions & 13 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ def scroll_visible(
force=force,
)

async def _scroll_to_center_of(
async def _scroll_widget_to_center_of_self(
self,
widget: Widget,
animate: bool = True,
Expand All @@ -2398,8 +2398,11 @@ async def _scroll_to_center_of(
duration: float | None = None,
easing: EasingFunction | str | None = None,
force: bool = False,
origin_visible: bool = False,
) -> None:
"""Scroll a widget to the center of this container.
"""Scroll a widget to the center of this container. Note that this may
result in more than one container scrolling, since multiple containers
might be encountered on the path from `widget` to `self`.

Args:
widget: The widget to center.
Expand All @@ -2408,6 +2411,7 @@ async def _scroll_to_center_of(
duration: Duration of animation, if `animate` is `True` and `speed` is `None`.
easing: An easing method for the scrolling animation.
force: Force scrolling even when prohibited by overflow styling.
origin_visible: Ensure that the top left corner of the widget remains visible after the scroll.
"""

central_point = Offset(
Expand All @@ -2418,15 +2422,19 @@ async def _scroll_to_center_of(
container = widget.parent
while isinstance(container, Widget) and widget is not self:
container_virtual_region = container.virtual_region
# The region we want to scroll to must be centered around the central point.
# We make it as big as possible because `scroll_to_region` scrolls as little
# as possible.
target_region = Region(
central_point.x - container_virtual_region.width // 2,
central_point.y - container_virtual_region.height // 2,
container_virtual_region.width,
container_virtual_region.height,
)
if origin_visible and widget.region.height > container.region.height:
target_region = widget.virtual_region
else:
# The region we want to scroll to must be centered around the central point.
# We make it as big as possible because `scroll_to_region` scrolls as little
# as possible.
target_region = Region(
central_point.x - container_virtual_region.width // 2,
central_point.y - container_virtual_region.height // 2,
container_virtual_region.width,
container_virtual_region.height,
)

scroll = container.scroll_to_region(
target_region,
animate=animate,
Expand Down Expand Up @@ -2463,25 +2471,31 @@ def scroll_to_center(
duration: float | None = None,
easing: EasingFunction | str | None = None,
force: bool = False,
origin_visible: bool = False,
) -> None:
"""Scroll this widget to the center of the screen.
"""Scroll this widget to the center of self.

The center of the widget will be scrolled to the center of the container.

Args:
widget: The widget to scroll to the center of self.
animate: Whether to animate the scroll.
speed: Speed of scroll if animate is `True`; or `None` to use `duration`.
duration: Duration of animation, if `animate` is `True` and `speed` is `None`.
easing: An easing method for the scrolling animation.
force: Force scrolling even when prohibited by overflow styling.
origin_visible: Ensure that the top left corner of the widget remains visible after the scroll.
"""

self.call_after_refresh(
self._scroll_to_center_of,
self._scroll_widget_to_center_of_self,
widget=widget,
animate=animate,
speed=speed,
duration=duration,
easing=easing,
force=force,
origin_visible=origin_visible,
)

def can_view(self, widget: Widget) -> bool:
Expand Down