Margin space is now not rendered as solid colour

This commit is contained in:
Darren Burns
2022-01-20 14:47:37 +00:00
parent 185788b760
commit e2fd92bda2
5 changed files with 44 additions and 6 deletions

View File

@@ -128,7 +128,7 @@ class StylesBuilder:
append = space.append
for token in tokens:
(token_name, value, _, _, location) = token
if token_name == "scalar":
if token_name in ("number", "scalar"):
try:
append(int(value))
except ValueError:

View File

@@ -415,6 +415,24 @@ class Region(NamedTuple):
)
return new_region
def shrink(self, margin: Spacing) -> Region:
"""Shrink a region by pushing each edge inwards.
Args:
margin (Spacing): Defines how many cells to shrink the Region by at each edge.
Returns:
Region: The new, smaller region.
"""
_clamp = clamp
top, right, bottom, left = margin
return Region(
x=_clamp(self.x + left, 0, self.width),
y=_clamp(self.y + top, 0, self.height),
width=_clamp(self.width - left - right, 0, self.width),
height=_clamp(self.height - top - bottom, 0, self.height),
)
def intersection(self, region: Region) -> Region:
"""Get that covers both regions.

View File

@@ -114,7 +114,24 @@ class View(Widget):
cached_size, cached_scroll, arrangement = self._cached_arrangement
if cached_size == size and cached_scroll == scroll:
return arrangement
arrangement = list(self._layout.arrange(self, size, scroll))
arrangement = []
placements = self._layout.arrange(self, size, scroll)
for placement in placements:
region, widget, order = placement
styles = widget.styles
if styles.has_margin:
margin = styles.margin
arrangement.append(
WidgetPlacement(
region=region.shrink(margin),
widget=widget,
order=order,
)
)
else:
arrangement.append(placement)
self._cached_arrangement = (size, scroll, arrangement)
return arrangement

View File

@@ -181,9 +181,6 @@ class Widget(DOMNode):
renderable, styles.border, style=renderable_text_style
)
if styles.has_margin:
renderable = Padding(renderable, styles.margin, style=parent_text_style)
if styles.has_outline:
renderable = Border(
renderable,

View File

@@ -1,6 +1,6 @@
import pytest
from textual.geometry import clamp, Offset, Size, Region
from textual.geometry import clamp, Offset, Size, Region, Spacing
def test_dimensions_region():
@@ -173,6 +173,12 @@ def test_clip():
assert Region(10, 10, 20, 30).clip(20, 25) == Region(10, 10, 10, 15)
def test_region_shrink():
margin = Spacing(top=1, right=2, bottom=3, left=4)
region = Region(x=10, y=10, width=50, height=50)
assert region.shrink(margin) == Region(x=14, y=11, width=44, height=46)
def test_region_intersection():
assert Region(0, 0, 100, 50).intersection(Region(10, 10, 10, 10)) == Region(
10, 10, 10, 10