Skip to content

Commit 469e24d

Browse files
committed
TextBox show when empty
1 parent 969fe01 commit 469e24d

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

adafruit_display_text/text_box.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class TextBox(bitmap_label.Label):
5454
:param height: The height of the TextBox in pixels.
5555
:param align: How to align the text within the box,
5656
valid values are ``ALIGN_LEFT``, ``ALIGN_CENTER``, ``ALIGN_RIGHT``.
57+
:param bool show_when_empty: Whether to show the background box
58+
when there is no text. True to show when empty, False to hide.
5759
"""
5860

5961
ALIGN_LEFT = const(0)
@@ -63,12 +65,19 @@ class TextBox(bitmap_label.Label):
6365
DYNAMIC_HEIGHT = const(-1)
6466

6567
def __init__(
66-
self, font: FontProtocol, width: int, height: int, align=ALIGN_LEFT, **kwargs
68+
self,
69+
font: FontProtocol,
70+
width: int,
71+
height: int,
72+
align=ALIGN_LEFT,
73+
show_when_empty=False,
74+
**kwargs,
6775
) -> None:
6876
self._bitmap = None
6977
self._tilegrid = None
7078
self._prev_label_direction = None
7179
self._width = width
80+
self._show_when_empty = show_when_empty
7281

7382
if height != TextBox.DYNAMIC_HEIGHT:
7483
self._height = height
@@ -238,23 +247,26 @@ def _reset_text(
238247
self._text = self._replace_tabs(text)
239248

240249
# Check for empty string
241-
if (not text) or (
242-
text is None
243-
): # If empty string, just create a zero-sized bounding box and that's it.
244-
self._bounding_box = (
245-
0,
246-
0,
247-
0, # zero width with text == ""
248-
0, # zero height with text == ""
249-
)
250-
# Clear out any items in the self._local_group Group, in case this is an
251-
# update to the bitmap_label
252-
for _ in self._local_group:
253-
self._local_group.pop(0)
250+
if (not text) or (text is None):
251+
if not self._show_when_empty:
252+
# If empty string, just create a zero-sized bounding box and that's it.
253+
self._bounding_box = (
254+
0,
255+
0,
256+
0, # zero width with text == ""
257+
0, # zero height with text == ""
258+
)
259+
# Clear out any items in the self._local_group Group, in case this is an
260+
# update to the bitmap_label
261+
for _ in self._local_group:
262+
self._local_group.pop(0)
254263

255-
# Free the bitmap and tilegrid since they are removed
256-
self._bitmap = None
257-
self._tilegrid = None
264+
# Free the bitmap and tilegrid since they are removed
265+
self._bitmap = None
266+
self._tilegrid = None
267+
else:
268+
# clear the existing bitmap and keep it
269+
self._bitmap.fill(0)
258270

259271
else: # The text string is not empty, so create the Bitmap and TileGrid and
260272
# append to the self Group
@@ -399,3 +411,14 @@ def align(self, align: int) -> None:
399411
if align not in {TextBox.ALIGN_LEFT, TextBox.ALIGN_CENTER, TextBox.ALIGN_RIGHT}:
400412
raise ValueError("Align must be one of: ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT")
401413
self._align = align
414+
415+
@property
416+
def show_when_empty(self):
417+
"""
418+
Whether to show the background box when there is no text
419+
"""
420+
return self._show_when_empty
421+
422+
@show_when_empty.setter
423+
def show_when_empty(self, value: bool) -> None:
424+
self._show_when_empty = value

0 commit comments

Comments
 (0)