Horizontal width auto (#1155)

* Improvements to width:auto HorizontalLayout

* Fix HorizontalLayout.get_content_width

* Horizontal width auto improvement

* Removing some printxz

* Update snapshot for horizontal layout width auto dock
This commit is contained in:
darrenburns
2022-11-16 14:49:52 +00:00
committed by GitHub
parent a37eac3cad
commit a465f5c236
6 changed files with 240 additions and 55 deletions

View File

@@ -1,46 +1,29 @@
from textual.geometry import Size
from textual.layouts.horizontal import HorizontalLayout
import pytest
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widget import Widget
class SizedWidget(Widget):
"""Simple Widget wrapped allowing you to modify the return values for
get_content_width and get_content_height via the constructor."""
@pytest.fixture
async def app():
class HorizontalAutoWidth(App):
def compose(self) -> ComposeResult:
child1 = Widget(id="child1")
child1.styles.width = 4
child2 = Widget(id="child2")
child2.styles.width = 6
child3 = Widget(id="child3")
child3.styles.width = 5
self.horizontal = Horizontal(child1, child2, child3)
yield self.horizontal
def __init__(
self,
*children: Widget,
content_width: int = 10,
content_height: int = 5,
):
super().__init__(*children)
self.content_width = content_width
self.content_height = content_height
def get_content_width(self, container: Size, viewport: Size) -> int:
return self.content_width
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return self.content_height
app = HorizontalAutoWidth()
async with app.run_test():
yield app
CHILDREN = [
SizedWidget(content_width=10, content_height=5),
SizedWidget(content_width=4, content_height=2),
SizedWidget(content_width=12, content_height=3),
]
def test_horizontal_get_content_width():
parent = Widget(*CHILDREN)
layout = HorizontalLayout()
width = layout.get_content_width(widget=parent, container=Size(), viewport=Size())
assert width == sum(child.content_width for child in CHILDREN)
def test_horizontal_get_content_width_no_children():
parent = Widget()
layout = HorizontalLayout()
container_size = Size(24, 24)
width = layout.get_content_width(widget=parent, container=container_size, viewport=Size())
assert width == container_size.width
async def test_horizontal_get_content_width(app):
size = app.screen.size
width = app.horizontal.get_content_width(size, size)
assert width == 15