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

limit scrolling by axis #5047

Merged
merged 2 commits into from
Sep 24, 2024
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Add

- Added `x_axis` and `y_axis` parameters to `Widget.scroll_to_region` https://github.com/Textualize/textual/pull/5047

### Changed

- Tree will no longer scroll the X axis when moving the cursor https://github.com/Textualize/textual/pull/5047

## [0.80.1] - 2024-09-24

### Fixed
Expand Down
10 changes: 8 additions & 2 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2874,6 +2874,8 @@ def scroll_to_region(
force: bool = False,
on_complete: CallbackType | None = None,
level: AnimationLevel = "basic",
x_axis: bool = True,
y_axis: bool = True,
) -> Offset:
"""Scrolls a given region in to view, if required.

Expand All @@ -2892,6 +2894,8 @@ def scroll_to_region(
force: Force scrolling even when prohibited by overflow styling.
on_complete: A callable to invoke when the animation is finished.
level: Minimum level required for the animation to take place (inclusive).
x_axis: Allow scrolling on X axis?
y_axis: Allow scrolling on Y axis?

Returns:
The distance that was scrolled.
Expand Down Expand Up @@ -2938,11 +2942,13 @@ def clamp_delta(delta: Offset) -> Offset:
delta = Offset(delta.x, 0)

if delta:
delta_x = delta.x if x_axis else 0
delta_y = delta.y if y_axis else 0
if speed is None and duration is None:
duration = 0.2
self.scroll_relative(
delta.x or None,
delta.y or None,
delta_x or None,
delta_y or None,
animate=animate,
speed=speed,
duration=duration,
Expand Down
7 changes: 6 additions & 1 deletion src/textual/widgets/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,12 @@ def scroll_to_line(self, line: int, animate: bool = True) -> None:
region = self._get_label_region(line)
if region is not None:
self.scroll_to_region(
region, animate=animate, force=True, center=self.center_scroll
region,
animate=animate,
force=True,
center=self.center_scroll,
origin_visible=False,
x_axis=False, # Scrolling the X axis is quite jarring, and rarely necessary
)

def scroll_to_node(
Expand Down
Loading