@@ -112,11 +112,7 @@ def __init__(
112112
113113 self ._background_color = background_color
114114 self ._background_palette = displayio .Palette (1 )
115- self .append (
116- displayio .TileGrid (
117- displayio .Bitmap (1 , 1 , 1 ), pixel_shader = self ._background_palette
118- )
119- ) # initialize with a blank tilegrid placeholder for background
115+ self ._added_background_tilegrid = False
120116
121117 self ._padding_top = padding_top
122118 self ._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160156 )
161157 y_box_offset = - ascender_max + y_offset - self ._padding_top
162158
163- self ._update_background_color (self ._background_color )
164159 box_width = max (0 , box_width ) # remove any negative values
165160 box_height = max (0 , box_height ) # remove any negative values
166161
@@ -171,21 +166,49 @@ def _create_background_box(self, lines, y_offset):
171166 x = left + x_box_offset ,
172167 y = y_box_offset ,
173168 )
169+
174170 return tile_grid
175171
176172 def _update_background_color (self , new_color ):
177173
178174 if new_color is None :
179175 self ._background_palette .make_transparent (0 )
176+ if self ._added_background_tilegrid :
177+ self .pop (0 )
178+ self ._added_background_tilegrid = False
180179 else :
181180 self ._background_palette .make_opaque (0 )
182181 self ._background_palette [0 ] = new_color
183182 self ._background_color = new_color
184183
185- def _update_text (self , new_text ): # pylint: disable=too-many-locals
184+ y_offset = int (
185+ (
186+ self ._font .get_glyph (ord ("M" )).height
187+ - self .text .count ("\n " ) * self .height * self .line_spacing
188+ )
189+ / 2
190+ )
191+ lines = self .text .count ("\n " ) + 1
192+ if not self ._added_background_tilegrid :
193+ # only if we have text or padding
194+ if len (self .text ) + self ._padding_left + self ._padding_right > 0 :
195+ if len (self ) > 0 :
196+ self .insert (0 , self ._create_background_box (lines , y_offset ))
197+ else :
198+ self .append (self ._create_background_box (lines , y_offset ))
199+ self ._added_background_tilegrid = True
200+ else :
201+ self [0 ] = self ._create_background_box (lines , y_offset )
202+
203+ def _update_text (
204+ self , new_text
205+ ): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
186206 x = 0
187207 y = 0
188- i = 1
208+ if self ._added_background_tilegrid :
209+ i = 1
210+ else :
211+ i = 0
189212 old_c = 0
190213 y_offset = int (
191214 (
@@ -267,7 +290,20 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267290 self .pop ()
268291 self ._text = new_text
269292 self ._boundingbox = (left , top , left + right , bottom - top )
270- self [0 ] = self ._create_background_box (lines , y_offset )
293+ if (
294+ self ._background_color
295+ and len (new_text ) + self ._padding_left + self ._padding_right > 0
296+ ):
297+ if not self ._added_background_tilegrid :
298+ self ._added_background_tilegrid = True
299+ self .insert (0 , self ._create_background_box (lines , y_offset ))
300+ else :
301+ self [0 ] = self ._create_background_box (lines , y_offset )
302+ else :
303+ self ._background_palette .make_transparent (0 )
304+ if self ._added_background_tilegrid :
305+ self .pop (0 )
306+ self ._added_background_tilegrid = False
271307
272308 @property
273309 def bounding_box (self ):
@@ -369,6 +405,5 @@ def anchored_position(self, new_position):
369405 - (self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
370406 + round ((self ._boundingbox [3 ] * self ._scale ) / 2.0 )
371407 )
372- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
373408 self .x = new_x
374409 self .y = new_y
0 commit comments