Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make notifications fit line length #45

Merged
merged 1 commit into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions modules/app_components/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<Notification '{self.message}' on port {self._port} ({self._open} - {self._open_time})>"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Loading