Skip to content

Commit 746b188

Browse files
committed
add pop_content, rename get_cell to get_content
1 parent 13376b5 commit 746b188

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

+39-7
Original file line numberDiff line numberDiff line change
@@ -393,31 +393,63 @@ def add_content(
393393
if layout_cells:
394394
self._layout_cells()
395395

396-
def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
396+
def get_content(self, grid_position: Tuple[int, int]) -> displayio.Group:
397397
"""
398-
Return a cells content based on the cell_coordinates. Raises
398+
Return a cells content based on the grid_position. Raises
399399
KeyError if coordinates were not found in the GridLayout.
400400
401-
:param tuple cell_coordinates: the coordinates to lookup in the grid
401+
:param tuple grid_position: the coordinates to lookup in the grid
402402
:return: the displayio content object at those coordinates
403403
"""
404404
for index, cell in enumerate(self._cell_content_list):
405405
# exact location 1x1 cell
406-
if cell["grid_position"] == cell_coordinates:
406+
if cell["grid_position"] == grid_position:
407407
return self._cell_content_list[index]["content"]
408408

409409
# multi-spanning cell, any size bigger than 1x1
410410
if (
411411
cell["grid_position"][0]
412-
<= cell_coordinates[0]
412+
<= grid_position[0]
413413
< cell["grid_position"][0] + cell["cell_size"][0]
414414
and cell["grid_position"][1]
415-
<= cell_coordinates[1]
415+
<= grid_position[1]
416416
< cell["grid_position"][1] + cell["cell_size"][1]
417417
):
418418
return self._cell_content_list[index]["content"]
419419

420-
raise KeyError(f"GridLayout does not contain cell at coordinates {cell_coordinates}")
420+
raise KeyError(f"GridLayout does not contain content at coordinates {grid_position}")
421+
422+
def pop_content(self, grid_position: Tuple[int, int]) -> None:
423+
"""
424+
Remove and return a cells content based on the grid_position. Raises
425+
KeyError if coordinates were not found in the GridLayout.
426+
427+
:param tuple grid_position: the coordinates to lookup in the grid
428+
:return: the displayio content object at those coordinates
429+
"""
430+
for index, cell in enumerate(self._cell_content_list):
431+
# exact location 1x1 cell
432+
if cell["grid_position"] == grid_position:
433+
_found = self._cell_content_list.pop(index)
434+
self._layout_cells()
435+
self.remove(_found["content"])
436+
return _found["content"]
437+
438+
# multi-spanning cell, any size bigger than 1x1
439+
if (
440+
cell["grid_position"][0]
441+
<= grid_position[0]
442+
< cell["grid_position"][0] + cell["cell_size"][0]
443+
and cell["grid_position"][1]
444+
<= grid_position[1]
445+
< cell["grid_position"][1] + cell["cell_size"][1]
446+
):
447+
_found = self._cell_content_list.pop(index)
448+
self._layout_cells()
449+
self.remove(_found["content"])
450+
return _found["content"]
451+
452+
raise KeyError(f"GridLayout does not contain content at coordinates {grid_position}")
421453

422454
@property
423455
def cell_size_pixels(self) -> Tuple[int, int]:

0 commit comments

Comments
 (0)