diff --git a/modules/app_components/notification.py b/modules/app_components/notification.py index fb9f9de..547d289 100644 --- a/modules/app_components/notification.py +++ b/modules/app_components/notification.py @@ -13,6 +13,7 @@ def __init__(self, message, port=0, open=True): self._animation_target = 1 if open else 0 self._open_time = 0 self._close_after = 1000 * 3 + self.width_limits = [120, 180, 220, 240, 240, 240, 180, 120] def __repr__(self): return f"" @@ -41,6 +42,21 @@ def update(self, delta): if self._open and self._open_time > self._close_after: self.close() + def get_text_for_line(self, ctx, text, line): + width_for_line = 240 + if line < (len(self.width_limits)): + width_for_line = self.width_limits[line] + + extra_text = "" + text_that_fits = text + text_width = ctx.text_width(text_that_fits) + while text_width > width_for_line: + character = text_that_fits[-1] + text_that_fits = text_that_fits[:-1] + extra_text = character + extra_text + text_width = ctx.text_width(text_that_fits) + return text_that_fits, extra_text + def draw(self, ctx): if not self._is_closed(): ctx.save() @@ -52,15 +68,25 @@ def draw(self, ctx): if self._port != 0: ctx.rotate(self._half_hex_rotation * (self._port * 2 - 1)) + lines = [] + extra_text = self.message + line = 0 + while extra_text: + text_that_fits, extra_text = self.get_text_for_line(ctx, extra_text, line) + lines.append(text_that_fits) + line = line + 1 + set_color(ctx, "mid_green") - ctx.rectangle(-120, -150 - self._animation_state * -30, 240, 30).fill() + ctx.rectangle(-120, -150 - 30 * (len(lines)-1) - (self._animation_state * -30 * len(lines)), 240, 30 * len(lines)).fill() if self._port != 0: ctx.rotate(3.14) set_color(ctx, "label") - ctx.move_to(0, 135 + self._animation_state * -30).text(self.message) + for i in range(len(lines)): + ctx.move_to(0, 135 - 30* (len(lines)-1) - (self._animation_state * -30 * len(lines)) + 30*i).text(lines[i]) else: set_color(ctx, "label") - ctx.move_to(0, -130 - self._animation_state * -30).text(self.message) + for i in range(len(lines)): + ctx.move_to(0, -130 - 30* (len(lines)-1) - (self._animation_state * -30 * len(lines)) + 30*i).text(lines[i]) ctx.restore()