mycue 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mycue/__init__.py +61 -0
- mycue/app.py +477 -0
- mycue/canvas.py +145 -0
- mycue/cli.py +185 -0
- mycue/device.py +255 -0
- mycue/display.py +613 -0
- mycue/issues.py +70 -0
- mycue/launchd.py +89 -0
- mycue/logpage.py +107 -0
- mycue/media.py +116 -0
- mycue/nexus-launch.c +31 -0
- mycue/poll.py +102 -0
- mycue/popup.py +155 -0
- mycue/py.typed +0 -0
- mycue/render.py +514 -0
- mycue/run.py +85 -0
- mycue/tabs.py +179 -0
- mycue/theme.py +247 -0
- mycue/widgets.py +241 -0
- mycue-0.2.0.dist-info/METADATA +314 -0
- mycue-0.2.0.dist-info/RECORD +26 -0
- mycue-0.2.0.dist-info/WHEEL +4 -0
- mycue-0.2.0.dist-info/entry_points.txt +2 -0
- mycue-0.2.0.dist-info/licenses/LICENSE +21 -0
- mycue-0.2.0.dist-info/licenses/NOTICE.md +16 -0
- mycue-0.2.0.dist-info/licenses/licenses/NexusTool-MIT.txt +21 -0
mycue/__init__.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""mycue: drive the Corsair iCUE Nexus touchscreen from Python, and build glanceable apps on it."""
|
|
2
|
+
|
|
3
|
+
from mycue.app import CLOSE, App, Banner, Page
|
|
4
|
+
from mycue.canvas import Canvas, Sketch
|
|
5
|
+
from mycue.device import Calibration, Gesture, Nexus, NexusNotFound, Touch
|
|
6
|
+
from mycue.display import Display, Log
|
|
7
|
+
from mycue.issues import Issues, IssuesLog, issues_widget
|
|
8
|
+
from mycue.logpage import Row, ScrollLog
|
|
9
|
+
from mycue.media import Picture
|
|
10
|
+
from mycue.poll import Poller
|
|
11
|
+
from mycue.popup import Popup
|
|
12
|
+
from mycue.render import Tile
|
|
13
|
+
from mycue.run import run, serve
|
|
14
|
+
from mycue.tabs import Slide, Tab
|
|
15
|
+
from mycue.theme import Theme
|
|
16
|
+
from mycue.widgets import (
|
|
17
|
+
ButtonWidget,
|
|
18
|
+
ClockWidget,
|
|
19
|
+
MessageWidget,
|
|
20
|
+
PictureWidget,
|
|
21
|
+
StatWidget,
|
|
22
|
+
Widget,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__version__ = "0.2.0"
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"Calibration",
|
|
29
|
+
"Canvas",
|
|
30
|
+
"CLOSE",
|
|
31
|
+
"App",
|
|
32
|
+
"Banner",
|
|
33
|
+
"ButtonWidget",
|
|
34
|
+
"ClockWidget",
|
|
35
|
+
"Display",
|
|
36
|
+
"Gesture",
|
|
37
|
+
"Issues",
|
|
38
|
+
"IssuesLog",
|
|
39
|
+
"Log",
|
|
40
|
+
"MessageWidget",
|
|
41
|
+
"Nexus",
|
|
42
|
+
"NexusNotFound",
|
|
43
|
+
"Page",
|
|
44
|
+
"Picture",
|
|
45
|
+
"PictureWidget",
|
|
46
|
+
"Popup",
|
|
47
|
+
"Poller",
|
|
48
|
+
"Row",
|
|
49
|
+
"ScrollLog",
|
|
50
|
+
"Sketch",
|
|
51
|
+
"Slide",
|
|
52
|
+
"StatWidget",
|
|
53
|
+
"Tab",
|
|
54
|
+
"Theme",
|
|
55
|
+
"Tile",
|
|
56
|
+
"Touch",
|
|
57
|
+
"Widget",
|
|
58
|
+
"issues_widget",
|
|
59
|
+
"run",
|
|
60
|
+
"serve",
|
|
61
|
+
]
|
mycue/app.py
ADDED
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
"""Pages, swiping between them, pop-up pages opened by a tap, and a priority banner."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import time
|
|
7
|
+
from collections.abc import Callable
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
|
|
10
|
+
from PIL import Image, ImageDraw
|
|
11
|
+
|
|
12
|
+
from mycue import render as r
|
|
13
|
+
from mycue.device import Gesture
|
|
14
|
+
from mycue.theme import DEFAULT, TRANSITIONS, Theme # noqa: F401 - TRANSITIONS re-exported
|
|
15
|
+
|
|
16
|
+
log = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
CLOSE = object() # returned from a pop-up page's tap to close it
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ease(p: float) -> float:
|
|
22
|
+
return 1 - (1 - p) ** 3
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Page:
|
|
26
|
+
name = "page"
|
|
27
|
+
|
|
28
|
+
def draw(self, img: Image.Image, d: ImageDraw.ImageDraw, now: float) -> None:
|
|
29
|
+
raise NotImplementedError
|
|
30
|
+
|
|
31
|
+
def visible(self) -> bool:
|
|
32
|
+
"""False hides this page from the swipe order for now."""
|
|
33
|
+
return True
|
|
34
|
+
|
|
35
|
+
def opened(self, now: float) -> None:
|
|
36
|
+
"""Called when this page opens as a pop-up."""
|
|
37
|
+
|
|
38
|
+
def tap(self, x: int, now: float = 0.0) -> Page | object | None:
|
|
39
|
+
"""Handle a tap; return a page to open on top, CLOSE to close this pop-up, or None."""
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
def swipe(self, kind: str, now: float = 0.0) -> bool:
|
|
43
|
+
"""Handle a swipe inside the page; True when it was used (no page change)."""
|
|
44
|
+
return False
|
|
45
|
+
|
|
46
|
+
def expired(self, now: float) -> bool:
|
|
47
|
+
"""For a pop-up page: True once it should close by itself."""
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class Banner:
|
|
52
|
+
"""A full-screen alert that takes over until tapped away, keyed by what it is about.
|
|
53
|
+
|
|
54
|
+
confirm: hide on a second tap, the first showing "tap again" (Theme.alert_confirm when
|
|
55
|
+
None). dismiss: a function; while it returns true the banner is hidden as if tapped.
|
|
56
|
+
switch: for a tab's alert, jump to that tab when it starts holding (Theme.alert_switch
|
|
57
|
+
when None)."""
|
|
58
|
+
|
|
59
|
+
def __init__(
|
|
60
|
+
self,
|
|
61
|
+
key: str,
|
|
62
|
+
text: str,
|
|
63
|
+
sub: str = "",
|
|
64
|
+
tone: str | None = None,
|
|
65
|
+
confirm: bool | None = None,
|
|
66
|
+
dismiss: Callable[[], bool] | None = None,
|
|
67
|
+
switch: bool | None = None,
|
|
68
|
+
) -> None:
|
|
69
|
+
self.key, self.text, self.sub, self._tone = key, text, sub, tone
|
|
70
|
+
self.confirm, self.dismiss, self._switch = confirm, dismiss, switch
|
|
71
|
+
self.armed = False # set while drawn: a first tap is waiting for the second
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def tone(self) -> str:
|
|
75
|
+
"""The banner's colour: its own, or Theme.alert_tone."""
|
|
76
|
+
return r.theme().alert_tone if self._tone is None else self._tone
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def switch(self) -> bool:
|
|
80
|
+
"""For a tab's alert: jump to the tab when it starts holding (Theme.alert_switch)."""
|
|
81
|
+
return r.theme().alert_switch if self._switch is None else self._switch
|
|
82
|
+
|
|
83
|
+
def confirms(self) -> bool:
|
|
84
|
+
return r.theme().alert_confirm if self.confirm is None else self.confirm
|
|
85
|
+
|
|
86
|
+
def hint(self) -> str:
|
|
87
|
+
if self.armed:
|
|
88
|
+
return "TAP AGAIN TO HIDE"
|
|
89
|
+
return "TAP TWICE TO HIDE" if self.confirms() else "TAP TO HIDE"
|
|
90
|
+
|
|
91
|
+
def draw(self, img: Image.Image, d: ImageDraw.ImageDraw, now: float) -> None:
|
|
92
|
+
"""A dark card tinted toward the alert's colour, a stripe and badge in full colour,
|
|
93
|
+
and the words in text inks: urgent without a wall of saturated red."""
|
|
94
|
+
t = r.theme()
|
|
95
|
+
colour = r.ink(self.tone)
|
|
96
|
+
x0, x1 = t.margin, r.WIDTH - t.margin
|
|
97
|
+
r.card(d, x0, x1, fill=r.mix(t.panel, colour, 0.18))
|
|
98
|
+
d.rounded_rectangle([x0, t.top, x0 + 5, t.bottom], radius=3, fill=colour)
|
|
99
|
+
cx, cy = x0 + 26, (t.top + t.bottom) // 2
|
|
100
|
+
d.ellipse([cx - 11, cy - 11, cx + 11, cy + 11], fill=colour)
|
|
101
|
+
bang = r.font(17, style="Bold")
|
|
102
|
+
d.text((cx, cy), "!", font=bang, fill=t.bg, anchor="mm")
|
|
103
|
+
tx = cx + 20
|
|
104
|
+
hint = r.font(9, style=t.label_style)
|
|
105
|
+
width = x1 - tx - 20 - int(d.textlength(self.hint(), font=hint)) # clear of the hint
|
|
106
|
+
size = r.fit(d, self.text, 22, width, style=t.title_style)
|
|
107
|
+
d.text((tx, t.top + (2 if self.sub else 9)), self.text,
|
|
108
|
+
font=r.font(size, style=t.title_style), fill=t.text) # fmt: skip
|
|
109
|
+
if self.sub:
|
|
110
|
+
sub, ssize = r.clip(d, self.sub, 11, width, style=t.text_style)
|
|
111
|
+
d.text((tx, t.top + 27), sub, font=r.font(ssize, style=t.text_style), fill=t.dim)
|
|
112
|
+
d.text((x1 - 10, cy), self.hint(), font=hint, fill=r.mix(colour, t.text, 0.3), anchor="rm")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class Alerts:
|
|
116
|
+
"""Alert functions and what has happened to their banner: when it was first shown, whether
|
|
117
|
+
it was tapped away, and a first tap waiting for its second. Earlier alerts win."""
|
|
118
|
+
|
|
119
|
+
def __init__(self, fns: list[Callable[[], Banner | None]] | None = None) -> None:
|
|
120
|
+
self.fns = fns if fns is not None else []
|
|
121
|
+
self.key: str | None = None # the banner holding
|
|
122
|
+
self.shown_at: float | None = None
|
|
123
|
+
self.dismissed: str | None = None
|
|
124
|
+
self.armed_at: float | None = None
|
|
125
|
+
self.switched: str | None = None # the key a switch to this tab was made for
|
|
126
|
+
|
|
127
|
+
def first(self) -> Banner | None:
|
|
128
|
+
for fn in self.fns:
|
|
129
|
+
b = fn()
|
|
130
|
+
if b is not None:
|
|
131
|
+
return b
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
def holding(self, now: float, shown: bool, timeout: float | None) -> Banner | None:
|
|
135
|
+
"""The banner to show, or None when nothing holds or it was hidden. shown: whether it
|
|
136
|
+
is on screen now, which starts its timeout."""
|
|
137
|
+
b = self.first()
|
|
138
|
+
if b is None:
|
|
139
|
+
self.key = self.shown_at = self.dismissed = self.armed_at = self.switched = None
|
|
140
|
+
return None
|
|
141
|
+
if b.key != self.key:
|
|
142
|
+
self.key, self.shown_at, self.armed_at = b.key, None, None
|
|
143
|
+
if b.dismiss is not None and _true(b.dismiss):
|
|
144
|
+
self.dismissed = b.key
|
|
145
|
+
if shown and self.shown_at is None:
|
|
146
|
+
self.shown_at = now
|
|
147
|
+
if timeout is not None and self.shown_at is not None and now - self.shown_at > timeout:
|
|
148
|
+
self.dismissed = b.key
|
|
149
|
+
if b.key == self.dismissed:
|
|
150
|
+
return None
|
|
151
|
+
if self.armed_at is not None and now - self.armed_at > r.theme().confirm_for:
|
|
152
|
+
self.armed_at = None
|
|
153
|
+
b.armed = self.armed_at is not None
|
|
154
|
+
return b
|
|
155
|
+
|
|
156
|
+
def tap(self, b: Banner, now: float) -> None:
|
|
157
|
+
"""A tap on the banner: hide it, or with confirm arm it for a second tap."""
|
|
158
|
+
if b.confirms() and self.armed_at is None:
|
|
159
|
+
self.armed_at = now
|
|
160
|
+
else:
|
|
161
|
+
self.dismissed, self.armed_at = b.key, None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _true(fn: Callable[[], bool]) -> bool:
|
|
165
|
+
try:
|
|
166
|
+
return bool(fn())
|
|
167
|
+
except Exception as e: # noqa: BLE001 - a failing check never takes the display down
|
|
168
|
+
log.debug("alert dismiss check failed: %s", e)
|
|
169
|
+
return False
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class ClockPage(Page):
|
|
173
|
+
name = "clock"
|
|
174
|
+
|
|
175
|
+
def draw(self, img, d, now):
|
|
176
|
+
t = datetime.fromtimestamp(now)
|
|
177
|
+
r.title(d, t.strftime("%a"), t.strftime("%d %b"))
|
|
178
|
+
th = r.theme()
|
|
179
|
+
r.card(d, r.content_left(), r.content_right())
|
|
180
|
+
d.text((r.content_left() + 10, th.top + 1), t.strftime("%H:%M"), font=r.value_font(34),
|
|
181
|
+
fill=th.text) # fmt: skip
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class App:
|
|
185
|
+
def __init__(
|
|
186
|
+
self,
|
|
187
|
+
pages: list[Page] | Callable[[], list[Page]] | None = None,
|
|
188
|
+
banner: Callable[[], Banner | None] | None = None,
|
|
189
|
+
clock: Callable[[], float] = time.time,
|
|
190
|
+
theme: Theme | None = None,
|
|
191
|
+
banner_timeout: float | None | object = DEFAULT,
|
|
192
|
+
) -> None:
|
|
193
|
+
"""Tabs are added with add_tab and alerts with add_alert; the swipe order is the tabs
|
|
194
|
+
whose visible() is true, asked again before each frame and gesture. pages may instead
|
|
195
|
+
be a fixed list or a function returning one.
|
|
196
|
+
|
|
197
|
+
theme is what everything draws in (the default Theme() when None). banner_timeout is
|
|
198
|
+
how long a banner shows before hiding as if tapped; None keeps it up until tapped; left
|
|
199
|
+
out, the theme's alert_timeout.
|
|
200
|
+
"""
|
|
201
|
+
self.theme = theme or Theme()
|
|
202
|
+
self._banner_timeout = banner_timeout
|
|
203
|
+
self.tabs: list[Page] = []
|
|
204
|
+
self.alerts: list[Callable[[], Banner | None]] = [banner] if banner else []
|
|
205
|
+
if pages is None:
|
|
206
|
+
self._source = lambda: [t for t in self.tabs if t.visible()]
|
|
207
|
+
else:
|
|
208
|
+
self._source = pages if callable(pages) else (lambda: pages)
|
|
209
|
+
self.pages: list[Page] = list(self._source())
|
|
210
|
+
self.index = 0
|
|
211
|
+
self.stack: list[Page] = []
|
|
212
|
+
self._alerts = Alerts(self.alerts)
|
|
213
|
+
self._tab_alerts: dict[int, Alerts] = {} # by id(tab), for tabs with alerts
|
|
214
|
+
self._shown: Alerts | None = None # whose banner is on screen
|
|
215
|
+
self._marks: dict[int, str] = {} # page index -> tone of a tab alert waiting there
|
|
216
|
+
self._clock = clock
|
|
217
|
+
self.wake_in: float | None = None # seconds until the last frame drawn changes
|
|
218
|
+
self._slide: list[int] | None = None # from, to, released dx, frames shown
|
|
219
|
+
self._fade: list | None = None # the image faded from, frames shown
|
|
220
|
+
self._last: bytes | None = None # the frame last shown
|
|
221
|
+
self._touched = clock() # the last gesture or drag, for the return home
|
|
222
|
+
|
|
223
|
+
def add_tab(self, tab: Page) -> App:
|
|
224
|
+
"""Add a tab at the end of the swipe order."""
|
|
225
|
+
self.tabs.append(tab)
|
|
226
|
+
self.sync()
|
|
227
|
+
return self
|
|
228
|
+
|
|
229
|
+
def add_alert(self, alert: Callable[[], Banner | None]) -> App:
|
|
230
|
+
"""Add an alert: a function returning a banner while it holds. Earlier alerts win."""
|
|
231
|
+
self.alerts.append(alert)
|
|
232
|
+
return self
|
|
233
|
+
|
|
234
|
+
def sync(self) -> None:
|
|
235
|
+
"""Take up a changed page list, staying on the same page when it is still there."""
|
|
236
|
+
new = list(self._source())
|
|
237
|
+
if new == self.pages:
|
|
238
|
+
return
|
|
239
|
+
cur = self.pages[self.index] if self.pages else None
|
|
240
|
+
self.pages = new
|
|
241
|
+
self._slide = None
|
|
242
|
+
self.index = new.index(cur) if cur in new else max(0, min(self.index, len(new) - 1))
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def current(self) -> Page:
|
|
246
|
+
return self.stack[-1] if self.stack else self.pages[self.index]
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
def banner_timeout(self) -> float | None:
|
|
250
|
+
"""Seconds a banner shows before hiding as if tapped; None waits for the tap. The
|
|
251
|
+
theme's alert_timeout unless App was given one."""
|
|
252
|
+
if self._banner_timeout is DEFAULT:
|
|
253
|
+
return self.theme.alert_timeout
|
|
254
|
+
return self._banner_timeout # type: ignore[return-value]
|
|
255
|
+
|
|
256
|
+
@banner_timeout.setter
|
|
257
|
+
def banner_timeout(self, value: float | None) -> None:
|
|
258
|
+
self._banner_timeout = value
|
|
259
|
+
|
|
260
|
+
def _tab_slot(self, page: Page) -> Alerts | None:
|
|
261
|
+
fns = getattr(page, "alerts", None)
|
|
262
|
+
if not fns:
|
|
263
|
+
return None
|
|
264
|
+
slot = self._tab_alerts.get(id(page))
|
|
265
|
+
if slot is None:
|
|
266
|
+
slot = self._tab_alerts[id(page)] = Alerts(fns)
|
|
267
|
+
return slot
|
|
268
|
+
|
|
269
|
+
def active_banner(self) -> Banner | None:
|
|
270
|
+
"""The banner on screen: an app-wide alert's, else the shown tab's own alert's (with
|
|
271
|
+
no popup open over it). App-wide alerts win."""
|
|
272
|
+
with r.using(self.theme):
|
|
273
|
+
now = self._clock()
|
|
274
|
+
b = self._alerts.holding(now, True, self.banner_timeout)
|
|
275
|
+
self._shown = self._alerts if b is not None else None
|
|
276
|
+
if b is None and not self.stack and self.pages:
|
|
277
|
+
slot = self._tab_slot(self.pages[self.index])
|
|
278
|
+
if slot is not None:
|
|
279
|
+
b = slot.holding(now, True, self.banner_timeout)
|
|
280
|
+
if b is not None:
|
|
281
|
+
self._shown, slot.switched = slot, b.key
|
|
282
|
+
return b
|
|
283
|
+
|
|
284
|
+
def _tab_banner_up(self) -> bool:
|
|
285
|
+
"""Whether the banner on screen is the shown tab's own, which a swipe may leave."""
|
|
286
|
+
return self.active_banner() is not None and self._shown is not self._alerts
|
|
287
|
+
|
|
288
|
+
def _watch_tabs(self, now: float) -> None:
|
|
289
|
+
"""Tab alerts on tabs not shown: mark their page dots, and jump to one that switches."""
|
|
290
|
+
self._marks = {}
|
|
291
|
+
for i, page in enumerate(self.pages):
|
|
292
|
+
slot = self._tab_slot(page)
|
|
293
|
+
if slot is None or (i == self.index and not self.stack):
|
|
294
|
+
continue
|
|
295
|
+
b = slot.holding(now, False, self.banner_timeout)
|
|
296
|
+
if b is None:
|
|
297
|
+
continue
|
|
298
|
+
self._marks[i] = b.tone
|
|
299
|
+
if b.switch and slot.switched != b.key and self._alerts.first() is None:
|
|
300
|
+
slot.switched = b.key
|
|
301
|
+
self.stack.clear()
|
|
302
|
+
self.index, self._slide, self._touched = i, None, now
|
|
303
|
+
self.fade()
|
|
304
|
+
|
|
305
|
+
def _view(self) -> tuple:
|
|
306
|
+
"""What is on screen, as far as transitions care: the tab, the popup and its tab."""
|
|
307
|
+
top = self.stack[-1] if self.stack else None
|
|
308
|
+
return self.index, id(top) if top else None, getattr(top, "index", None)
|
|
309
|
+
|
|
310
|
+
def fade(self) -> None:
|
|
311
|
+
"""Fade what is on screen out to black, then fade in whatever is shown next."""
|
|
312
|
+
if self._last is not None:
|
|
313
|
+
img = Image.frombytes("RGBA", (r.WIDTH, r.HEIGHT), self._last)
|
|
314
|
+
self._fade, self._slide = [img, 0], None
|
|
315
|
+
|
|
316
|
+
def home(self) -> int | None:
|
|
317
|
+
"""The index of the first shown tab with home_after set, if any."""
|
|
318
|
+
for i, page in enumerate(self.pages):
|
|
319
|
+
if getattr(page, "home_after", None) is not None:
|
|
320
|
+
return i
|
|
321
|
+
return None
|
|
322
|
+
|
|
323
|
+
def _go_home(self, now: float) -> None:
|
|
324
|
+
"""Fade back to the home tab once it has been left alone long enough elsewhere."""
|
|
325
|
+
i = self.home()
|
|
326
|
+
if i is None or self._fade is not None or self._slide is not None:
|
|
327
|
+
return
|
|
328
|
+
if (self.stack or self.index != i) and now - self._touched > self.pages[i].home_after:
|
|
329
|
+
self.fade()
|
|
330
|
+
self.stack.clear()
|
|
331
|
+
self.index = i
|
|
332
|
+
self._touched = now
|
|
333
|
+
|
|
334
|
+
def handle(self, g: Gesture) -> None:
|
|
335
|
+
with r.using(self.theme):
|
|
336
|
+
self._handle_themed(g)
|
|
337
|
+
|
|
338
|
+
def _handle_themed(self, g: Gesture) -> None:
|
|
339
|
+
self._touched = self._clock()
|
|
340
|
+
top = self.stack[-1] if self.stack else None
|
|
341
|
+
before = self._view()
|
|
342
|
+
self._handle(g)
|
|
343
|
+
if self._view() == before:
|
|
344
|
+
return
|
|
345
|
+
after = self.stack[-1] if self.stack else None
|
|
346
|
+
# a popup opening, closing or changing tab moves the way that popup says
|
|
347
|
+
popup = after if after is not None else top
|
|
348
|
+
if popup is not None and getattr(popup, "transition", "cut") == "fade":
|
|
349
|
+
self.fade()
|
|
350
|
+
|
|
351
|
+
def _handle(self, g: Gesture) -> None:
|
|
352
|
+
self.sync()
|
|
353
|
+
banner = self.active_banner()
|
|
354
|
+
tab_banner = banner is not None and self._shown is not self._alerts
|
|
355
|
+
if banner is not None and self._shown is not None:
|
|
356
|
+
if g.kind == "tap":
|
|
357
|
+
self._shown.tap(banner, self._clock())
|
|
358
|
+
if not (tab_banner and g.kind in ("swipe_left", "swipe_right")):
|
|
359
|
+
return
|
|
360
|
+
now = self._clock()
|
|
361
|
+
if g.kind == "tap":
|
|
362
|
+
opened = self.current.tap(g.x, now)
|
|
363
|
+
if opened is CLOSE:
|
|
364
|
+
if self.stack:
|
|
365
|
+
self.stack.pop()
|
|
366
|
+
elif isinstance(opened, Page) and not self.stack: # no popups in popups
|
|
367
|
+
opened.opened(now)
|
|
368
|
+
self.stack.append(opened)
|
|
369
|
+
return
|
|
370
|
+
if g.kind not in ("swipe_left", "swipe_right"):
|
|
371
|
+
return
|
|
372
|
+
if not tab_banner and self.current.swipe(g.kind, now):
|
|
373
|
+
return
|
|
374
|
+
if self.stack:
|
|
375
|
+
self.stack.pop()
|
|
376
|
+
return
|
|
377
|
+
step = 1 if g.kind == "swipe_left" else -1
|
|
378
|
+
before = self.index
|
|
379
|
+
self.index = (self.index + step) % len(self.pages)
|
|
380
|
+
if self.index != before:
|
|
381
|
+
self._slide = [before, self.index, g.dx, 0]
|
|
382
|
+
|
|
383
|
+
@property
|
|
384
|
+
def animating(self) -> bool:
|
|
385
|
+
return self._slide is not None or self._fade is not None
|
|
386
|
+
|
|
387
|
+
def can_drag(self) -> bool:
|
|
388
|
+
return (
|
|
389
|
+
not self.stack
|
|
390
|
+
and self._fade is None
|
|
391
|
+
and len(self.pages) > 1
|
|
392
|
+
and (self.active_banner() is None or self._tab_banner_up())
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
def _page_image(self, i: int, now: float) -> Image.Image:
|
|
396
|
+
img, d = r.canvas()
|
|
397
|
+
r.backdrop(img, now)
|
|
398
|
+
slot = self._tab_slot(self.pages[i])
|
|
399
|
+
b = slot.holding(now, i == self.index, self.banner_timeout) if slot else None
|
|
400
|
+
if b is not None and not (i == self.index and self._alerts.first() is not None):
|
|
401
|
+
b.draw(img, d, now) # a tab's alert slides with its tab
|
|
402
|
+
else:
|
|
403
|
+
self.pages[i].draw(img, d, now)
|
|
404
|
+
r.dots(d, len(self.pages), i, marks=self._marks)
|
|
405
|
+
return img
|
|
406
|
+
|
|
407
|
+
def _pair(self, a: int, b: int, offset: int, now: float) -> bytes:
|
|
408
|
+
"""Page a shifted by offset, with page b beside it on the side it comes in from."""
|
|
409
|
+
img, _ = r.canvas()
|
|
410
|
+
img.paste(self._page_image(a, now), (offset, 0))
|
|
411
|
+
if offset:
|
|
412
|
+
side = r.WIDTH if offset < 0 else -r.WIDTH
|
|
413
|
+
img.paste(self._page_image(b, now), (offset + side, 0))
|
|
414
|
+
return r.to_frame(img)
|
|
415
|
+
|
|
416
|
+
def frame(self, drag: int | None = None) -> bytes:
|
|
417
|
+
with r.using(self.theme):
|
|
418
|
+
r.take_wake()
|
|
419
|
+
self._last = self._frame(drag)
|
|
420
|
+
self.wake_in = r.take_wake()
|
|
421
|
+
return self._last
|
|
422
|
+
|
|
423
|
+
def _frame(self, drag: int | None) -> bytes:
|
|
424
|
+
self.sync()
|
|
425
|
+
fade_frames, slide_frames = self.theme.fade_frames, self.theme.slide_frames
|
|
426
|
+
now = self._clock()
|
|
427
|
+
if drag is not None:
|
|
428
|
+
self._touched = now
|
|
429
|
+
while self.stack and self.stack[-1].expired(now):
|
|
430
|
+
# a popup closing by itself always fades, so the change is never abrupt
|
|
431
|
+
self.stack.pop()
|
|
432
|
+
self.fade()
|
|
433
|
+
self._go_home(now)
|
|
434
|
+
self._watch_tabs(now)
|
|
435
|
+
if self._fade is not None:
|
|
436
|
+
# frame-stepped like the slide: out to black, then in
|
|
437
|
+
src, shown = self._fade
|
|
438
|
+
shown += 1
|
|
439
|
+
if shown >= 2 * fade_frames:
|
|
440
|
+
self._fade = None
|
|
441
|
+
else:
|
|
442
|
+
self._fade[1] = shown
|
|
443
|
+
black, _ = r.canvas()
|
|
444
|
+
if shown <= fade_frames:
|
|
445
|
+
return r.to_frame(Image.blend(black, src, 1 - shown / fade_frames))
|
|
446
|
+
to = self._render(now)
|
|
447
|
+
return r.to_frame(Image.blend(black, to, (shown - fade_frames) / fade_frames))
|
|
448
|
+
if self._slide is not None:
|
|
449
|
+
# Frame-stepped rather than timed: uploads take ~45 ms, and a timed slide
|
|
450
|
+
# skipped most of its frames.
|
|
451
|
+
src, dst, start, shown = self._slide
|
|
452
|
+
shown += 1
|
|
453
|
+
if shown >= slide_frames:
|
|
454
|
+
self._slide = None
|
|
455
|
+
else:
|
|
456
|
+
self._slide[3] = shown
|
|
457
|
+
end = -r.WIDTH if start < 0 else r.WIDTH
|
|
458
|
+
offset = int(start + (end - start) * ease(shown / slide_frames))
|
|
459
|
+
return self._pair(src, dst, offset, now)
|
|
460
|
+
if drag and self.can_drag():
|
|
461
|
+
drag = max(-r.WIDTH, min(r.WIDTH, drag))
|
|
462
|
+
step = 1 if drag < 0 else -1
|
|
463
|
+
return self._pair(self.index, (self.index + step) % len(self.pages), drag, now)
|
|
464
|
+
return r.to_frame(self._render(now))
|
|
465
|
+
|
|
466
|
+
def _render(self, now: float) -> Image.Image:
|
|
467
|
+
"""The screen at rest: the banner, or the popup or tab shown."""
|
|
468
|
+
img, d = r.canvas()
|
|
469
|
+
r.backdrop(img, now)
|
|
470
|
+
banner = self.active_banner()
|
|
471
|
+
if banner is not None:
|
|
472
|
+
banner.draw(img, d, now)
|
|
473
|
+
else:
|
|
474
|
+
self.current.draw(img, d, now)
|
|
475
|
+
if not self.stack:
|
|
476
|
+
r.dots(d, len(self.pages), self.index, marks=self._marks)
|
|
477
|
+
return img
|
mycue/canvas.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Drawing your own art: a function paints a Canvas, shown wherever a picture can be.
|
|
2
|
+
|
|
3
|
+
def sky(c, now):
|
|
4
|
+
c.clear("#000010")
|
|
5
|
+
for i in range(20):
|
|
6
|
+
c.pixel((i * 37 + int(now * 5)) % c.width, i * 7 % c.height, "white")
|
|
7
|
+
|
|
8
|
+
ui.canvas(sky, scale=2, every=0.1) # a card, at half resolution, redrawn 10 times a second
|
|
9
|
+
ui.slide(Sketch(sky)) # a whole page
|
|
10
|
+
mc.Theme(background=Sketch(sky)) # behind everything
|
|
11
|
+
|
|
12
|
+
The function is called with a fresh, clear Canvas the size of its box, and the frame's time.
|
|
13
|
+
Coordinates are the box's, from 0, 0 at its top left. scale paints at 1/scale the size and
|
|
14
|
+
scales up with crisp pixels: scale=4 on the whole screen is 160 x 12 big pixels. every asks
|
|
15
|
+
for a redraw that often, for animation; None redraws only when the screen does (about once a
|
|
16
|
+
second when idle). A function that raises draws nothing that frame, and logs the error
|
|
17
|
+
once until it next succeeds.
|
|
18
|
+
|
|
19
|
+
Colours are a tone name ("accent", "good", "dim"...), a hex string, a CSS name, or an (r, g,
|
|
20
|
+
b) or (r, g, b, a) tuple. For anything more, c.draw is the Pillow ImageDraw and c.img the
|
|
21
|
+
image.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import logging
|
|
27
|
+
from collections.abc import Callable
|
|
28
|
+
|
|
29
|
+
from PIL import Image, ImageColor, ImageDraw
|
|
30
|
+
|
|
31
|
+
from mycue import render as r
|
|
32
|
+
from mycue.media import Drawable, Picture, Source, fitted
|
|
33
|
+
|
|
34
|
+
log = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
Ink = str | tuple[int, ...]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def ink(c: Ink) -> tuple[int, int, int, int]:
|
|
40
|
+
"""A colour as RGBA: a tone name, a hex or CSS name, or a tuple."""
|
|
41
|
+
if isinstance(c, str):
|
|
42
|
+
tones = r.theme().tones
|
|
43
|
+
rgb = tones.get(c) or ImageColor.getrgb(c)
|
|
44
|
+
else:
|
|
45
|
+
rgb = tuple(int(v) for v in c)
|
|
46
|
+
return (*rgb, 255) if len(rgb) == 3 else tuple(rgb) # type: ignore[return-value]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Canvas:
|
|
50
|
+
"""A clear image to paint, the size of the box (divided by the sketch's scale)."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, width: int, height: int) -> None:
|
|
53
|
+
self.width, self.height = width, height
|
|
54
|
+
self.img = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
|
55
|
+
self.draw = ImageDraw.Draw(self.img)
|
|
56
|
+
|
|
57
|
+
def clear(self, colour: Ink | None = None) -> None:
|
|
58
|
+
"""Fill it all with colour, or make it clear again."""
|
|
59
|
+
self.draw.rectangle([0, 0, self.width, self.height], fill=ink(colour or (0, 0, 0, 0)))
|
|
60
|
+
|
|
61
|
+
def pixel(self, x: int, y: int, colour: Ink) -> None:
|
|
62
|
+
"""One pixel; off the canvas does nothing."""
|
|
63
|
+
if 0 <= x < self.width and 0 <= y < self.height:
|
|
64
|
+
self.img.putpixel((int(x), int(y)), ink(colour))
|
|
65
|
+
|
|
66
|
+
def get(self, x: int, y: int) -> tuple[int, int, int, int]:
|
|
67
|
+
"""The colour painted at x, y so far."""
|
|
68
|
+
return self.img.getpixel((int(x), int(y))) # type: ignore[return-value]
|
|
69
|
+
|
|
70
|
+
def line(self, x0: float, y0: float, x1: float, y1: float, colour: Ink, width: int = 1):
|
|
71
|
+
self.draw.line([(x0, y0), (x1, y1)], fill=ink(colour), width=width)
|
|
72
|
+
|
|
73
|
+
def rect(self, x0: float, y0: float, x1: float, y1: float, colour: Ink, fill: bool = True):
|
|
74
|
+
"""A rectangle with corners x0, y0 and x1, y1 included; fill=False for its outline."""
|
|
75
|
+
c = ink(colour)
|
|
76
|
+
self.draw.rectangle([x0, y0, x1, y1], fill=c if fill else None, outline=c)
|
|
77
|
+
|
|
78
|
+
def circle(self, x: float, y: float, radius: float, colour: Ink, fill: bool = True):
|
|
79
|
+
"""A circle centred on x, y; fill=False for its outline."""
|
|
80
|
+
c = ink(colour)
|
|
81
|
+
box = [x - radius, y - radius, x + radius, y + radius]
|
|
82
|
+
self.draw.ellipse(box, fill=c if fill else None, outline=c)
|
|
83
|
+
|
|
84
|
+
def text(self, x: float, y: float, text: str, colour: Ink = "text", size: int = 11):
|
|
85
|
+
"""text with its top left at x, y, in the theme's font."""
|
|
86
|
+
self.draw.text((x, y), text, font=r.font(size), fill=ink(colour))
|
|
87
|
+
|
|
88
|
+
def image(
|
|
89
|
+
self,
|
|
90
|
+
source: Source | Picture,
|
|
91
|
+
x: int = 0,
|
|
92
|
+
y: int = 0,
|
|
93
|
+
w: int | None = None,
|
|
94
|
+
h: int | None = None,
|
|
95
|
+
fit: str = "contain",
|
|
96
|
+
smooth: bool = False,
|
|
97
|
+
now: float = 0.0,
|
|
98
|
+
) -> None:
|
|
99
|
+
"""A picture (or a GIF's frame at now) fitted to w x h at x, y; its own size when
|
|
100
|
+
w and h are None."""
|
|
101
|
+
pic = source if isinstance(source, Picture) else Picture(source)
|
|
102
|
+
if w is None and h is None:
|
|
103
|
+
frame = pic.frames[pic.at(now)[0]]
|
|
104
|
+
else:
|
|
105
|
+
frame = fitted(pic.frames[pic.at(now)[0]], w or pic.size[0], h or pic.size[1], fit,
|
|
106
|
+
smooth) # fmt: skip
|
|
107
|
+
self.img.alpha_composite(frame, (int(x), int(y)))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Sketch(Drawable):
|
|
111
|
+
"""paint(canvas, now) as a picture. scale paints at 1/scale the size, scaled up crisply;
|
|
112
|
+
every asks for a redraw that often (None: only when the screen redraws anyway)."""
|
|
113
|
+
|
|
114
|
+
def __init__(
|
|
115
|
+
self,
|
|
116
|
+
paint: Callable[[Canvas, float], object],
|
|
117
|
+
scale: int = 1,
|
|
118
|
+
every: float | None = None,
|
|
119
|
+
) -> None:
|
|
120
|
+
if scale < 1:
|
|
121
|
+
raise ValueError("scale must be at least 1")
|
|
122
|
+
if every is not None and every <= 0:
|
|
123
|
+
raise ValueError("every must be positive, or None")
|
|
124
|
+
self.paint, self.scale, self.every = paint, int(scale), every
|
|
125
|
+
self._failed = False
|
|
126
|
+
|
|
127
|
+
def at(self, now: float) -> tuple[int, float | None]:
|
|
128
|
+
return 0, self.every
|
|
129
|
+
|
|
130
|
+
def image(self, w: int, h: int, now: float, fit: str = "cover", smooth: bool = True):
|
|
131
|
+
c = Canvas(max(1, -(-w // self.scale)), max(1, -(-h // self.scale)))
|
|
132
|
+
try:
|
|
133
|
+
self.paint(c, now)
|
|
134
|
+
self._failed = False
|
|
135
|
+
except Exception:
|
|
136
|
+
if not self._failed:
|
|
137
|
+
log.exception("sketch %r failed", getattr(self.paint, "__name__", self.paint))
|
|
138
|
+
self._failed = True
|
|
139
|
+
return Image.new("RGBA", (w, h), (0, 0, 0, 0))
|
|
140
|
+
img = c.img
|
|
141
|
+
if self.scale > 1:
|
|
142
|
+
img = img.resize(
|
|
143
|
+
(c.width * self.scale, c.height * self.scale), Image.Resampling.NEAREST
|
|
144
|
+
)
|
|
145
|
+
return img.crop((0, 0, w, h))
|