Files
lintunes/andtunes/tools/make_buttons.py
T
trav 7ad51ab4dc v0.62.0: andTunes 0.4.0 — speakers (AirPlay, Chromecast), a list for a menu, icon pills, full-screen art
Now Playing: tap the art for it full screen, the title or artist line
for its album; shuffle, repeat and a new speakers button are icon pills.
The main menu is a list (Now Playing on top, Settings at the bottom, six
rows to a screen). The speakers button lists this Rabbit plus every
AirPlay speaker and Chromecast on the Wi-Fi: AirPlayRemote is the
desktop's RAOP sender in Java (verified from the R1 against
shairport-sync), CastRemote speaks the Cast protocol directly to the
Default Media Receiver and serves the file over HTTP (not yet tried on a
real Chromecast).
2026-10-17 01:00:54 -07:00

212 lines
7.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Render andTunes' default button artwork.
These are the stand-ins trav can replace: LinTunes copies each one into
``Music/andTunes/Buttons/`` on the Rabbit only when that file is missing, and
the app draws ``Buttons/<name>.png`` in preference to its bundled copy. So the
names here are a contract with both halves — change one and the user's own art
for it silently stops being used.
python3 andtunes/tools/make_buttons.py
Writes the same PNGs to the app's ``res/drawable-nodpi/`` (bundled fallback)
and to ``lintunes/andtunes/buttons/`` (what sync ships). Black and white only:
the R1's panel is dim and contrast is the only thing that reads on it.
"""
import os
import sys
from pathlib import Path
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PyQt6.QtCore import QPointF, QRectF, Qt # noqa: E402
from PyQt6.QtGui import ( # noqa: E402
QColor, QFont, QGuiApplication, QImage, QPainter, QPainterPath, QPolygonF,
)
HERE = Path(__file__).resolve().parent
RES = HERE.parent / "app" / "src" / "main" / "res" / "drawable-nodpi"
SHIP = HERE.parent.parent / "lintunes" / "andtunes" / "buttons"
# The menu: a 2 × 3 grid on a 480-dp-wide screen, so ~240 × 208 per tile.
TILE_W, TILE_H = 240, 208
MENU = {
"playlists": "Playlists",
"artists": "Artists",
"albums": "Albums",
"songs": "Songs",
"search": "Search",
"now_playing": "Now Playing",
"shuffle_all": "Shuffle All",
}
ICON = 128
TRANSPORT = ("prev", "play", "pause", "next")
# Now Playing's bottom row: icons inside the toggle pills (drawn black; the
# app tints them white when the pill is on).
TOGGLES = ("shuffle", "repeat", "repeat_one", "speakers")
def _canvas(w, h):
image = QImage(w, h, QImage.Format.Format_ARGB32)
image.fill(Qt.GlobalColor.transparent)
painter = QPainter(image)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
return image, painter
def tile(label: str) -> QImage:
image, p = _canvas(TILE_W, TILE_H)
path = QPainterPath()
path.addRoundedRect(QRectF(4, 4, TILE_W - 8, TILE_H - 8), 20, 20)
p.fillPath(path, QColor("black"))
font = QFont("DejaVu Sans")
font.setPixelSize(40)
font.setBold(True)
p.setFont(font)
p.setPen(QColor("white"))
p.drawText(QRectF(12, 12, TILE_W - 24, TILE_H - 24),
int(Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextWordWrap),
label)
p.end()
return image
def _tri(x0, y0, x1, y1, pointing_right=True) -> QPolygonF:
if pointing_right:
pts = [(x0, y0), (x1, (y0 + y1) / 2), (x0, y1)]
else:
pts = [(x1, y0), (x0, (y0 + y1) / 2), (x1, y1)]
return QPolygonF([QPointF(x, y) for x, y in pts])
def transport(name: str, color="black", size=ICON) -> QImage:
image, p = _canvas(size, size)
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(color))
s = size / 128
if name == "play":
p.drawPolygon(_tri(34 * s, 20 * s, 110 * s, 108 * s))
elif name == "pause":
p.drawRect(QRectF(28 * s, 20 * s, 26 * s, 88 * s))
p.drawRect(QRectF(74 * s, 20 * s, 26 * s, 88 * s))
elif name == "next":
p.drawPolygon(_tri(18 * s, 28 * s, 86 * s, 100 * s))
p.drawRect(QRectF(88 * s, 28 * s, 16 * s, 72 * s))
elif name == "prev":
p.drawPolygon(_tri(42 * s, 28 * s, 110 * s, 100 * s, False))
p.drawRect(QRectF(24 * s, 28 * s, 16 * s, 72 * s))
p.end()
return image
def toggle_icon(name: str, color="black", size=ICON) -> QImage:
from PyQt6.QtGui import QPen
image, p = _canvas(size, size)
s = size / 128
pen = QPen(QColor(color), 10 * s)
pen.setCapStyle(Qt.PenCapStyle.FlatCap)
pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
p.setPen(pen)
p.setBrush(Qt.BrushStyle.NoBrush)
def head(x, y, right=True):
# a solid arrowhead whose point is at (x, y)
d = 1 if right else -1
p.save()
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(color))
p.drawPolygon(QPolygonF([QPointF(x, y), QPointF(x - d * 22 * s, y - 16 * s),
QPointF(x - d * 22 * s, y + 16 * s)]))
p.restore()
if name == "shuffle":
# two strands that cross, each ending in an arrow on the right
a = QPainterPath(QPointF(14 * s, 38 * s))
a.cubicTo(QPointF(60 * s, 38 * s), QPointF(60 * s, 90 * s),
QPointF(96 * s, 90 * s))
b = QPainterPath(QPointF(14 * s, 90 * s))
b.cubicTo(QPointF(60 * s, 90 * s), QPointF(60 * s, 38 * s),
QPointF(96 * s, 38 * s))
p.drawPath(a)
p.drawPath(b)
head(116 * s, 38 * s)
head(116 * s, 90 * s)
elif name in ("repeat", "repeat_one"):
# a loop: top run left to right, bottom run back, rounded ends
loop = QPainterPath(QPointF(30 * s, 64 * s))
loop.lineTo(30 * s, 52 * s)
loop.arcTo(QRectF(30 * s, 32 * s, 40 * s, 40 * s), 180, -90)
loop.lineTo(92 * s, 32 * s)
p.drawPath(loop)
head(114 * s, 32 * s)
back = QPainterPath(QPointF(98 * s, 64 * s))
back.lineTo(98 * s, 76 * s)
back.arcTo(QRectF(58 * s, 56 * s, 40 * s, 40 * s), 0, -90)
back.lineTo(36 * s, 96 * s)
p.drawPath(back)
head(14 * s, 96 * s, right=False)
if name == "repeat_one":
font = QFont("DejaVu Sans")
font.setPixelSize(int(34 * s))
font.setBold(True)
p.setFont(font)
p.setPen(QColor(color))
p.drawText(QRectF(44 * s, 40 * s, 40 * s, 48 * s),
int(Qt.AlignmentFlag.AlignCenter), "1")
elif name == "speakers":
# broadcast: a dot with two arcs either side, (( • ))
p.save()
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(color))
p.drawEllipse(QPointF(64 * s, 64 * s), 11 * s, 11 * s)
p.restore()
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
p.setPen(pen)
for r in (30, 52):
box = QRectF((64 - r) * s, (64 - r) * s, 2 * r * s, 2 * r * s)
p.drawArc(box, -45 * 16, 90 * 16)
p.drawArc(box, 135 * 16, 90 * 16)
p.end()
return image
def launcher() -> QImage:
size = 192
image, p = _canvas(size, size)
path = QPainterPath()
path.addRoundedRect(QRectF(8, 8, size - 16, size - 16), 36, 36)
p.fillPath(path, QColor("black"))
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor("white"))
p.drawPolygon(_tri(70, 50, 142, 96))
p.drawPolygon(QPolygonF([QPointF(70, 96), QPointF(142, 96),
QPointF(70, 142)]))
p.end()
return image
def save(image: QImage, name: str, *dirs: Path):
for d in dirs:
d.mkdir(parents=True, exist_ok=True)
if not image.save(str(d / f"{name}.png"), "PNG"):
sys.exit(f"could not write {d / name}.png")
def main():
app = QGuiApplication(sys.argv) # noqa: F841 — must outlive the painting
for name, label in MENU.items():
save(tile(label), name, RES, SHIP)
for name in TRANSPORT:
save(transport(name), name, RES, SHIP)
for name in TOGGLES:
save(toggle_icon(name), name, RES, SHIP)
save(launcher(), "ic_launcher", RES)
# Status-bar icons must be white-on-transparent; Android tints by alpha.
save(transport("play", "white", 96), "ic_notify", RES)
print(f"wrote {len(MENU) + len(TRANSPORT) + len(TOGGLES)} buttons to {RES} and {SHIP}")
if __name__ == "__main__":
main()