Skip to content

Commit

Permalink
Move scroll navigation key bindings out of widget and into own container
Browse files Browse the repository at this point in the history
The idea here is that not every widget will scroll, and as such not every
widget needs to have default bindings for calling the scrolling methods.
Generally scrolling is something done in a container.

These days we have *Scroll containers. As such it makes sense to introduce
the bindings in a common parent class for those containers.

This commit moves the binding from widget and creates that common parent
class, and then has HorizontalScroll and VerticalScroll inherit from it.

This is, it should be noted, a breaking change. Any code that creates a
scrolling widget that assumes that the bindings are just there, where that
widget doesn't inherit either from HorizontalScroll or VerticalScroll, will
suddenly find that scrolling with the keyboard is no longer possible.

See Textualize#2332.
  • Loading branch information
davep committed Apr 20, 2023
1 parent 0e8e232 commit 4942293
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
36 changes: 34 additions & 2 deletions src/textual/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"""

from __future__ import annotations

from typing import ClassVar

from .binding import Binding, BindingType
from .widget import Widget


Expand All @@ -19,6 +23,34 @@ class Container(Widget):
"""


class ScrollableContainer(Widget):
"""Base container widget that binds navigation keys for scrolling."""

BINDINGS: ClassVar[list[BindingType]] = [
Binding("up", "scroll_up", "Scroll Up", show=False),
Binding("down", "scroll_down", "Scroll Down", show=False),
Binding("left", "scroll_left", "Scroll Up", show=False),
Binding("right", "scroll_right", "Scroll Right", show=False),
Binding("home", "scroll_home", "Scroll Home", show=False),
Binding("end", "scroll_end", "Scroll End", show=False),
Binding("pageup", "page_up", "Page Up", show=False),
Binding("pagedown", "page_down", "Page Down", show=False),
]
"""Keyboard bindings for scrollable containers.
| Key(s) | Description |
| :- | :- |
| up | Scroll up, if vertical scrolling is available. |
| down | Scroll down, if vertical scrolling is available. |
| left | Scroll left, if horizontal scrolling is available. |
| right | Scroll right, if horizontal scrolling is available. |
| home | Scroll to the home position, if scrolling is available. |
| end | Scroll to the end position, if scrolling is available. |
| pageup | Scroll up one page, if vertical scrolling is available. |
| pagedown | Scroll down one page, if vertical scrolling is available. |
"""


class Vertical(Widget):
"""A container which arranges children vertically."""

Expand All @@ -31,7 +63,7 @@ class Vertical(Widget):
"""


class VerticalScroll(Widget, can_focus=True):
class VerticalScroll(ScrollableContainer, can_focus=True):
"""A container which arranges children vertically, with an automatic vertical scrollbar."""

DEFAULT_CSS = """
Expand All @@ -55,7 +87,7 @@ class Horizontal(Widget):
"""


class HorizontalScroll(Widget, can_focus=True):
class HorizontalScroll(ScrollableContainer, can_focus=True):
"""A container which arranges children horizontally, with an automatic horizontal scrollbar."""

DEFAULT_CSS = """
Expand Down
12 changes: 0 additions & 12 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from ._styles_cache import StylesCache
from .actions import SkipAction
from .await_remove import AwaitRemove
from .binding import Binding
from .box_model import BoxModel
from .css.query import NoMatches, WrongType
from .css.scalar import ScalarOffset
Expand Down Expand Up @@ -238,17 +237,6 @@ class Widget(DOMNode):
"""

BINDINGS = [
Binding("up", "scroll_up", "Scroll Up", show=False),
Binding("down", "scroll_down", "Scroll Down", show=False),
Binding("left", "scroll_left", "Scroll Up", show=False),
Binding("right", "scroll_right", "Scroll Right", show=False),
Binding("home", "scroll_home", "Scroll Home", show=False),
Binding("end", "scroll_end", "Scroll End", show=False),
Binding("pageup", "page_up", "Page Up", show=False),
Binding("pagedown", "page_down", "Page Down", show=False),
]

DEFAULT_CSS = """
Widget{
scrollbar-background: $panel-darken-1;
Expand Down

0 comments on commit 4942293

Please sign in to comment.