From 5611b1f9269bd51f9473ecc73e1c55aecdb72699 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 24 Sep 2024 14:58:21 +0100 Subject: [PATCH] limit scrolling by axis --- CHANGELOG.md | 10 ++++++++++ src/textual/widget.py | 10 ++++++++-- src/textual/widgets/_tree.py | 7 ++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4b991648b..c32cdc1104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/textual/widget.py b/src/textual/widget.py index adb26f596e..b7bcfa7df3 100644 --- a/src/textual/widget.py +++ b/src/textual/widget.py @@ -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. @@ -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. @@ -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, diff --git a/src/textual/widgets/_tree.py b/src/textual/widgets/_tree.py index 210d193d28..4992327a3d 100644 --- a/src/textual/widgets/_tree.py +++ b/src/textual/widgets/_tree.py @@ -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(