@@ -112,11 +112,7 @@ def __init__(
112
112
113
113
self ._background_color = background_color
114
114
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
120
116
121
117
self ._padding_top = padding_top
122
118
self ._padding_bottom = padding_bottom
@@ -160,7 +156,6 @@ def _create_background_box(self, lines, y_offset):
160
156
)
161
157
y_box_offset = - ascender_max + y_offset - self ._padding_top
162
158
163
- self ._update_background_color (self ._background_color )
164
159
box_width = max (0 , box_width ) # remove any negative values
165
160
box_height = max (0 , box_height ) # remove any negative values
166
161
@@ -171,21 +166,49 @@ def _create_background_box(self, lines, y_offset):
171
166
x = left + x_box_offset ,
172
167
y = y_box_offset ,
173
168
)
169
+
174
170
return tile_grid
175
171
176
172
def _update_background_color (self , new_color ):
177
173
178
174
if new_color is None :
179
175
self ._background_palette .make_transparent (0 )
176
+ if self ._added_background_tilegrid :
177
+ self .pop (0 )
178
+ self ._added_background_tilegrid = False
180
179
else :
181
180
self ._background_palette .make_opaque (0 )
182
181
self ._background_palette [0 ] = new_color
183
182
self ._background_color = new_color
184
183
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
186
206
x = 0
187
207
y = 0
188
- i = 1
208
+ if self ._added_background_tilegrid :
209
+ i = 1
210
+ else :
211
+ i = 0
189
212
old_c = 0
190
213
y_offset = int (
191
214
(
@@ -267,7 +290,20 @@ def _update_text(self, new_text): # pylint: disable=too-many-locals
267
290
self .pop ()
268
291
self ._text = new_text
269
292
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
271
307
272
308
@property
273
309
def bounding_box (self ):
@@ -369,6 +405,5 @@ def anchored_position(self, new_position):
369
405
- (self ._anchor_point [1 ] * self ._boundingbox [3 ] * self ._scale )
370
406
+ round ((self ._boundingbox [3 ] * self ._scale ) / 2.0 )
371
407
)
372
- self ._boundingbox = (new_x , new_y , self ._boundingbox [2 ], self ._boundingbox [3 ])
373
408
self .x = new_x
374
409
self .y = new_y
0 commit comments