Skip to content

Commit f3f9c55

Browse files
committed
Do not crash Theme Editor when error in theme
1 parent 904bb4d commit f3f9c55

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

library/lcd/lcd_comm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,9 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
506506
else:
507507
angle_end += 0.1
508508

509-
assert xc - radius >= 0 and xc + radius <= self.get_width(), 'Progress bar width exceeds display width'
510-
assert yc - radius >= 0 and yc + radius <= self.get_height(), 'Progress bar height exceeds display height'
511-
assert 0 < bar_width <= radius, f'Progress bar linewidth is {bar_width}, must be > 0 and <= radius'
509+
assert xc - radius >= 0 and xc + radius <= self.get_width(), 'Radial is out of screen (left/right)'
510+
assert yc - radius >= 0 and yc + radius <= self.get_height(), 'Radial is out of screen (up/down)'
511+
assert 0 < bar_width <= radius, f'Radial linewidth is {bar_width}, must be > 0 and <= radius'
512512
assert angle_end % 361 != angle_start % 361, f'Invalid angles values, start = {angle_start}, end = {angle_end}'
513513
assert isinstance(angle_steps, int), 'angle_steps value must be an integer'
514514
assert angle_sep >= 0, 'Provide an angle_sep value >= 0'
@@ -521,7 +521,7 @@ def DisplayRadialProgressBar(self, xc: int, yc: int, radius: int, bar_width: int
521521
elif max_value < value:
522522
value = max_value
523523

524-
assert min_value <= value <= max_value, 'Progress bar value shall be between min and max'
524+
assert min_value <= value <= max_value, 'Radial value shall be between min and max'
525525

526526
diameter = 2 * radius
527527
bbox = (xc - radius, yc - radius, xc + radius, yc + radius)

res/docs/error-in-theme.png

11.6 KB
Loading

theme-editor.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
# Resize editor if display is too big (e.g. 8.8" displays are 1920x480)
8686
RESIZE_FACTOR = 2 if (display.lcd.get_width() > 1000 or display.lcd.get_height() > 1000) else 1
8787

88+
ERROR_IN_THEME = Image.open("res/docs/error-in-theme.png")
89+
8890

8991
def refresh_theme():
9092
config.load_theme()
@@ -244,7 +246,12 @@ def on_zone_click(event):
244246
subprocess.call(('xdg-open', config.MAIN_DIRECTORY / theme_file))
245247

246248
# Load theme file and generate first preview
247-
refresh_theme()
249+
try:
250+
refresh_theme()
251+
error_in_theme = False
252+
except Exception as e:
253+
logger.error(f"Error in theme: {e}")
254+
error_in_theme = True
248255

249256
display_width, display_height = int(display.lcd.get_width() / RESIZE_FACTOR), int(
250257
display.lcd.get_height() / RESIZE_FACTOR)
@@ -268,12 +275,16 @@ def on_zone_click(event):
268275
circular_mask = Image.open(config.MAIN_DIRECTORY / "res/backgrounds/circular-mask.png")
269276

270277
# Display preview in the window
271-
if config.THEME_DATA["display"].get("DISPLAY_SIZE", '3.5"') == '2.1"':
272-
# This is a circular screen: apply a circle mask over the preview
273-
display.lcd.screen_image.paste(circular_mask, mask=circular_mask)
274-
screen_image = display.lcd.screen_image
275-
display_image = ImageTk.PhotoImage(
276-
screen_image.resize((int(screen_image.width / RESIZE_FACTOR), int(screen_image.height / RESIZE_FACTOR))))
278+
if not error_in_theme:
279+
screen_image = display.lcd.screen_image
280+
if config.THEME_DATA["display"].get("DISPLAY_SIZE", '3.5"') == '2.1"':
281+
# This is a circular screen: apply a circle mask over the preview
282+
screen_image.paste(circular_mask, mask=circular_mask)
283+
display_image = ImageTk.PhotoImage(
284+
screen_image.resize((int(screen_image.width / RESIZE_FACTOR), int(screen_image.height / RESIZE_FACTOR))))
285+
else:
286+
size = display_width if display_width < display_height else display_height
287+
display_image = ImageTk.PhotoImage(ERROR_IN_THEME.resize((display_width, display_width)))
277288
viewer_picture = tkinter.Label(viewer, image=display_image, borderwidth=0)
278289
viewer_picture.place(x=RGB_LED_MARGIN, y=RGB_LED_MARGIN)
279290

@@ -300,19 +311,29 @@ def on_zone_click(event):
300311
while True:
301312
if os.path.exists(theme_file) and os.path.getmtime(theme_file) > last_edit_time:
302313
logger.debug("The theme file has been updated, the preview window will refresh")
303-
refresh_theme()
314+
try:
315+
refresh_theme()
316+
error_in_theme = False
317+
except Exception as e:
318+
logger.error(f"Error in theme: {e}")
319+
error_in_theme = True
304320
last_edit_time = os.path.getmtime(theme_file)
305321

306322
# Update the preview.png that is in the theme folder
307323
display.lcd.screen_image.save(config.THEME_DATA['PATH'] + "preview.png", "PNG")
308324

309325
# Display new picture
310-
if config.THEME_DATA["display"].get("DISPLAY_SIZE", '3.5"') == '2.1"':
311-
# This is a circular screen: apply a circle mask over the preview
312-
display.lcd.screen_image.paste(circular_mask, mask=circular_mask)
313-
screen_image = display.lcd.screen_image
314-
display_image = ImageTk.PhotoImage(screen_image.resize(
315-
(int(screen_image.width / RESIZE_FACTOR), int(screen_image.height / RESIZE_FACTOR))))
326+
if not error_in_theme:
327+
screen_image = display.lcd.screen_image
328+
if config.THEME_DATA["display"].get("DISPLAY_SIZE", '3.5"') == '2.1"':
329+
# This is a circular screen: apply a circle mask over the preview
330+
screen_image.paste(circular_mask, mask=circular_mask)
331+
display_image = ImageTk.PhotoImage(
332+
screen_image.resize(
333+
(int(screen_image.width / RESIZE_FACTOR), int(screen_image.height / RESIZE_FACTOR))))
334+
else:
335+
size = display_width if display_width < display_height else display_height
336+
display_image = ImageTk.PhotoImage(ERROR_IN_THEME.resize((display_width, display_width)))
316337
viewer_picture.config(image=display_image)
317338

318339
# Refresh RGB backplate LEDs color

0 commit comments

Comments
 (0)