Skip to content

Commit

Permalink
limit scrolling by axis
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Sep 24, 2024
1 parent 821316d commit 5611b1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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`

### Changed

- Tree will no longer scroll the X axis when moving the cursor

## [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

0 comments on commit 5611b1f

Please sign in to comment.