From f91750ed3d45c416a6da42ddb4c2f63242c53d6f 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 15:20:36 +0000 Subject: [PATCH] Add container 'Center'. This container will centre children horizontally. Related issues: #1957. --- CHANGELOG.md | 1 + src/textual/containers.py | 12 ++++++++++++ tests/test_containers.py | 19 +++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce0bf2b8a6..59e0649743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Added `HorizontalScroll` https://github.com/Textualize/textual/issues/1957 +- Added `Center` https://github.com/Textualize/textual/issues/1957 ## [0.14.0] - 2023-03-09 diff --git a/src/textual/containers.py b/src/textual/containers.py index 946ab52e1d..a1836aa852 100644 --- a/src/textual/containers.py +++ b/src/textual/containers.py @@ -49,6 +49,18 @@ class HorizontalScroll(Widget): """ +class Center(Widget): + """A container widget which centers children horizontally.""" + + DEFAULT_CSS = """ + Center { + align-horizontal: center; + width: 100%; + height: auto; + } + """ + + class Grid(Widget): """A container widget with grid alignment.""" diff --git a/tests/test_containers.py b/tests/test_containers.py index d84941082a..7dde9446e7 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -1,12 +1,12 @@ """Test basic functioning of some containers.""" from textual.app import App, ComposeResult -from textual.containers import Horizontal, HorizontalScroll +from textual.containers import Center, Horizontal, HorizontalScroll from textual.widgets import Label async def test_horizontal_vs_horizontalscroll_scrolling(): - """Check the default scrollbar behaviours for Horizontal and HorizontalScroll.""" + """Check the default scrollbar behaviours for `Horizontal` and `HorizontalScroll`.""" class HorizontalsApp(App[None]): CSS = """ @@ -32,3 +32,18 @@ def compose(self) -> ComposeResult: assert horizontal.size.height == horizontal_scroll.size.height assert horizontal.scrollbars_enabled == (False, False) assert horizontal_scroll.scrollbars_enabled == (False, True) + + +async def test_center_container(): + """Check the size of the container `Center`.""" + + class CenterApp(App[None]): + def compose(self) -> ComposeResult: + with Center(): + yield Label("<>\n<>\n<>") + + app = CenterApp() + async with app.run_test(): + center = app.query_one(Center) + assert center.size.width == app.size.width + assert center.size.height == 3