Skip to content

Commit

Permalink
Horizontal width auto (#1155)
Browse files Browse the repository at this point in the history
* 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
darrenburns authored Nov 16, 2022
1 parent a37eac3 commit a465f5c
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 58 deletions.
22 changes: 7 additions & 15 deletions src/textual/layouts/horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,12 @@ def get_content_width(self, widget: Widget, container: Size, viewport: Size) ->
Returns:
int: Width of the content.
"""
width: int | None = None
gutter_width = widget.gutter.width
for child in widget.displayed_children:
if not child.is_container:
child_width = (
child.get_content_width(container, viewport)
+ gutter_width
+ child.gutter.width
)
if width is None:
width = child_width
else:
width += child_width
if width is None:
if not widget.displayed_children:
width = container.width

else:
placements, *_ = widget._arrange(container)
width = max(
placement.region.right + placement.margin.right
for placement in placements
)
return width
69 changes: 26 additions & 43 deletions tests/layouts/test_horizontal.py
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
158 changes: 158 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -5849,6 +5849,164 @@

'''
# ---
# name: test_horizontal_layout_width_auto_dock
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io -->
<style>

@font-face {
font-family: "Fira Code";
src: local("FiraCode-Regular"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal;
font-weight: 400;
}
@font-face {
font-family: "Fira Code";
src: local("FiraCode-Bold"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold;
font-weight: 700;
}

.terminal-3689181897-matrix {
font-family: Fira Code, monospace;
font-size: 20px;
line-height: 24.4px;
font-variant-east-asian: full-width;
}

.terminal-3689181897-title {
font-size: 18px;
font-weight: bold;
font-family: arial;
}

.terminal-3689181897-r1 { fill: #e1f0ff }
.terminal-3689181897-r2 { fill: #c5c8c6 }
.terminal-3689181897-r3 { fill: #e1e1e1 }
.terminal-3689181897-r4 { fill: #ebf0e2 }
.terminal-3689181897-r5 { fill: #f7e0ef }
</style>

<defs>
<clipPath id="terminal-3689181897-clip-terminal">
<rect x="0" y="0" width="975.0" height="584.5999999999999" />
</clipPath>
<clipPath id="terminal-3689181897-line-0">
<rect x="0" y="1.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-1">
<rect x="0" y="25.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-2">
<rect x="0" y="50.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-3">
<rect x="0" y="74.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-4">
<rect x="0" y="99.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-5">
<rect x="0" y="123.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-6">
<rect x="0" y="147.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-7">
<rect x="0" y="172.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-8">
<rect x="0" y="196.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-9">
<rect x="0" y="221.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-10">
<rect x="0" y="245.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-11">
<rect x="0" y="269.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-12">
<rect x="0" y="294.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-13">
<rect x="0" y="318.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-14">
<rect x="0" y="343.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-15">
<rect x="0" y="367.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-16">
<rect x="0" y="391.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-17">
<rect x="0" y="416.3" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-18">
<rect x="0" y="440.7" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-19">
<rect x="0" y="465.1" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-20">
<rect x="0" y="489.5" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-21">
<rect x="0" y="513.9" width="976" height="24.65"/>
</clipPath>
<clipPath id="terminal-3689181897-line-22">
<rect x="0" y="538.3" width="976" height="24.65"/>
</clipPath>
</defs>

<rect fill="#292929" stroke="rgba(255,255,255,0.35)" stroke-width="1" x="1" y="1" width="992" height="633.6" rx="8"/><text class="terminal-3689181897-title" fill="#c5c8c6" text-anchor="middle" x="496" y="27">HorizontalAutoWidth</text>
<g transform="translate(26,22)">
<circle cx="0" cy="0" r="7" fill="#ff5f57"/>
<circle cx="22" cy="0" r="7" fill="#febc2e"/>
<circle cx="44" cy="0" r="7" fill="#28c840"/>
</g>

<g transform="translate(9, 41)" clip-path="url(#terminal-3689181897-clip-terminal)">
<rect fill="#1e90ff" x="0" y="1.5" width="61" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="61" y="1.5" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="597.8" y="1.5" width="378.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e90ff" x="0" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e90ff" x="24.4" y="25.9" width="36.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="61" y="25.9" width="268.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#6b8e23" x="329.4" y="25.9" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#6b8e23" x="427" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="451.4" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#6b8e23" x="463.6" y="25.9" width="97.6" height="24.65" shape-rendering="crispEdges"/><rect fill="#6b8e23" x="561.2" y="25.9" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="585.6" y="25.9" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="597.8" y="25.9" width="378.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e90ff" x="0" y="50.3" width="61" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="61" y="50.3" width="536.8" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="597.8" y="50.3" width="378.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e90ff" x="0" y="74.7" width="12.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e90ff" x="12.2" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#c71585" x="36.6" y="74.7" width="24.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#c71585" x="61" y="74.7" width="134.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#c71585" x="195.2" y="74.7" width="85.4" height="24.65" shape-rendering="crispEdges"/><rect fill="#483d8b" x="280.6" y="74.7" width="317.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="597.8" y="74.7" width="378.2" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="99.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="123.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="147.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="172.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="196.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="221.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="245.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="269.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="294.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="318.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="343.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="367.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="391.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="416.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="440.7" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="465.1" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="489.5" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="513.9" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="538.3" width="976" height="24.65" shape-rendering="crispEdges"/><rect fill="#1e1e1e" x="0" y="562.7" width="976" height="24.65" shape-rendering="crispEdges"/>
<g class="terminal-3689181897-matrix">
<text class="terminal-3689181897-r1" x="0" y="20" textLength="61" clip-path="url(#terminal-3689181897-line-0)">Docke</text><text class="terminal-3689181897-r2" x="976" y="20" textLength="12.2" clip-path="url(#terminal-3689181897-line-0)">
</text><text class="terminal-3689181897-r1" x="0" y="44.4" textLength="24.4" clip-path="url(#terminal-3689181897-line-1)">d&#160;</text><text class="terminal-3689181897-r4" x="329.4" y="44.4" textLength="97.6" clip-path="url(#terminal-3689181897-line-1)">Widget&#160;1</text><text class="terminal-3689181897-r4" x="463.6" y="44.4" textLength="97.6" clip-path="url(#terminal-3689181897-line-1)">Widget&#160;2</text><text class="terminal-3689181897-r2" x="976" y="44.4" textLength="12.2" clip-path="url(#terminal-3689181897-line-1)">
</text><text class="terminal-3689181897-r1" x="0" y="68.8" textLength="61" clip-path="url(#terminal-3689181897-line-2)">left&#160;</text><text class="terminal-3689181897-r2" x="976" y="68.8" textLength="12.2" clip-path="url(#terminal-3689181897-line-2)">
</text><text class="terminal-3689181897-r1" x="0" y="93.2" textLength="12.2" clip-path="url(#terminal-3689181897-line-3)">1</text><text class="terminal-3689181897-r5" x="36.6" y="93.2" textLength="24.4" clip-path="url(#terminal-3689181897-line-3)">Do</text><text class="terminal-3689181897-r5" x="61" y="93.2" textLength="134.2" clip-path="url(#terminal-3689181897-line-3)">cked&#160;left&#160;2</text><text class="terminal-3689181897-r2" x="976" y="93.2" textLength="12.2" clip-path="url(#terminal-3689181897-line-3)">
</text><text class="terminal-3689181897-r2" x="976" y="117.6" textLength="12.2" clip-path="url(#terminal-3689181897-line-4)">
</text><text class="terminal-3689181897-r2" x="976" y="142" textLength="12.2" clip-path="url(#terminal-3689181897-line-5)">
</text><text class="terminal-3689181897-r2" x="976" y="166.4" textLength="12.2" clip-path="url(#terminal-3689181897-line-6)">
</text><text class="terminal-3689181897-r2" x="976" y="190.8" textLength="12.2" clip-path="url(#terminal-3689181897-line-7)">
</text><text class="terminal-3689181897-r2" x="976" y="215.2" textLength="12.2" clip-path="url(#terminal-3689181897-line-8)">
</text><text class="terminal-3689181897-r2" x="976" y="239.6" textLength="12.2" clip-path="url(#terminal-3689181897-line-9)">
</text><text class="terminal-3689181897-r2" x="976" y="264" textLength="12.2" clip-path="url(#terminal-3689181897-line-10)">
</text><text class="terminal-3689181897-r2" x="976" y="288.4" textLength="12.2" clip-path="url(#terminal-3689181897-line-11)">
</text><text class="terminal-3689181897-r2" x="976" y="312.8" textLength="12.2" clip-path="url(#terminal-3689181897-line-12)">
</text><text class="terminal-3689181897-r2" x="976" y="337.2" textLength="12.2" clip-path="url(#terminal-3689181897-line-13)">
</text><text class="terminal-3689181897-r2" x="976" y="361.6" textLength="12.2" clip-path="url(#terminal-3689181897-line-14)">
</text><text class="terminal-3689181897-r2" x="976" y="386" textLength="12.2" clip-path="url(#terminal-3689181897-line-15)">
</text><text class="terminal-3689181897-r2" x="976" y="410.4" textLength="12.2" clip-path="url(#terminal-3689181897-line-16)">
</text><text class="terminal-3689181897-r2" x="976" y="434.8" textLength="12.2" clip-path="url(#terminal-3689181897-line-17)">
</text><text class="terminal-3689181897-r2" x="976" y="459.2" textLength="12.2" clip-path="url(#terminal-3689181897-line-18)">
</text><text class="terminal-3689181897-r2" x="976" y="483.6" textLength="12.2" clip-path="url(#terminal-3689181897-line-19)">
</text><text class="terminal-3689181897-r2" x="976" y="508" textLength="12.2" clip-path="url(#terminal-3689181897-line-20)">
</text><text class="terminal-3689181897-r2" x="976" y="532.4" textLength="12.2" clip-path="url(#terminal-3689181897-line-21)">
</text><text class="terminal-3689181897-r2" x="976" y="556.8" textLength="12.2" clip-path="url(#terminal-3689181897-line-22)">
</text>
</g>
</g>
</svg>

'''
# ---
# name: test_input_and_focus
'''
<svg class="rich-terminal" viewBox="0 0 994 635.5999999999999" xmlns="http://www.w3.org/2000/svg">
Expand Down
24 changes: 24 additions & 0 deletions tests/snapshot_tests/snapshot_apps/horizontal_auto_width.css
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 tests/snapshot_tests/snapshot_apps/horizontal_auto_width.py
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()
6 changes: 6 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import pytest

# These paths should be relative to THIS directory.
WIDGET_EXAMPLES_DIR = Path("../../docs/examples/widgets")
LAYOUT_EXAMPLES_DIR = Path("../../docs/examples/guide/layout")
STYLES_EXAMPLES_DIR = Path("../../docs/examples/styles")
SNAPSHOT_APPS_DIR = Path("./snapshot_apps")


# --- Layout related stuff ---
Expand All @@ -29,6 +31,10 @@ def test_horizontal_layout(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "horizontal_layout.py")


def test_horizontal_layout_width_auto_dock(snap_compare):
assert snap_compare(SNAPSHOT_APPS_DIR / "horizontal_auto_width.py")


def test_vertical_layout(snap_compare):
assert snap_compare(LAYOUT_EXAMPLES_DIR / "vertical_layout.py")

Expand Down

0 comments on commit a465f5c

Please sign in to comment.