From 90dce06eaef107f0b7b274784da50af2faee7c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 9 Mar 2023 14:35:49 +0000 Subject: [PATCH] Add 'HorizontalScroll'. Related issues: #1957. --- CHANGELOG.md | 1 + src/textual/containers.py | 12 ++++++++++++ tests/test_containers.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/test_containers.py diff --git a/CHANGELOG.md b/CHANGELOG.md index f665c00e4..b403661c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Breaking change: Added `toggle_button` attribute to RadioButton and Checkbox events, replaces `input` https://github.com/Textualize/textual/pull/1940 - A percentage alpha can now be applied to a border https://github.com/Textualize/textual/issues/1863 - Added `Color.multiply_alpha`. +- Added `HorizontalScroll` https://github.com/Textualize/textual/issues/1957 ### Fixed diff --git a/src/textual/containers.py b/src/textual/containers.py index 0d45e8cda..946ab52e1 100644 --- a/src/textual/containers.py +++ b/src/textual/containers.py @@ -37,6 +37,18 @@ class Horizontal(Widget): """ +class HorizontalScroll(Widget): + """A container widget which aligns children horizontally and overflows if needed.""" + + DEFAULT_CSS = """ + HorizontalScroll { + height: 1fr; + layout: horizontal; + overflow-x: auto; + } + """ + + class Grid(Widget): """A container widget with grid alignment.""" diff --git a/tests/test_containers.py b/tests/test_containers.py new file mode 100644 index 000000000..d84941082 --- /dev/null +++ b/tests/test_containers.py @@ -0,0 +1,34 @@ +"""Test basic functioning of some containers.""" + +from textual.app import App, ComposeResult +from textual.containers import Horizontal, HorizontalScroll +from textual.widgets import Label + + +async def test_horizontal_vs_horizontalscroll_scrolling(): + """Check the default scrollbar behaviours for Horizontal and HorizontalScroll.""" + + class HorizontalsApp(App[None]): + CSS = """ + Screen { + layout: vertical; + } + """ + + def compose(self) -> ComposeResult: + with Horizontal(): + for _ in range(10): + yield Label("How is life going? " * 3 + " | ") + with HorizontalScroll(): + for _ in range(10): + yield Label("How is life going? " * 3 + " | ") + + WIDTH = 80 + HEIGHT = 24 + app = HorizontalsApp() + async with app.run_test(size=(WIDTH, HEIGHT)): + horizontal = app.query_one(Horizontal) + horizontal_scroll = app.query_one(HorizontalScroll) + assert horizontal.size.height == horizontal_scroll.size.height + assert horizontal.scrollbars_enabled == (False, False) + assert horizontal_scroll.scrollbars_enabled == (False, True)