@@ -393,31 +393,63 @@ def add_content(
393
393
if layout_cells :
394
394
self ._layout_cells ()
395
395
396
- def get_cell (self , cell_coordinates : Tuple [int , int ]) -> displayio .Group :
396
+ def get_content (self , grid_position : Tuple [int , int ]) -> displayio .Group :
397
397
"""
398
- Return a cells content based on the cell_coordinates . Raises
398
+ Return a cells content based on the grid_position . Raises
399
399
KeyError if coordinates were not found in the GridLayout.
400
400
401
- :param tuple cell_coordinates : the coordinates to lookup in the grid
401
+ :param tuple grid_position : the coordinates to lookup in the grid
402
402
:return: the displayio content object at those coordinates
403
403
"""
404
404
for index , cell in enumerate (self ._cell_content_list ):
405
405
# exact location 1x1 cell
406
- if cell ["grid_position" ] == cell_coordinates :
406
+ if cell ["grid_position" ] == grid_position :
407
407
return self ._cell_content_list [index ]["content" ]
408
408
409
409
# multi-spanning cell, any size bigger than 1x1
410
410
if (
411
411
cell ["grid_position" ][0 ]
412
- <= cell_coordinates [0 ]
412
+ <= grid_position [0 ]
413
413
< cell ["grid_position" ][0 ] + cell ["cell_size" ][0 ]
414
414
and cell ["grid_position" ][1 ]
415
- <= cell_coordinates [1 ]
415
+ <= grid_position [1 ]
416
416
< cell ["grid_position" ][1 ] + cell ["cell_size" ][1 ]
417
417
):
418
418
return self ._cell_content_list [index ]["content" ]
419
419
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 } " )
421
453
422
454
@property
423
455
def cell_size_pixels (self ) -> Tuple [int , int ]:
0 commit comments