From edf00a7d0b9349ecae074b1d82783490fd10d76f Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 17 Feb 2023 11:36:01 +0000 Subject: [PATCH] fix for spatial calculation --- src/textual/_compositor.py | 2 +- src/textual/_spatial_map.py | 8 ++++---- tests/test_spatial_map.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/textual/_compositor.py b/src/textual/_compositor.py index 5502675bf..6282402a8 100644 --- a/src/textual/_compositor.py +++ b/src/textual/_compositor.py @@ -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 diff --git a/src/textual/_spatial_map.py b/src/textual/_spatial_map.py index 480e665ad..574a72bf2 100644 --- a/src/textual/_spatial_map.py +++ b/src/textual/_spatial_map.py @@ -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( diff --git a/tests/test_spatial_map.py b/tests/test_spatial_map.py index 18fd16648..adbbb9330 100644 --- a/tests/test_spatial_map.py +++ b/tests/test_spatial_map.py @@ -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