-
Notifications
You must be signed in to change notification settings - Fork 21
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:
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)
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:
p.set_text("new text", tIdx)
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:
p.set_text("", tIdx)
And then later on need to set the next to a non-empty value:
p.set_text("non_empty", tIdx)
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:
| self._text[index]["label"] = None |
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.
| self._text[index]["label"] = Label( |
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?