Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add pop_content() rename get_cell() to get_content() #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions adafruit_displayio_layout/layouts/grid_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,31 +393,63 @@ def add_content(
if layout_cells:
self._layout_cells()

def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
def get_content(self, grid_position: Tuple[int, int]) -> displayio.Group:
"""
Return a cells content based on the cell_coordinates. Raises
Return a cells content based on the grid_position. Raises
KeyError if coordinates were not found in the GridLayout.

:param tuple cell_coordinates: the coordinates to lookup in the grid
:param tuple grid_position: the coordinates to lookup in the grid
:return: the displayio content object at those coordinates
"""
for index, cell in enumerate(self._cell_content_list):
# exact location 1x1 cell
if cell["grid_position"] == cell_coordinates:
if cell["grid_position"] == grid_position:
return self._cell_content_list[index]["content"]

# multi-spanning cell, any size bigger than 1x1
if (
cell["grid_position"][0]
<= cell_coordinates[0]
<= grid_position[0]
< cell["grid_position"][0] + cell["cell_size"][0]
and cell["grid_position"][1]
<= cell_coordinates[1]
<= grid_position[1]
< cell["grid_position"][1] + cell["cell_size"][1]
):
return self._cell_content_list[index]["content"]

raise KeyError(f"GridLayout does not contain cell at coordinates {cell_coordinates}")
raise KeyError(f"GridLayout does not contain content at coordinates {grid_position}")

def pop_content(self, grid_position: Tuple[int, int]) -> None:
"""
Remove and return a cells content based on the grid_position. Raises
KeyError if coordinates were not found in the GridLayout.

:param tuple grid_position: the coordinates to lookup in the grid
:return: the displayio content object at those coordinates
"""
for index, cell in enumerate(self._cell_content_list):
# exact location 1x1 cell
if cell["grid_position"] == grid_position:
_found = self._cell_content_list.pop(index)
self._layout_cells()
self.remove(_found["content"])
return _found["content"]

# multi-spanning cell, any size bigger than 1x1
if (
cell["grid_position"][0]
<= grid_position[0]
< cell["grid_position"][0] + cell["cell_size"][0]
and cell["grid_position"][1]
<= grid_position[1]
< cell["grid_position"][1] + cell["cell_size"][1]
):
_found = self._cell_content_list.pop(index)
self._layout_cells()
self.remove(_found["content"])
return _found["content"]

raise KeyError(f"GridLayout does not contain content at coordinates {grid_position}")

@property
def cell_size_pixels(self) -> Tuple[int, int]:
Expand Down