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

View File

@@ -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">

View File

@@ -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;
}

View File

@@ -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()

View File

@@ -2,9 +2,11 @@ from pathlib import Path
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 ---
@@ -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")