"""Simple full-screen clock.""" import time from PIL import Image, ImageDraw from screens.base import Screen from screens.render_utils import format_date_de, load_font, text_width LINE_GAP = 0.04 # fraction of min(width, height) between the time and date lines class ClockScreen(Screen): id = "clock" display_name = "Clock" poll_interval = 30 def check(self, settings: dict): return time.strftime("%H:%M") def render(self, state, settings: dict, resolution: tuple[int, int]) -> Image.Image: width, height = resolution image = Image.new("RGB", (width, height), "white") draw = ImageDraw.Draw(image) now = time.localtime() time_str = state or time.strftime("%H:%M", now) date_str = format_date_de(now) scale = min(width, height) time_font = load_font(size=int(scale * 0.32), bold=True) date_font = load_font(size=int(scale * 0.06)) gap = int(scale * LINE_GAP) time_left, time_top, time_right, time_bottom = draw.textbbox((0, 0), time_str, font=time_font) time_h = time_bottom - time_top date_left, date_top, date_right, date_bottom = draw.textbbox((0, 0), date_str, font=date_font) date_h = date_bottom - date_top block_h = time_h + gap + date_h block_top = (height - block_h) / 2 tw = time_right - time_left draw.text(((width - tw) / 2, block_top - time_top), time_str, font=time_font, fill="black") dw = date_right - date_left draw.text(((width - dw) / 2, block_top + time_h + gap - date_top), date_str, font=date_font, fill="black") return image