mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #221 from Textualize/dont-render-margin
Margin spacing is now invisible
This commit is contained in:
@@ -129,7 +129,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:
|
||||
|
||||
@@ -415,6 +415,25 @@ 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
|
||||
x, y, width, height = self
|
||||
return Region(
|
||||
x=_clamp(x + left, 0, width),
|
||||
y=_clamp(y + top, 0, height),
|
||||
width=_clamp(width - left - right, 0, width),
|
||||
height=_clamp(height - top - bottom, 0, height),
|
||||
)
|
||||
|
||||
def intersection(self, region: Region) -> Region:
|
||||
"""Get that covers both regions.
|
||||
|
||||
|
||||
@@ -55,6 +55,17 @@ class WidgetPlacement(NamedTuple):
|
||||
widget: Widget | None = None
|
||||
order: int = 0
|
||||
|
||||
def apply_margin(self) -> "WidgetPlacement":
|
||||
region, widget, order = self
|
||||
styles = widget.styles
|
||||
if styles.has_margin:
|
||||
return WidgetPlacement(
|
||||
region=region.shrink(styles.margin),
|
||||
widget=widget,
|
||||
order=order,
|
||||
)
|
||||
return self
|
||||
|
||||
|
||||
@rich.repr.auto
|
||||
class LayoutUpdate:
|
||||
|
||||
@@ -102,9 +102,14 @@ 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))
|
||||
self._cached_arrangement = (size, scroll, arrangement)
|
||||
return arrangement
|
||||
|
||||
placements = [
|
||||
placement.apply_margin()
|
||||
for placement in self._layout.arrange(self, size, scroll)
|
||||
]
|
||||
|
||||
self._cached_arrangement = (size, scroll, placements)
|
||||
return placements
|
||||
|
||||
async def handle_update(self, message: messages.Update) -> None:
|
||||
if self.is_root_view:
|
||||
|
||||
@@ -170,9 +170,6 @@ class Widget(DOMNode):
|
||||
if styles.has_border:
|
||||
renderable = Border(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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user