Add container 'Center'.

This container will centre children horizontally.

Related issues: #1957.
This commit is contained in:
Rodrigo Girão Serrão
2023-03-09 15:20:36 +00:00
parent be41797a8d
commit f91750ed3d
3 changed files with 30 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added ### Added
- Added `HorizontalScroll` https://github.com/Textualize/textual/issues/1957 - Added `HorizontalScroll` https://github.com/Textualize/textual/issues/1957
- Added `Center` https://github.com/Textualize/textual/issues/1957
## [0.14.0] - 2023-03-09 ## [0.14.0] - 2023-03-09

View File

@@ -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): class Grid(Widget):
"""A container widget with grid alignment.""" """A container widget with grid alignment."""

View File

@@ -1,12 +1,12 @@
"""Test basic functioning of some containers.""" """Test basic functioning of some containers."""
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.containers import Horizontal, HorizontalScroll from textual.containers import Center, Horizontal, HorizontalScroll
from textual.widgets import Label from textual.widgets import Label
async def test_horizontal_vs_horizontalscroll_scrolling(): 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]): class HorizontalsApp(App[None]):
CSS = """ CSS = """
@@ -32,3 +32,18 @@ async def test_horizontal_vs_horizontalscroll_scrolling():
assert horizontal.size.height == horizontal_scroll.size.height assert horizontal.size.height == horizontal_scroll.size.height
assert horizontal.scrollbars_enabled == (False, False) assert horizontal.scrollbars_enabled == (False, False)
assert horizontal_scroll.scrollbars_enabled == (False, True) 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