adafruit / adafruit/Adafruit_CircuitPython_PortalBase

PortalBase.set_text() with empty string can unexpectedly change render ordering

Open
#117 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
17
Forks
21
PR merge metrics
No merged PRs in 30d

Description

With `displayio` the order in which you add append items into a group makes a difference, items that are appended to the group later are supposed to render on top of other elements in the group.

In my application I have a bit of tricky rendering that I am doing where I have an image that I want to make sure shows up on top of text even if the text overlaps with the image. To make sure this is the case I add the call to add the image AFTER I make the call to add the text. Something like this, using a rectangle as a stand in for the image:

```py
from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_display_shapes.rect import Rect
p = MatrixPortal()
tIdx = p.add_text(text_position=(2, 5), text="test")
rectangle = Rect(x=20, y=5, width=20, height=20, fill=0x0000DD)
p.root_group.append(rectangle)
```

Image

This generally works as I expect it to.

However while I am running my application I sometimes need to update the text field to display some new text. Since `MatrixPortal` comes with `set_text` I am just using that:

```py
p.set_text("new text", tIdx)
```
Image

This also works fine _if_ I am setting the text to a non-empty value. If i happen to make a call to set the text to an empty value:

```py
p.set_text("", tIdx)
```
Image

And then later on need to set the next to a non-empty value:

```py
p.set_text("non_empty", tIdx)
```
Image

All of the sudden my text unexpectedly shows up on top of my image!!!!

I am fairly certain this is happening because there is a branch in `set_text` where when I do `p.set_text("", tIdx)` it deletes the label because the length of the string is zero:
https://github.com/adafruit/Adafruit_CircuitPython_PortalBase/blob/25fc43dd67ae95a8e62173e90c3069502194873a/adafruit_portalbase/__init__.py#L312

Then later on when I do `p.set_text("non_empty", tIdx)` it is creating a new label because we no longer have a label.

https://github.com/adafruit/Adafruit_CircuitPython_PortalBase/blob/25fc43dd67ae95a8e62173e90c3069502194873a/adafruit_portalbase/__init__.py#L288

Now that I know what is going on here I am sure I can find some way of working around this. Probably I'll just add some logic in to set the text to a single space character instead of a empty string when I don't have any text to display, `p.set_text(" ", tIdx)` that way it will "trick" `set_text` into leaving the label there.

I am mainly reporting this because it took me quite some time to track down what was going on here since setting the text field to an empty string only happens once a day or so in my application. It was also quite unexpected that `set_text` removed the label. To me it feels like instead of doing `self._text[index]["label"] = None` it would be safer to do `self._text[index]["label"].text = ""` and keep the text in place. But I also get that that might have some memory implications of keeping labels around when they aren't needed and might break backwards compatibility with existing programs. So maybe just add some documentation note the mentions that `set_text` will remove the label if an empty string is provided?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in adafruit_portalbase/__init__.py around set_text at lines 288 and 312, and reproduce the sequence of adding text and an image, setting the text to an empty string, then restoring it. Compare the label-removal and label-creation paths, and establish whether the intended fix is to preserve ordering or document the empty-string behavior. Done when the behavior is covered by an agreed implementation or documentation change.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics, embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.