Improve calendar text wrapping and use smaller font for summaries

This commit is contained in:
ChuckBuilds
2025-04-22 15:03:46 -05:00
parent 367d014757
commit 5391f60f0c

View File

@@ -124,22 +124,23 @@ class CalendarManager:
time_str = self._format_event_time(event) time_str = self._format_event_time(event)
date_str = self._format_event_date(event) date_str = self._format_event_date(event)
# Use display manager's font for wrapping # Use different fonts for header and summary
font = self.display_manager.small_font header_font = self.display_manager.small_font
summary_font = self.display_manager.extra_small_font
available_width = self.display_manager.matrix.width - 4 # Leave 2 pixel margin on each side available_width = self.display_manager.matrix.width - 4 # Leave 2 pixel margin on each side
# Draw date and time on top line # Draw date and time on top line
datetime_str = f"{date_str} {time_str}" datetime_str = f"{date_str} {time_str}"
self.display_manager.draw_text(datetime_str, y=2, color=self.text_color, small_font=True) self.display_manager.draw_text(datetime_str, y=2, color=self.text_color, small_font=True)
# Wrap summary text for two lines # Wrap summary text for two lines using extra small font
title_lines = self._wrap_text(summary, available_width, font, max_lines=2) title_lines = self._wrap_text(summary, available_width, summary_font, max_lines=2)
# Draw summary lines # Draw summary lines
y_pos = 12 # Start position for summary (below date/time) y_pos = 12 # Start position for summary (below date/time)
for line in title_lines: for line in title_lines:
self.display_manager.draw_text(line, y=y_pos, color=self.text_color, small_font=True) self.display_manager.draw_text(line, y=y_pos, color=self.text_color, extra_small_font=True)
y_pos += 8 # Move down for next line y_pos += 6 # Move down less for extra small font
return True return True
@@ -153,53 +154,58 @@ class CalendarManager:
return [""] return [""]
lines = [] lines = []
words = text.split()
current_line = [] current_line = []
words = text.split()
for word in words: for word in words:
test_line = ' '.join(current_line + [word]) # Try adding the word to the current line
# Use textbbox for accurate width calculation test_line = ' '.join(current_line + [word]) if current_line else word
bbox = self.display_manager.draw.textbbox((0, 0), test_line, font=font) bbox = self.display_manager.draw.textbbox((0, 0), test_line, font=font)
text_width = bbox[2] - bbox[0] text_width = bbox[2] - bbox[0]
if text_width <= max_width: if text_width <= max_width:
# Word fits, add it to current line
current_line.append(word) current_line.append(word)
else: else:
# Word doesn't fit, start a new line
if current_line: if current_line:
lines.append(' '.join(current_line)) lines.append(' '.join(current_line))
current_line = [word] current_line = [word]
else: else:
# Word is too long for the line, truncate it # Single word too long, truncate it
lines.append(word[:10] + "...") truncated = word
while len(truncated) > 0:
bbox = self.display_manager.draw.textbbox((0, 0), truncated + "...", font=font)
if bbox[2] - bbox[0] <= max_width:
lines.append(truncated + "...")
break
truncated = truncated[:-1]
if not truncated:
lines.append(word[:10] + "...")
# Check if we've reached max lines # Check if we've filled all lines
if len(lines) >= max_lines - 1 and current_line: if len(lines) >= max_lines:
# For the last line, add ellipsis if there are more words
test_line = ' '.join(current_line + [word])
if len(words) > words.index(word) + 1:
test_line += "..."
# Check if the line with ellipsis fits
bbox = self.display_manager.draw.textbbox((0, 0), test_line, font=font)
if bbox[2] - bbox[0] <= max_width:
lines.append(test_line)
else:
# If it doesn't fit, truncate the last line
last_line = ' '.join(current_line)
if len(last_line) > 10:
last_line = last_line[:10] + "..."
lines.append(last_line)
break break
# Add the last line if we haven't hit max_lines # Handle any remaining text in current_line
if current_line and len(lines) < max_lines: if current_line and len(lines) < max_lines:
lines.append(' '.join(current_line)) remaining_text = ' '.join(current_line)
if len(words) > len(current_line): # More words remain
# Try to fit with ellipsis
while len(remaining_text) > 0:
bbox = self.display_manager.draw.textbbox((0, 0), remaining_text + "...", font=font)
if bbox[2] - bbox[0] <= max_width:
lines.append(remaining_text + "...")
break
remaining_text = remaining_text[:-1]
else:
lines.append(remaining_text)
# If we only have one line, pad with an empty line # Ensure we have exactly max_lines
if len(lines) == 1: while len(lines) < max_lines:
lines.append("") lines.append("")
return lines return lines[:max_lines]
def update(self, current_time): def update(self, current_time):
"""Update calendar events if needed.""" """Update calendar events if needed."""