fix for spatial calculation

This commit is contained in:
Will McGugan
2023-02-17 11:36:01 +00:00
parent 11d10db1ab
commit edf00a7d0b
3 changed files with 33 additions and 8 deletions

View File

@@ -450,7 +450,7 @@ class Compositor:
if visible_only:
placements = arrange_result.get_visible_placements(
widget.size.region + widget.scroll_offset
container_size.region + widget.scroll_offset
)
else:
placements = arrange_result.placements

View File

@@ -35,13 +35,13 @@ class SpatialMap(Generic[ValueType]):
Iterable of grid squares (tuple of 2 values).
"""
x1, y1, width, height = region
x2 = x1 + width
y2 = y1 + height
x2 = x1 + width - 1
y2 = y1 + height - 1
grid_width, grid_height = self._grid_size
return product(
range(x1 // grid_width, 1 + x2 // grid_width),
range(y1 // grid_height, 1 + y2 // grid_height),
range(x1 // grid_width, x2 // grid_width + 1),
range(y1 // grid_height, y2 // grid_height + 1),
)
def insert(

View File

@@ -1,8 +1,33 @@
import pytest
from textual._spatial_map import SpatialMap
from textual.geometry import Region
def test_region_to_grid():
spatial_map = SpatialMap()
@pytest.mark.parametrize(
"region,grid",
[
(
Region(0, 0, 10, 10),
[
(0, 0),
],
),
(
Region(0, 0, 11, 11),
[(0, 0), (0, 1), (1, 0), (1, 1)],
),
(
Region(5, 5, 15, 3),
[(0, 0), (1, 0)],
),
(
Region(5, 5, 2, 15),
[(0, 0), (0, 1)],
),
],
)
def test_region_to_grid(region, grid):
spatial_map = SpatialMap(10, 10)
assert list(spatial_map._region_to_grid(Region(0, 0, 10, 10))) == [(0, 0)]
assert list(spatial_map._region_to_grid(region)) == grid