-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
1 parent
a37eac3
commit a465f5c
Showing
6 changed files
with
243 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,29 @@ | ||
from textual.geometry import Size | ||
from textual.layouts.horizontal import HorizontalLayout | ||
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.""" | ||
|
||
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 | ||
import pytest | ||
|
||
def get_content_height(self, container: Size, viewport: Size, width: int) -> int: | ||
return self.content_height | ||
|
||
|
||
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) | ||
from textual.app import App, ComposeResult | ||
from textual.containers import Horizontal | ||
from textual.widget import Widget | ||
|
||
|
||
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 | ||
@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 | ||
|
||
app = HorizontalAutoWidth() | ||
async with app.run_test(): | ||
yield app | ||
|
||
|
||
async def test_horizontal_get_content_width(app): | ||
size = app.screen.size | ||
width = app.horizontal.get_content_width(size, size) | ||
assert width == 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/snapshot_tests/snapshot_apps/horizontal_auto_width.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.widget { | ||
background: olivedrab; | ||
width: 10; | ||
margin: 1; | ||
} | ||
|
||
#dock-1 { | ||
dock: left; | ||
background: dodgerblue; | ||
width: 5; | ||
} | ||
|
||
#dock-2 { | ||
dock: left; | ||
background: mediumvioletred; | ||
margin: 3; | ||
width: 20; | ||
} | ||
|
||
#horizontal { | ||
width: auto; | ||
height: auto; | ||
background: darkslateblue; | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/snapshot_tests/snapshot_apps/horizontal_auto_width.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.containers import Horizontal | ||
from textual.widgets import Static | ||
|
||
|
||
class HorizontalAutoWidth(App): | ||
""" | ||
Checks that the auto width of the parent Horizontal is correct. | ||
""" | ||
def compose(self) -> ComposeResult: | ||
yield Horizontal( | ||
Static("Docked left 1", id="dock-1"), | ||
Static("Docked left 2", id="dock-2"), | ||
Static("Widget 1", classes="widget"), | ||
Static("Widget 2", classes="widget"), | ||
id="horizontal", | ||
) | ||
|
||
|
||
app = HorizontalAutoWidth(css_path="horizontal_auto_width.css") | ||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters