From 8b2f056d66b82596c40121756a4aa56ee5259d1d Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 8 Dec 2024 12:36:46 +0000 Subject: [PATCH 1/3] Fix alignment in auto container --- src/textual/layout.py | 13 +------ .../snapshot_apps/max_height_100.py | 5 ++- tests/snapshot_tests/test_snapshots.py | 39 ++++++++++++++++++- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/textual/layout.py b/src/textual/layout.py index a3ef9ec006..ecbe34e365 100644 --- a/src/textual/layout.py +++ b/src/textual/layout.py @@ -259,18 +259,7 @@ def get_content_height( if not widget._nodes: height = 0 else: - # Use a height of zero to ignore relative heights - styles_height = widget.styles.height - if widget._parent and len(widget._nodes) == 1: - # If it is an only child with height auto we want it to expand - height = ( - container.height - if styles_height is not None and styles_height.is_auto - else 0 - ) - else: - height = 0 - arrangement = widget._arrange(Size(width, height)) + arrangement = widget._arrange(Size(width, 0)) height = arrangement.total_region.bottom return height diff --git a/tests/snapshot_tests/snapshot_apps/max_height_100.py b/tests/snapshot_tests/snapshot_apps/max_height_100.py index 83bc94386a..95790d7761 100644 --- a/tests/snapshot_tests/snapshot_apps/max_height_100.py +++ b/tests/snapshot_tests/snapshot_apps/max_height_100.py @@ -3,9 +3,12 @@ class HappyDataTableFunApp(App[None]): - """The DataTable should expand as if it has height 1fr.""" + """The DataTable should expand to full the screen and show a horizontal scrollbar.""" CSS = """ + #s { + max-height: 100%; + } DataTable { max-height: 100%; } diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index c2d7b8856b..96aba055c0 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -1385,7 +1385,7 @@ def test_vertical_max_height(snap_compare): def test_max_height_100(snap_compare): - """Test vertical max height takes border in to account.""" + """Test a datatable with max height 100%.""" assert snap_compare(SNAPSHOT_APPS_DIR / "max_height_100.py") @@ -3074,3 +3074,40 @@ def compose(self): yield MainContainer() snap_compare(Test1()) + + +def test_auto_parent_with_alignment(snap_compare): + class Sidebar(Vertical): + DEFAULT_CSS = """ + Sidebar { + dock: right; + width: auto; + height: auto; + background: blue; + align-vertical: bottom; + + #contents { + width: auto; + height: auto; + background: red; + border: white; + } + } + """ + + def compose(self) -> ComposeResult: + with Vertical(id="contents"): + yield Button("Start") + yield Button("Stop") + + class FloatSidebarApp(App): + CSS = """ + Screen { + layers: base sidebar; + } + """ + + def compose(self) -> ComposeResult: + yield Sidebar() + + snap_compare(FloatSidebarApp()) From 97ae5ababf084644b26a49fa991e40040a8f5195 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 8 Dec 2024 12:41:30 +0000 Subject: [PATCH 2/3] snapshot --- CHANGELOG.md | 4 + .../test_auto_parent_with_alignment.svg | 155 ++++++++++++++++++ tests/snapshot_tests/test_snapshots.py | 2 +- 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/snapshot_tests/__snapshots__/test_snapshots/test_auto_parent_with_alignment.svg diff --git a/CHANGELOG.md b/CHANGELOG.md index 25fb5ae6e1..9cfe480f44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Change default quit key to `ctrl+q` https://github.com/Textualize/textual/pull/5352 - Changed delete line binding on TextArea to use `ctrl+shift+x` https://github.com/Textualize/textual/pull/5352 +### Fixed + +- Fixed issue with alignment in auto containers https://github.com/Textualize/textual/pull/5360 + ## [0.89.1] - 2024-11-05 ### Fixed diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots/test_auto_parent_with_alignment.svg b/tests/snapshot_tests/__snapshots__/test_snapshots/test_auto_parent_with_alignment.svg new file mode 100644 index 0000000000..b0626bddff --- /dev/null +++ b/tests/snapshot_tests/__snapshots__/test_snapshots/test_auto_parent_with_alignment.svg @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + FloatSidebarApp + + + + + + + + + + ┌────────────────┐ +▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + Start  +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ +▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔ + Stop  +▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ +└────────────────┘ + + + + + + + + + + + + + + + + + + + diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 96aba055c0..a6723b8be2 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -3080,7 +3080,7 @@ def test_auto_parent_with_alignment(snap_compare): class Sidebar(Vertical): DEFAULT_CSS = """ Sidebar { - dock: right; + dock: right; # Not strictly required to replicate the issue width: auto; height: auto; background: blue; From 455c085b5392e47aa17cbe7ca6250f243928ce4b Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 8 Dec 2024 14:34:08 +0000 Subject: [PATCH 3/3] simplify --- src/textual/layout.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/textual/layout.py b/src/textual/layout.py index ecbe34e365..5f55448ca6 100644 --- a/src/textual/layout.py +++ b/src/textual/layout.py @@ -256,11 +256,11 @@ def get_content_height( Returns: Content height (in lines). """ - if not widget._nodes: - height = 0 - else: + if widget._nodes: arrangement = widget._arrange(Size(width, 0)) height = arrangement.total_region.bottom + else: + height = 0 return height