Skip to content

Commit e0f4370

Browse files
committed
Only recreate the label if text is longer, otherwise reuse
1 parent 7bd92ac commit e0f4370

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

adafruit_portalbase/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,36 @@ def set_text(self, val, index=0):
237237
index_in_splash = self.splash.index(self._text[index]["label"])
238238
elif self._debug:
239239
print("Creating text area with :", string)
240+
if len(string) > 0:
241+
# If longer text, destroy and recreate the label because we can't increase max_glyphs
242+
if (
243+
self._text[index]["label"] is not None
244+
and len(string) > self._text[index]["label"].width
245+
):
246+
self._text[index]["label"] = None
240247

241-
if len(string.strip()) > 0:
242248
if self._text[index]["label"] is None:
243249
self._text[index]["label"] = Label(
244250
self._fonts[self._text[index]["font"]],
245251
text=string,
246252
scale=self._text[index]["scale"],
247253
)
254+
if index_in_splash is not None:
255+
self.splash[index_in_splash] = self._text[index]["label"]
256+
else:
257+
self.splash.append(self._text[index]["label"])
258+
else:
259+
self._text[index]["label"].text = string
248260
self._text[index]["label"].color = self._text[index]["color"]
249261
self._text[index]["label"].anchor_point = self._text[index]["anchor_point"]
250262
self._text[index]["label"].anchored_position = self._text[index]["position"]
251263
self._text[index]["label"].line_spacing = self._text[index]["line_spacing"]
252264
elif index_in_splash is not None:
253265
self._text[index]["label"] = None
254-
del self.splash[index_in_splash]
255266

256-
if self._text[index]["label"] is not None and index_in_splash is None:
257-
self.splash.append(self._text[index]["label"])
267+
# Remove the label from splash
268+
if index_in_splash is not None and self._text[index]["label"] is None:
269+
del self.splash[index_in_splash]
258270

259271
def preload_font(self, glyphs=None, index=0):
260272
# pylint: disable=line-too-long

0 commit comments

Comments
 (0)