mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
add tests for arrange
This commit is contained in:
@@ -13,6 +13,9 @@ from ._partition import partition
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .widget import Widget
|
from .widget import Widget
|
||||||
|
|
||||||
|
# TODO: This is a bit of a fudge, need to ensure it is impossible for layouts to generate this value
|
||||||
|
TOP_Z = 2**31 - 1
|
||||||
|
|
||||||
|
|
||||||
def arrange(
|
def arrange(
|
||||||
widget: Widget, children: Sequence[Widget], size: Size, viewport: Size
|
widget: Widget, children: Sequence[Widget], size: Size, viewport: Size
|
||||||
@@ -43,10 +46,7 @@ def arrange(
|
|||||||
region = size.region
|
region = size.region
|
||||||
|
|
||||||
_WidgetPlacement = WidgetPlacement
|
_WidgetPlacement = WidgetPlacement
|
||||||
|
top_z = TOP_Z
|
||||||
# TODO: This is a bit of a fudge, need to ensure it is impossible for layouts to generate this value
|
|
||||||
top_z = 2**31 - 1
|
|
||||||
|
|
||||||
scroll_spacing = Spacing()
|
scroll_spacing = Spacing()
|
||||||
|
|
||||||
get_dock = attrgetter("styles.dock")
|
get_dock = attrgetter("styles.dock")
|
||||||
@@ -87,7 +87,7 @@ def arrange(
|
|||||||
right = max(right, dock_region.width)
|
right = max(right, dock_region.width)
|
||||||
else:
|
else:
|
||||||
# Should not occur, mainly to keep Mypy happy
|
# Should not occur, mainly to keep Mypy happy
|
||||||
raise AssertionError("invalid value for edge")
|
raise AssertionError("invalid value for edge") # pragma: no-cover
|
||||||
|
|
||||||
align_offset = dock_widget.styles.align_size(
|
align_offset = dock_widget.styles.align_size(
|
||||||
(widget_width, widget_height), size
|
(widget_width, widget_height), size
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from rich.tree import Tree
|
|||||||
|
|
||||||
from ._context import NoActiveAppError
|
from ._context import NoActiveAppError
|
||||||
from ._node_list import NodeList
|
from ._node_list import NodeList
|
||||||
from .color import Color
|
from .color import Color, WHITE, BLACK
|
||||||
from .css._error_tools import friendly_list
|
from .css._error_tools import friendly_list
|
||||||
from .css.constants import VALID_DISPLAY, VALID_VISIBILITY
|
from .css.constants import VALID_DISPLAY, VALID_VISIBILITY
|
||||||
from .css.errors import StyleValueError
|
from .css.errors import StyleValueError
|
||||||
@@ -379,7 +379,7 @@ class DOMNode(MessagePump):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
base_background = background = Color(0, 0, 0, 0)
|
base_background = background = BLACK
|
||||||
|
|
||||||
for node in reversed(self.ancestors):
|
for node in reversed(self.ancestors):
|
||||||
styles = node.styles
|
styles = node.styles
|
||||||
@@ -395,8 +395,8 @@ class DOMNode(MessagePump):
|
|||||||
Returns:
|
Returns:
|
||||||
tuple[Color, Color, Color, Color]: Tuple of (base background, base color, background, color)
|
tuple[Color, Color, Color, Color]: Tuple of (base background, base color, background, color)
|
||||||
"""
|
"""
|
||||||
base_background = background = Color(0, 0, 0, 0)
|
base_background = background = WHITE
|
||||||
base_color = color = Color(255, 255, 255, 0)
|
base_color = color = BLACK
|
||||||
for node in reversed(self.ancestors):
|
for node in reversed(self.ancestors):
|
||||||
styles = node.styles
|
styles = node.styles
|
||||||
if styles.has_rule("background"):
|
if styles.has_rule("background"):
|
||||||
|
|||||||
109
tests/test_arrange.py
Normal file
109
tests/test_arrange.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
from textual._arrange import arrange, TOP_Z
|
||||||
|
from textual._layout import WidgetPlacement
|
||||||
|
from textual.geometry import Region, Size, Spacing
|
||||||
|
from textual.widget import Widget
|
||||||
|
|
||||||
|
|
||||||
|
def test_arrange_empty():
|
||||||
|
container = Widget(id="container")
|
||||||
|
|
||||||
|
placements, widgets, spacing = arrange(container, [], Size(80, 24), Size(80, 24))
|
||||||
|
assert placements == []
|
||||||
|
assert widgets == set()
|
||||||
|
assert spacing == Spacing(0, 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_arrange_dock_top():
|
||||||
|
container = Widget(id="container")
|
||||||
|
child = Widget(id="child")
|
||||||
|
header = Widget(id="header")
|
||||||
|
header.styles.dock = "top"
|
||||||
|
header.styles.height = "1"
|
||||||
|
|
||||||
|
placements, widgets, spacing = arrange(
|
||||||
|
container, [child, header], Size(80, 24), Size(80, 24)
|
||||||
|
)
|
||||||
|
assert placements == [
|
||||||
|
WidgetPlacement(Region(0, 0, 80, 1), header, order=TOP_Z, fixed=True),
|
||||||
|
WidgetPlacement(Region(0, 1, 80, 23), child, order=0, fixed=False),
|
||||||
|
WidgetPlacement(
|
||||||
|
region=Region(x=0, y=1, width=80, height=23),
|
||||||
|
widget=None,
|
||||||
|
order=0,
|
||||||
|
fixed=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
assert widgets == {child, header}
|
||||||
|
assert spacing == Spacing(1, 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_arrange_dock_left():
|
||||||
|
container = Widget(id="container")
|
||||||
|
child = Widget(id="child")
|
||||||
|
header = Widget(id="header")
|
||||||
|
header.styles.dock = "left"
|
||||||
|
header.styles.width = "10"
|
||||||
|
|
||||||
|
placements, widgets, spacing = arrange(
|
||||||
|
container, [child, header], Size(80, 24), Size(80, 24)
|
||||||
|
)
|
||||||
|
assert placements == [
|
||||||
|
WidgetPlacement(Region(0, 0, 10, 24), header, order=TOP_Z, fixed=True),
|
||||||
|
WidgetPlacement(Region(10, 0, 70, 24), child, order=0, fixed=False),
|
||||||
|
WidgetPlacement(
|
||||||
|
region=Region(x=10, y=0, width=70, height=24),
|
||||||
|
widget=None,
|
||||||
|
order=0,
|
||||||
|
fixed=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
assert widgets == {child, header}
|
||||||
|
assert spacing == Spacing(0, 0, 0, 10)
|
||||||
|
|
||||||
|
|
||||||
|
def test_arrange_dock_right():
|
||||||
|
container = Widget(id="container")
|
||||||
|
child = Widget(id="child")
|
||||||
|
header = Widget(id="header")
|
||||||
|
header.styles.dock = "right"
|
||||||
|
header.styles.width = "10"
|
||||||
|
|
||||||
|
placements, widgets, spacing = arrange(
|
||||||
|
container, [child, header], Size(80, 24), Size(80, 24)
|
||||||
|
)
|
||||||
|
assert placements == [
|
||||||
|
WidgetPlacement(Region(70, 0, 10, 24), header, order=TOP_Z, fixed=True),
|
||||||
|
WidgetPlacement(Region(0, 0, 70, 24), child, order=0, fixed=False),
|
||||||
|
WidgetPlacement(
|
||||||
|
region=Region(x=0, y=0, width=70, height=24),
|
||||||
|
widget=None,
|
||||||
|
order=0,
|
||||||
|
fixed=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
assert widgets == {child, header}
|
||||||
|
assert spacing == Spacing(0, 10, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_arrange_dock_bottom():
|
||||||
|
container = Widget(id="container")
|
||||||
|
child = Widget(id="child")
|
||||||
|
header = Widget(id="header")
|
||||||
|
header.styles.dock = "bottom"
|
||||||
|
header.styles.height = "1"
|
||||||
|
|
||||||
|
placements, widgets, spacing = arrange(
|
||||||
|
container, [child, header], Size(80, 24), Size(80, 24)
|
||||||
|
)
|
||||||
|
assert placements == [
|
||||||
|
WidgetPlacement(Region(0, 23, 80, 1), header, order=TOP_Z, fixed=True),
|
||||||
|
WidgetPlacement(Region(0, 0, 80, 23), child, order=0, fixed=False),
|
||||||
|
WidgetPlacement(
|
||||||
|
region=Region(x=0, y=0, width=80, height=23),
|
||||||
|
widget=None,
|
||||||
|
order=0,
|
||||||
|
fixed=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
assert widgets == {child, header}
|
||||||
|
assert spacing == Spacing(0, 0, 1, 0)
|
||||||
Reference in New Issue
Block a user