-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_screen.py
More file actions
451 lines (384 loc) · 18.4 KB
/
Copy pathclock_screen.py
File metadata and controls
451 lines (384 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
Main clock + weather display screen.
Layout (1024x600):
Left (0..640): Time, AM/PM, Date, Temp/Humidity, Wind/Sunrise
Right (650..1024): Weather icon, Description, City
Bottom (y=358..570): 5-day forecast strip
Bottom bar (y=576..600): Scrolling news ticker
Gear icon: bottom-right
"""
import math
import time
from datetime import datetime
import pygame
import icons as icon_module
import theme as _theme
# ── Non-colour constants ───────────────────────────────────────────────────
TICKER_H = 24
TICKER_Y = 576
TICKER_SEP = " • "
TICKER_SPEED_PPS = 60.0 # pixels per second (≈ 2px × 30 fps)
NIGHT_OVERLAY_ALPHA = 200
def load_font(paths, size):
for p in paths:
try:
return pygame.font.Font(p, size)
except Exception:
pass
return pygame.font.SysFont("sans", size, bold=False)
UBUNTU_BOLD = "/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttf"
UBUNTU_REG = "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf"
UBUNTU_LIGHT = "/usr/share/fonts/truetype/ubuntu/Ubuntu-L.ttf"
ROBOTO_BOLD = "/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf"
ROBOTO_REG = "/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf"
class ClockScreen:
GEAR_RECT = pygame.Rect(980, 566, 40, 32) # bottom-right tap zone
# Spotify player buttons — right panel (x=650–1024, cx=837), y=330–357
# Three 52px-wide buttons centred in the panel; only active when playing.
_SPOT_PREV = pygame.Rect(749, 330, 52, 27)
_SPOT_PLAY = pygame.Rect(811, 330, 52, 27)
_SPOT_NEXT = pygame.Rect(873, 330, 52, 27)
def __init__(self, surface: pygame.Surface, config, weather_service,
news_service=None, spotify_service=None):
self.surface = surface
self.config = config
self.weather = weather_service
self.news = news_service
self.spotify = spotify_service
self.W, self.H = surface.get_size()
# Fonts
self.font_time = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 169)
self.font_ampm = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 62)
self.font_date = load_font([UBUNTU_REG, ROBOTO_REG], 30)
self.font_temp = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 56)
self.font_label = load_font([UBUNTU_REG, ROBOTO_REG], 24)
self.font_cond = load_font([UBUNTU_REG, ROBOTO_REG], 26)
self.font_city = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 28)
self.font_fc_day = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 20)
self.font_fc_tmp = load_font([UBUNTU_BOLD, ROBOTO_BOLD], 18)
self.font_fc_pop = load_font([UBUNTU_REG, ROBOTO_REG], 16)
self.font_small = load_font([UBUNTU_REG, ROBOTO_REG], 20)
self.font_ticker = load_font([UBUNTU_REG, ROBOTO_REG], 18)
self.font_sp_title = load_font([UBUNTU_REG, ROBOTO_REG], 16)
self.font_sp_artist = load_font([UBUNTU_REG, ROBOTO_REG], 14)
self._colon_visible = True
self._last_sec = -1
self._night_surface = None
# Ticker state
self._ticker_surf = None
self._ticker_x: float = float(self.W)
self._ticker_heads: list[str] = []
self._ticker_theme_key = "" # invalidate surf on theme change
self._ticker_last_t: float = 0.0
# Current palette (updated each draw)
self._p = _theme.DARK
def on_enter(self):
pass
def handle_event(self, event):
pos = None
if event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
elif event.type == pygame.FINGERDOWN:
pos = (int(event.x * self.W), int(event.y * self.H))
if pos:
if self.GEAR_RECT.collidepoint(pos):
return "settings"
if self.spotify and self.spotify.get_current():
if self._SPOT_PREV.collidepoint(pos):
self.spotify.prev_track()
elif self._SPOT_PLAY.collidepoint(pos):
self.spotify.play_pause()
elif self._SPOT_NEXT.collidepoint(pos):
self.spotify.next_track()
return None
def update(self):
now = datetime.now()
sec = now.second
if sec != self._last_sec:
self._colon_visible = not self._colon_visible
self._last_sec = sec
self._update_ticker()
def draw(self):
now = datetime.now()
self._p = _theme.get_palette(self.config, now)
self.surface.fill(self._p['BG'])
self._draw_time(now)
self._draw_date(now)
self._draw_weather_stats(now)
self._draw_divider()
self._draw_weather_right()
self._draw_spotify_widget()
self._draw_forecast()
self._draw_ticker()
self._draw_gear()
self._apply_night_mode(now)
# Stale weather warning
current = self.weather.get_current()
if current and self.weather.is_stale():
self._draw_text("! Weather offline", self.font_small,
(255, 120, 60), 10, self.H - 22)
# ── Private drawing helpers ────────────────────────────────────────────
def _draw_text(self, text, font, color, x, y, alpha=255):
surf = font.render(text, True, color)
if alpha < 255:
surf.set_alpha(alpha)
self.surface.blit(surf, (x, y))
return surf.get_width()
def _draw_text_centered(self, text, font, color, cx, y):
surf = font.render(text, True, color)
self.surface.blit(surf, (cx - surf.get_width() // 2, y))
def _draw_time(self, now):
p = self._p
use_24h = self.config.get("clock_24h", False)
if use_24h:
h = now.hour
ampm = None
else:
h = now.hour % 12 or 12
ampm = "AM" if now.hour < 12 else "PM"
m = now.minute
x = 28
y = 18
w = self._draw_text(f"{h:02d}", self.font_time, p['WHITE'], x, y)
x += w
colon_color = p['WHITE'] if self._colon_visible else p['COLON_DIM']
w = self._draw_text(":", self.font_time, colon_color, x, y)
x += w
w = self._draw_text(f"{m:02d}", self.font_time, p['WHITE'], x, y)
x += w + 10
if ampm:
self._draw_text(ampm, self.font_ampm, p['GRAY'], x, y + 18)
def _draw_date(self, now):
p = self._p
date_str = now.strftime("%a, %d %B %Y")
self._draw_text(date_str, self.font_date, p['GRAY'], 30, 192)
def _draw_weather_stats(self, now):
p = self._p
current = self.weather.get_current()
sunrise, sunset = self.weather.get_sun_times()
units = self.config.get("units", "metric")
deg = "°C" if units == "metric" else "°F"
y = 238
if current:
temp_str = f"{current['temp']}{deg}"
humid_str = f"{current['humidity']}%"
# Thermometer dot + bar
pygame.draw.circle(self.surface, p['TEMP_COLOR'], (38, y + 28), 6)
pygame.draw.rect(self.surface, p['TEMP_COLOR'], (35, y + 2, 6, 26), border_radius=3)
self._draw_text(temp_str, self.font_temp, p['WHITE'], 54, y - 4)
# Humidity raindrop
hx = 230
pygame.draw.circle(self.surface, p['HUMID_COLOR'], (hx + 10, y + 14), 5)
pygame.draw.polygon(self.surface, p['HUMID_COLOR'], [
(hx + 10, y + 1), (hx + 5, y + 14), (hx + 15, y + 14)
])
w = self._draw_text(humid_str, self.font_date, p['HUMID_COLOR'], hx + 22, y + 6)
# Wind speed
wind_str = f"{current['wind_speed']} m/s"
wx = hx + w + 50
pygame.draw.polygon(self.surface, p['WIND_COLOR'],
[(wx + 7, y + 5), (wx + 3, y + 14), (wx + 11, y + 14)])
self._draw_text(wind_str, self.font_date, p['WIND_COLOR'], wx + 16, y + 6)
if sunrise and sunset:
sy = y + 52
sr_str = sunrise.strftime("%-H:%M")
ss_str = sunset.strftime("%-H:%M")
# Sunrise half-circle
scx, scy = 38, sy + 12
pygame.draw.circle(self.surface, p['SUN_COLOR'], (scx, scy + 4), 10, 0)
pygame.draw.rect(self.surface, p['BG'], (scx - 12, scy + 4, 24, 16))
pygame.draw.line(self.surface, p['SUN_COLOR'], (scx - 13, scy + 4), (scx + 13, scy + 4), 2)
pygame.draw.polygon(self.surface, p['SUN_COLOR'],
[(62, sy + 3), (58, sy + 14), (66, sy + 14)])
self._draw_text(sr_str, self.font_label, p['SUN_COLOR'], 70, sy + 3)
# Sunset down-triangle
pygame.draw.polygon(self.surface, (180, 160, 100),
[(179, sy + 14), (175, sy + 3), (183, sy + 3)])
self._draw_text(ss_str, self.font_label, (180, 160, 100), 188, sy + 3)
def _draw_divider(self):
p = self._p
pygame.draw.line(self.surface, p['DIVIDER'], (645, 20), (645, 345), 1)
def _draw_weather_right(self):
p = self._p
current = self.weather.get_current()
rx = 650
rw = self.W - rx
cx = rx + rw // 2
if not current:
self._draw_text_centered("No weather data", self.font_label, p['DIM_GRAY'], cx, 160)
return
icon_surf = icon_module.get_icon(current["icon"], 155)
ix = cx - icon_surf.get_width() // 2
self.surface.blit(icon_surf, (ix, 25))
self._draw_text_centered(current["description"], self.font_cond, p['GRAY'], cx, 194)
city_str = f" {current['city']}, {current['country']}"
city_surf = self.font_city.render(city_str, True, p['WHITE'])
pcx = cx - city_surf.get_width() // 2
pygame.draw.circle(self.surface, p['ACCENT'], (pcx - 6, 232), 5)
self.surface.blit(city_surf, (pcx + 2, 222))
self._draw_text_centered(
f"Feels {current['feels_like']}° · Wind {current['wind_speed']} m/s",
self.font_label, p['DIM_GRAY'], cx, 264
)
def _draw_spotify_widget(self):
"""Now Playing widget in the right panel, y=290–357."""
if not self.spotify or not self.spotify.is_authenticated():
return
track = self.spotify.get_current()
if not track:
return
p = self._p
rx = 650
rw = self.W - rx
cx = rx + rw // 2
# Subtle divider above the widget
pygame.draw.line(self.surface, p['DIVIDER'], (rx + 10, 290), (self.W - 10, 290), 1)
# Music note dot
pygame.draw.circle(self.surface, p['ACCENT'], (rx + 18, 303), 5)
# Track title (truncated)
max_w = rw - 40
title = self._sp_truncate(track["title"], self.font_sp_title, max_w)
self._draw_text(title, self.font_sp_title, p['WHITE'], rx + 30, 295)
# Artist (dimmer)
artist = self._sp_truncate(track["artist"], self.font_sp_artist, max_w)
self._draw_text(artist, self.font_sp_artist, p['DIM_GRAY'], rx + 30, 315)
# Buttons: prev play/pause next
for rect, kind in (
(self._SPOT_PREV, "prev"),
(self._SPOT_PLAY, "pause" if track["is_playing"] else "play"),
(self._SPOT_NEXT, "next"),
):
bg = p.get('BTN_SEL', (60, 60, 80))
pygame.draw.rect(self.surface, bg, rect, border_radius=5)
self._draw_sp_symbol(kind, rect.centerx, rect.centery, p['WHITE'])
def _sp_truncate(self, text, font, max_width):
if font.size(text)[0] <= max_width:
return text
while text and font.size(text + "…")[0] > max_width:
text = text[:-1]
return text + "…"
def _draw_sp_symbol(self, kind, cx, cy, color):
"""Draw prev/play/pause/next icons centred at (cx, cy)."""
if kind == "play":
# Right-pointing triangle
pygame.draw.polygon(self.surface, color,
[(cx - 5, cy - 7), (cx - 5, cy + 7), (cx + 7, cy)])
elif kind == "pause":
# Two vertical bars
pygame.draw.rect(self.surface, color, (cx - 6, cy - 6, 4, 12))
pygame.draw.rect(self.surface, color, (cx + 2, cy - 6, 4, 12))
elif kind == "prev":
# Vertical bar + left triangle
pygame.draw.rect(self.surface, color, (cx - 7, cy - 6, 3, 12))
pygame.draw.polygon(self.surface, color,
[(cx + 5, cy - 6), (cx + 5, cy + 6), (cx - 3, cy)])
elif kind == "next":
# Right triangle + vertical bar
pygame.draw.polygon(self.surface, color,
[(cx - 5, cy - 6), (cx - 5, cy + 6), (cx + 3, cy)])
pygame.draw.rect(self.surface, color, (cx + 4, cy - 6, 3, 12))
def _draw_forecast(self):
p = self._p
forecast = self.weather.get_forecast()
if not forecast:
return
strip_y = 358
strip_h = self.H - strip_y - 30
pygame.draw.rect(self.surface, p['FORECAST_BG'],
(0, strip_y, self.W, strip_h))
pygame.draw.line(self.surface, p['DIVIDER'], (0, strip_y), (self.W, strip_y), 1)
n = min(len(forecast), 5)
col_w = self.W // n
deg = "°"
for i, day in enumerate(forecast[:n]):
x = i * col_w
cx = x + col_w // 2
if i < n - 1:
pygame.draw.line(self.surface, p['DIVIDER'],
(x + col_w - 1, strip_y + 8),
(x + col_w - 1, self.H - 35), 1)
self._draw_text_centered(day["day"], self.font_fc_day, p['GRAY'], cx, strip_y + 8)
ic = icon_module.get_icon(day["icon"], 52)
self.surface.blit(ic, (cx - 26, strip_y + 34))
hi_str = f"{day['high']}{deg}"
lo_str = f"{day['low']}{deg}"
ty = strip_y + 97
hi_w = self.font_fc_tmp.size(hi_str)[0]
hi_x = cx - hi_w // 2 - 14
pygame.draw.polygon(self.surface, p['WHITE'],
[(hi_x + 5, ty), (hi_x, ty + 8), (hi_x + 10, ty + 8)])
self._draw_text(hi_str, self.font_fc_tmp, p['WHITE'], hi_x + 13, ty - 1)
lo_w = self.font_fc_tmp.size(lo_str)[0]
lo_x = cx + lo_w // 2 + 4
pygame.draw.polygon(self.surface, p['DIM_GRAY'],
[(lo_x + 5, ty + 8), (lo_x, ty), (lo_x + 10, ty)])
self._draw_text(lo_str, self.font_fc_tmp, p['DIM_GRAY'], lo_x + 13, ty - 1)
pop_color = p['HUMID_COLOR'] if day["pop"] > 50 else p['DIM_GRAY']
pop_str = f"{day['pop']}%"
pw = self.font_fc_pop.size(pop_str)[0]
drop_x = cx - pw // 2 - 8
drop_y = strip_y + 123
pygame.draw.circle(self.surface, pop_color, (drop_x, drop_y + 5), 4)
pygame.draw.polygon(self.surface, pop_color,
[(drop_x, drop_y - 2), (drop_x - 4, drop_y + 5), (drop_x + 4, drop_y + 5)])
self._draw_text(pop_str, self.font_fc_pop, pop_color, drop_x + 7, strip_y + 118)
def _update_ticker(self):
if self.news is None:
return
heads = self.news.get_headlines()
if not heads:
return
theme_key = self.config.get('theme', 'dark')
# Rebuild surface when headlines or theme changes
if heads != self._ticker_heads or theme_key != self._ticker_theme_key:
self._ticker_heads = heads
self._ticker_theme_key = theme_key
pal = _theme.get_palette(self.config)
joined = TICKER_SEP.join(heads) + TICKER_SEP
self._ticker_surf = self.font_ticker.render(joined, True, pal['TICKER_COLOR'])
self._ticker_x = float(self.W)
self._ticker_last_t = time.monotonic()
if self._ticker_surf is not None:
now_t = time.monotonic()
dt = now_t - self._ticker_last_t
self._ticker_last_t = now_t
self._ticker_x -= TICKER_SPEED_PPS * dt
if self._ticker_x < -self._ticker_surf.get_width():
self._ticker_x = float(self.W)
def _draw_ticker(self):
if self.news is None or self._ticker_surf is None:
return
p = self._p
pygame.draw.rect(self.surface, p['TICKER_BG'],
(0, TICKER_Y, self.W, TICKER_H))
pygame.draw.line(self.surface, p['DIVIDER'],
(0, TICKER_Y), (self.W, TICKER_Y), 1)
clip = pygame.Rect(0, TICKER_Y, self.W, TICKER_H)
self.surface.set_clip(clip)
ty = TICKER_Y + (TICKER_H - self._ticker_surf.get_height()) // 2
self.surface.blit(self._ticker_surf, (int(self._ticker_x), ty))
self.surface.set_clip(None)
def _draw_gear(self):
p = self._p
gcx, gcy = 997, 583
outer_r, inner_r, tooth = 11, 6, 4
pygame.draw.circle(self.surface, p['GEAR_COLOR'], (gcx, gcy), outer_r, 2)
pygame.draw.circle(self.surface, p['GEAR_COLOR'], (gcx, gcy), inner_r, 2)
for i in range(8):
angle = i * 3.14159 / 4
tx = gcx + int((outer_r + tooth) * math.cos(angle))
ty = gcy + int((outer_r + tooth) * math.sin(angle))
pygame.draw.circle(self.surface, p['GEAR_COLOR'], (tx, ty), 3)
def _apply_night_mode(self, now):
if not self.config.get("night_mode", True):
return
h = now.hour
ns = self.config.get("night_start", 23)
ne = self.config.get("night_end", 7)
nb = self.config.get("night_brightness", 15)
if h >= ns or h < ne:
alpha = int((1 - nb / 100) * NIGHT_OVERLAY_ALPHA)
if self._night_surface is None or self._night_surface.get_size() != (self.W, self.H):
self._night_surface = pygame.Surface((self.W, self.H), pygame.SRCALPHA)
self._night_surface.fill((0, 0, 0, alpha))
self.surface.blit(self._night_surface, (0, 0))