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

Scroll sensitivity #1689

Merged
merged 13 commits into from
Jan 30, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Added the coroutines `Animator.wait_until_complete` and `pilot.wait_for_scheduled_animations` that allow waiting for all current and scheduled animations https://github.com/Textualize/textual/issues/1658
- Added the method `Animator.is_being_animated` that checks if an attribute of an object is being animated or is scheduled for animation
- Added App.scroll_sensitivity_x and App.scroll_sensitivity_y to adjust how many lines the scroll wheel moves the scroll position https://github.com/Textualize/textual/issues/928
- Added Shift+scroll wheel and ctrl+scroll wheel to scroll horizontally

### Changed

Expand Down
7 changes: 7 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ def __init__(
self.css_path = css_paths
self._registry: WeakSet[DOMNode] = WeakSet()

# Sensitivity on X is double the sensitivity on Y to account for
# cells being twice as tall as wide
self.scroll_sensitivity_x: float = 4.0
"""Number of columns to scroll in the X direction with wheel or trackpad."""
self.scroll_sensitivity_y: float = 2.0
"""Number of lines to scroll in the Y direction with wheel or trackpad."""

self._installed_screens: WeakValueDictionary[
str, Screen | Callable[[], Screen]
] = WeakValueDictionary()
Expand Down
36 changes: 23 additions & 13 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ def scroll_left(

"""
return self.scroll_to(
x=self.scroll_target_x - 1,
x=self.scroll_target_x - self.app.scroll_sensitivity_x,
animate=animate,
speed=speed,
duration=duration,
Expand Down Expand Up @@ -1609,7 +1609,7 @@ def scroll_right(

"""
return self.scroll_to(
x=self.scroll_target_x + 1,
x=self.scroll_target_x + self.app.scroll_sensitivity_x,
animate=animate,
speed=speed,
duration=duration,
Expand Down Expand Up @@ -1641,7 +1641,7 @@ def scroll_down(

"""
return self.scroll_to(
y=self.scroll_target_y + 1,
y=self.scroll_target_y + self.app.scroll_sensitivity_y,
animate=animate,
speed=speed,
duration=duration,
Expand Down Expand Up @@ -1673,7 +1673,7 @@ def scroll_up(

"""
return self.scroll_to(
y=self.scroll_target_y - 1,
y=self.scroll_target_y - +self.app.scroll_sensitivity_y,
animate=animate,
speed=speed,
duration=duration,
Expand Down Expand Up @@ -2480,15 +2480,25 @@ def _on_descendant_focus(self, event: events.DescendantBlur) -> None:
if self._has_focus_within:
self.app.update_styles(self)

def _on_mouse_scroll_down(self, event) -> None:
if self.allow_vertical_scroll:
if self.scroll_down(animate=False):
event.stop()

def _on_mouse_scroll_up(self, event) -> None:
if self.allow_vertical_scroll:
if self.scroll_up(animate=False):
event.stop()
def _on_mouse_scroll_down(self, event: events.MouseScrollDown) -> None:
if event.ctrl or event.shift:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to steal both control and shift here? I forget which one is usually used for this kind of behaviour.

If we steal both, users wouldn't be able to do something like "hold shift and use mouse wheel to zoom instead of scroll", would they?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it would be shift+mouse wheel only, but the problem is that iTerm doesn't report shift when pressed with the mouse wheel. Kitty does. I added both shift and ctrl so there is at least one way of horizontal scrolling.

if self.allow_horizontal_scroll:
if self.scroll_right(animate=False):
event.stop()
else:
if self.allow_vertical_scroll:
if self.scroll_down(animate=False):
event.stop()

def _on_mouse_scroll_up(self, event: events.MouseScrollUp) -> None:
if event.ctrl or event.shift:
if self.allow_horizontal_scroll:
if self.scroll_left(animate=False):
event.stop()
else:
if self.allow_vertical_scroll:
if self.scroll_up(animate=False):
event.stop()

def _on_scroll_to(self, message: ScrollTo) -> None:
if self._allow_scroll:
Expand Down
Loading