From e111449856da0006d7b53051bad029c959da9201 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:24:13 +0000 Subject: [PATCH] Add container 'Middle'. Related issues: #1957. --- CHANGELOG.md | 1 + src/textual/containers.py | 12 ++++++++++++ tests/test_containers.py | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e064974..2670f3f32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `HorizontalScroll` https://github.com/Textualize/textual/issues/1957 - Added `Center` https://github.com/Textualize/textual/issues/1957 +- Added `Middle` 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 a1836aa85..156965ff2 100644 --- a/src/textual/containers.py +++ b/src/textual/containers.py @@ -61,6 +61,18 @@ class Center(Widget): """ +class Middle(Widget): + """A container widget which aligns children vertically in the middle.""" + + DEFAULT_CSS = """ + Middle { + align-vertical: middle; + height: 100%; + width: auto; + } + """ + + class Grid(Widget): """A container widget with grid alignment.""" diff --git a/tests/test_containers.py b/tests/test_containers.py index 7dde9446e..866e62800 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -1,7 +1,7 @@ """Test basic functioning of some containers.""" from textual.app import App, ComposeResult -from textual.containers import Center, Horizontal, HorizontalScroll +from textual.containers import Center, Horizontal, HorizontalScroll, Middle from textual.widgets import Label @@ -47,3 +47,18 @@ async def test_center_container(): center = app.query_one(Center) assert center.size.width == app.size.width assert center.size.height == 3 + + +async def test_middle_container(): + """Check the size of the container `Middle`.""" + + class MiddleApp(App[None]): + def compose(self) -> ComposeResult: + with Middle(): + yield Label("1234") + + app = MiddleApp() + async with app.run_test(): + middle = app.query_one(Middle) + assert middle.size.width == 4 + assert middle.size.height == app.size.height