Files
lintunes/lintunes/theme.py
T

490 lines
20 KiB
Python

"""App-wide theming: highlight color and UI/text scale, driven by Preferences.
Everything that shows the highlight color (table/sidebar selection, the seek
slider's filled groove, the visualizer bars) reads QPalette.Highlight, so
recoloring is a palette swap. UI scale adjusts the app font, scrollbar
dimensions, and (via MainWindow.apply_ui_metrics) table row heights.
"""
from PyQt6.QtGui import QPalette, QColor, QFontDatabase, QIcon, QPixmap
# The font the now-playing panel wants by default. Not bundled (it's a
# proprietary Monotype face), so it's simply the head of the fallback chain
# below: whatever geometric sans this machine actually has wins, silently.
# Startup never asks — the user can override in Edit ▸ Preferences.
CENTURY_GOTHIC = "Century Gothic"
# Closest-first substitutes for Century Gothic. URW Gothic is the ITC Avant
# Garde clone CG derives from and ships with ghostscript, so it's present on
# most Linux boxes; TeX Gyre Adventor is its TeX-world descendant. Names must
# be spelled exactly as the family is registered — QFontDatabase does no fuzzy
# matching, which is why "Jost*" appears with its asterisk.
NOW_PLAYING_FALLBACKS = (
CENTURY_GOTHIC,
"URW Gothic", "URW Gothic L", "URW Gothic Book",
"TeX Gyre Adventor",
"Questrial", "Didact Gothic", "Jost*", "Jost", "Josefin Sans",
"Poppins", "Futura", "Avant Garde", "ITC Avant Garde Gothic",
"Montserrat", "Nunito Sans", "Nunito",
)
HIGHLIGHT_COLORS = {
"blue": "#3584E4",
"marigold": "#F5A623",
"magenta": "#D6409F",
"seafoam": "#7FD1AE",
"gray": "#8A8A8A",
}
UI_SCALES = {
"small": {"font_pt": 9, "scrollbar": 10, "row_height": 20, "header_strip": 28,
"status_height": 22},
"medium": {"font_pt": 11, "scrollbar": 14, "row_height": 24, "header_strip": 34,
"status_height": 26},
"large": {"font_pt": 13, "scrollbar": 18, "row_height": 28, "header_strip": 40,
"status_height": 32},
}
_base_palette = None
# The user-tunable grayscale surfaces. now-playing bg/text have fixed
# defaults (white/black); the rest default to whatever the base palette uses,
# so an untouched install looks exactly as it did before the feature existed.
_PALETTE_GRAY_ROLE = {
"color_background": QPalette.ColorRole.Window,
"color_stripe": QPalette.ColorRole.AlternateBase,
"color_button": QPalette.ColorRole.AlternateBase,
"color_base": QPalette.ColorRole.Base,
}
_FIXED_GRAY_DEFAULT = {
"color_now_playing_bg": 255,
"color_now_playing_text": 0,
}
# Dark mode (Round 101) has its own set of every slider, saved under the
# same key with ``dark_`` in front, and its own tint; the highlight color is
# shared. The light set keeps the original key names, so an existing
# preferences.json reads exactly as before.
MODES = ("light", "dark")
DARK_DEFAULTS = {
"color_background": 48,
"color_button": 72,
"color_now_playing_bg": 30,
"color_now_playing_text": 225,
"color_stripe": 40,
"color_base": 32,
"color_visualizer_gray": 95,
}
# Text and glyphs, which follow the mode rather than a slider.
_TEXT = {"light": QColor("black"), "dark": QColor("#e6e6e6")}
_ICON = {"light": QColor("#4A4A4A"), "dark": QColor("#c8c8c8")}
_mode = "light" # the mode last applied (apply_theme)
def mode(prefs) -> str:
""""dark" or "light", as Preferences has it."""
return "dark" if prefs is not None and prefs.get("dark_mode") else "light"
def current_mode() -> str:
"""The mode last applied, for painters with no prefs to hand."""
return _mode
def is_dark() -> bool:
return _mode == "dark"
def mode_key(key: str, which: str) -> str:
"""The preferences key for ``key`` in mode ``which``."""
return key if which == "light" else f"dark_{key}"
def text_color(which: str | None = None) -> QColor:
"""Text on a button or panel in this mode (black in light)."""
return QColor(_TEXT[which or _mode])
def icon_gray(which: str | None = None) -> QColor:
"""The transport glyphs' resting color in this mode."""
return QColor(_ICON[which or _mode])
def tint(prefs, which: str | None = None) -> tuple[QColor, float]:
"""(tint color, level 0..1) for mode ``which`` (the current one when
None). Level 0 is no tint at all."""
which = which or mode(prefs)
color = QColor(str(prefs.get(mode_key("tint_color", which)) or "#3584E4"))
if not color.isValid():
color = QColor("#3584E4")
try:
level = float(prefs.get(mode_key("tint_level", which)) or 0) / 100
except (TypeError, ValueError):
level = 0.0
return color, max(0.0, min(1.0, level))
def tinted(value, color: QColor, level: float) -> QColor:
"""A 0..255 gray with ``color``'s hue at ``level`` of its saturation.
Lightness stays the gray's, so the slider still means light ↔ dark."""
plain = gray(value)
if level <= 0:
return plain
h, s, _l, _a = color.getHslF()
if h < 0: # an achromatic tint has no hue
return plain
return QColor.fromHslF(h, s * level, plain.lightnessF())
def _base() -> QPalette:
"""The app's original palette, captured once, used as the reference for
highlight/grayscale overrides and for the sliders' 'current' defaults."""
global _base_palette
if _base_palette is None:
from PyQt6.QtWidgets import QApplication
app = QApplication.instance()
_base_palette = QPalette(app.palette()) if app is not None else QPalette()
return _base_palette
def gray(value) -> QColor:
"""A 0..255 lightness as an opaque gray QColor."""
v = max(0, min(255, int(value)))
return QColor(v, v, v)
def default_gray(key: str, which: str = "light") -> int:
"""The slider's value when this surface hasn't been overridden — i.e. its
current appearance, so showing the slider doesn't imply a change."""
if which == "dark":
return DARK_DEFAULTS[key]
if key in _FIXED_GRAY_DEFAULT:
return _FIXED_GRAY_DEFAULT[key]
if key == "color_visualizer_gray":
# Dim-mode bars derive from alternateBase dropped ~12% in lightness
# (see VisualizerWidget._bar_color); mirror that so the slider shows
# the current appearance.
color = _base().color(QPalette.ColorGroup.Active,
QPalette.ColorRole.AlternateBase)
h, s, l, a = color.getHslF()
color.setHslF(h, s, l * 0.88, a)
else:
color = _base().color(QPalette.ColorGroup.Active,
_PALETTE_GRAY_ROLE[key])
return round(0.299 * color.red() + 0.587 * color.green()
+ 0.114 * color.blue())
def gray_value(prefs, key: str, which: str | None = None) -> int:
"""The 0..255 value to show/apply for a surface in mode ``which`` (the
current one when None): the saved override, or the default when the
user hasn't touched it."""
which = which or mode(prefs)
saved = prefs.get(mode_key(key, which))
return (default_gray(key, which) if saved is None
else max(0, min(255, int(saved))))
def surface(prefs, key: str) -> QColor:
"""A surface's color as painted: its gray in the current mode, tinted.
(Text — the now-playing text — is never tinted; use gray_value.)"""
return tinted(gray_value(prefs, key), *tint(prefs))
def swatch_icon(color_hex: str, size: int = 18) -> QIcon:
"""A flat square of `color_hex` — the icon on a color-choosing control."""
pixmap = QPixmap(size, size)
pixmap.fill(QColor(color_hex))
return QIcon(pixmap)
def highlight_palette(prefs) -> dict:
"""The highlight swatches as this user has them: the stock colors, with
any slot they repainted (Preferences → a swatch → color picker) in its
place. Saved in ``highlight_palette`` as {slot name: "#rrggbb"}."""
palette = dict(HIGHLIGHT_COLORS)
saved = prefs.get("highlight_palette") or {}
if isinstance(saved, dict):
for name, value in saved.items():
if name in palette and QColor.isValidColorName(str(value)):
palette[name] = QColor(str(value)).name()
return palette
def highlight_color(prefs) -> QColor:
palette = highlight_palette(prefs)
return QColor(palette.get(prefs.get("highlight"), palette["blue"]))
def scale_metrics(prefs) -> dict:
return UI_SCALES.get(prefs.get("ui_scale"), UI_SCALES["medium"])
def _dark_palette(prefs) -> QPalette:
"""Every role, since nothing of the light base palette suits dark."""
window = surface(prefs, "color_background")
button = surface(prefs, "color_button")
palette = QPalette(button, window)
text = text_color("dark")
dim = QColor(text)
dim.setAlpha(110)
for group in (QPalette.ColorGroup.Active, QPalette.ColorGroup.Inactive,
QPalette.ColorGroup.Disabled):
disabled = group == QPalette.ColorGroup.Disabled
for role in (QPalette.ColorRole.WindowText, QPalette.ColorRole.Text,
QPalette.ColorRole.ButtonText):
palette.setColor(group, role, QColor("#808080") if disabled else text)
palette.setColor(group, QPalette.ColorRole.Base, surface(prefs, "color_base"))
palette.setColor(group, QPalette.ColorRole.AlternateBase,
surface(prefs, "color_stripe"))
palette.setColor(group, QPalette.ColorRole.PlaceholderText, dim)
palette.setColor(group, QPalette.ColorRole.ToolTipBase, window.lighter(130))
palette.setColor(group, QPalette.ColorRole.ToolTipText, text)
palette.setColor(group, QPalette.ColorRole.Link, QColor("#8ab4f8"))
return palette
def apply_theme(app, prefs):
global _mode
base = _base()
_mode = mode(prefs)
color = highlight_color(prefs)
# Black text on light highlights, white on dark
luminance = (0.299 * color.red() + 0.587 * color.green()
+ 0.114 * color.blue()) / 255
text = QColor("black") if luminance > 0.6 else QColor("white")
palette = _dark_palette(prefs) if _mode == "dark" else QPalette(base)
groups = (QPalette.ColorGroup.Active, QPalette.ColorGroup.Inactive)
for group in groups:
palette.setColor(group, QPalette.ColorRole.Highlight, color)
palette.setColor(group, QPalette.ColorRole.HighlightedText, text)
palette.setColor(group, QPalette.ColorRole.Accent, color)
if _mode == "light":
# User grayscale overrides; a surface nobody touched keeps the base
# palette's own color unless a tint asks to color it. The
# button-box color lives in transport.py, not the palette.
_hue, level = tint(prefs, "light")
for key, role in (("color_background", QPalette.ColorRole.Window),
("color_stripe", QPalette.ColorRole.AlternateBase),
("color_base", QPalette.ColorRole.Base)):
if prefs.get(key) is None and level <= 0:
continue
for group in groups:
palette.setColor(group, role, surface(prefs, key))
app.setPalette(palette)
metrics = scale_metrics(prefs)
font = app.font()
font.setPointSize(metrics["font_pt"])
app.setFont(font)
# The scrollbars are painted by the app style (track_table's
# ClickToJumpScrollStyle), not the style sheet: a QSS rule on any of
# their parts would make Qt draw them itself, arrows and all.
from lintunes.gui.track_table import ClickToJumpScrollStyle
ClickToJumpScrollStyle.extent = metrics["scrollbar"]
button = palette.color(QPalette.ColorRole.Button)
app.setStyleSheet(button_emboss_qss(button))
def emboss_stops(color: QColor, pressed: bool = False) -> list:
"""[(position, QColor)] of the emboss: a crisp lit rim along the top and
a crisp shaded rim along the bottom, with the face itself nearly flat.
``pressed`` flips it — a button sinking in, or a debossed panel. The
color itself sits at the midpoint, so what the user picked is still
exactly what the middle shows."""
rim_top, face_top = color.lighter(EMBOSS_RIM), color.lighter(EMBOSS_LIGHTER)
face_bottom, rim_bottom = color.darker(EMBOSS_DARKER), color.darker(EMBOSS_RIM)
if pressed:
rim_top, face_top, face_bottom, rim_bottom = (
rim_bottom, face_bottom, face_top, rim_top)
return [(0, rim_top), (EMBOSS_RIM_WIDTH, face_top), (0.5, color),
(round(1 - EMBOSS_RIM_WIDTH, 2), face_bottom), (1, rim_bottom)]
def emboss_gradient(color: QColor, pressed: bool = False) -> str:
"""``emboss_stops`` as a style-sheet gradient. Computed from the live
color, so it follows the palette and the user's grayscale overrides."""
stops = ", ".join(f"stop:{pos} {c.name()}"
for pos, c in emboss_stops(color, pressed))
return f"qlineargradient(x1:0, y1:0, x2:0, y2:1, {stops})"
# How far the emboss strays from the base color (Qt's lighter/darker factor,
# 100 = unchanged). The face barely moves; the rims are where the shape is.
EMBOSS_LIGHTER = 103
EMBOSS_DARKER = 103
EMBOSS_RIM = 116
EMBOSS_RIM_WIDTH = 0.08 # fraction of the height each rim takes
BUTTON_RADIUS = 4 # every button's corners, and the scrollbar thumb's
HOVER_DARKER = 107 # hover: the whole button, slightly darker
# Held down: clearly darker again (Round 92) — between Material 3's state
# layers (hover 8 %, pressed 10 %) and GNOME's buttons (15 % → 30 %).
PRESSED_DARKER = 125
CHECKED_DARKER = 112 # a toggle that is on (checked), not held
def emboss_edges(color: QColor) -> str:
"""A defined outline: a touch darker at the bottom, like a lit shape."""
edge = color.darker(135).name()
bottom = color.darker(155).name()
return f"border: 1px solid {edge}; border-bottom-color: {bottom};"
def deboss_edges(color: QColor) -> str:
"""The emboss outline turned over: darker along the top, as a panel set
into the surface catches shadow there."""
edge = color.darker(135).name()
top = color.darker(155).name()
return f"border: 1px solid {edge}; border-top-color: {top};"
def deboss_qss(selector: str, color: QColor, *, radius: int = 8,
extra: str = "") -> str:
"""Pushed in, by exactly as much as a button is pushed out: the same
rims and outline, flipped. For panels that show things rather than do
things (the now-playing panel, the visualizer)."""
return (f"{selector} {{ {deboss_edges(color)} border-radius: {radius}px;"
f" background: {emboss_gradient(color, pressed=True)}; {extra} }}")
def paint_deboss(painter, rect, color: QColor, radius: float,
like: float | None = None):
"""``deboss_qss`` for a widget that paints itself: fills ``rect`` (a
QRectF) as a debossed rounded panel and returns its path, for clipping.
``like`` (pixels) shades a panel taller than that exactly as a panel
``like`` tall is shaded along its top and bottom, flat in between, so a
tall panel is recessed as much as a short one rather than gray at the
top."""
from PyQt6.QtGui import QPen
path = _fill_embossed(painter, rect, color, radius, like, pressed=True)
painter.setPen(QPen(color.darker(135), 1))
painter.drawPath(path)
# the darker top edge
painter.save()
painter.setClipRect(rect.adjusted(-1, -1, 1, -rect.height() / 2))
painter.setPen(QPen(color.darker(155), 1))
painter.drawPath(path)
painter.restore()
return path
def _fill_embossed(painter, rect, color: QColor, radius: float,
like: float | None, pressed: bool):
"""Fill a rounded ``rect`` with ``emboss_stops``, shaded as a shape
``like`` pixels tall would be at its top and bottom (None: as itself).
Returns the path."""
from PyQt6.QtCore import QPointF
from PyQt6.QtGui import QLinearGradient, QPainterPath
path = QPainterPath()
path.addRoundedRect(rect, radius, radius)
height = max(rect.height(), 1.0)
scale = min(like, height) / height if like else 1.0
stops = emboss_stops(color, pressed)
middle = stops[2][1]
top = [(pos * scale, c) for pos, c in stops[:3]]
bottom = [(1 - (1 - pos) * scale, c) for pos, c in stops[2:]]
gradient = QLinearGradient(QPointF(0, rect.top()), QPointF(0, rect.bottom()))
for pos, stop in top + [(top[-1][0], middle)] + bottom:
gradient.setColorAt(pos, stop)
painter.fillPath(path, gradient)
return path
def button_height_px(metrics) -> int:
"""How tall a button is for a font with these QFontMetrics: the text
line plus padding, outline and Fusion's margin (14 px). For things shaped
otherwise (a scrollbar thumb) that still want a button's emboss."""
return metrics.height() + 14
def paint_emboss(painter, rect, color: QColor, radius: float, like: float):
"""A button's face for a widget that paints itself: shaded at its top
and bottom exactly as a button ``like`` pixels tall, flat in between, so
a long scrollbar thumb is embossed exactly as much as a button."""
from PyQt6.QtGui import QPen
path = _fill_embossed(painter, rect, color, radius, like, pressed=False)
painter.setPen(QPen(color.darker(135), 1))
painter.drawPath(path)
# the darker bottom edge (emboss_edges)
painter.save()
painter.setClipRect(rect.adjusted(-1, rect.height() / 2, 1, 1))
painter.setPen(QPen(color.darker(155), 1))
painter.drawPath(path)
painter.restore()
return path
def button_face_qss(selector: str, color: QColor, *, radius: int = BUTTON_RADIUS,
extra: str = "", checked_color: QColor | None = None,
checked_text: str = "") -> str:
"""The one look every button shares: the emboss, the outline, a hover that
darkens the whole button a little, and a pressed state that sinks in.
``extra`` carries per-button layout (padding, margins, corners);
``checked_color`` is for toggles like the Library button."""
hover = color.darker(HOVER_DARKER)
down = color.darker(PRESSED_DARKER)
qss = (f"{selector} {{ {emboss_edges(color)} border-radius: {radius}px;"
f" background: {emboss_gradient(color)}; {extra} }}"
f"{selector}:hover {{ background: {emboss_gradient(hover)}; }}"
f"{selector}:pressed {{ background: {emboss_gradient(down, pressed=True)}; }}")
if checked_color is not None:
lit = checked_color
text = f" color: {checked_text};" if checked_text else ""
qss += (f"{selector}:checked {{ background: {emboss_gradient(lit, pressed=True)};"
f" {emboss_edges(lit)}{text} }}"
f"{selector}:checked:hover {{ background:"
f" {emboss_gradient(lit.darker(HOVER_DARKER), pressed=True)}; }}")
return qss
def button_emboss_qss(button: QColor) -> str:
"""Global rule for every non-flat QPushButton. Flat buttons (the version
button, the cancel-transfer ✕) opt out by being flat; the transport's bpm
button is flat inside a box that carries the look instead."""
return button_face_qss(
"QPushButton:!flat", button,
extra="padding: 3px 12px; min-height: 1.3em;",
checked_color=button.darker(CHECKED_DARKER)) + \
"QPushButton:!flat:default { border-color: palette(highlight); }" \
"QPushButton:!flat:disabled { color: palette(mid); }"
def installed_families() -> set[str]:
"""Families Qt can actually resolve on this machine."""
return set(QFontDatabase.families())
def auto_now_playing_family() -> str | None:
"""Best available face from NOW_PLAYING_FALLBACKS, or None for the app font.
Deliberately uncached: a cache would go stale for tests that stub
QFontDatabase, and for a font installed while the app is running.
"""
families = installed_families()
return next((f for f in NOW_PLAYING_FALLBACKS if f in families), None)
def now_playing_font_family(prefs) -> str | None:
"""Family the now-playing panel should use, or None for the app default.
prefs["now_playing_font"] semantics:
- missing/None : automatic → best available face from the fallback chain
- "" : user chose "same as the app" → always the app default
- "Family" : user picked a family → use it, or fall back to automatic
if it has since been uninstalled
Never prompts and never blocks; an unavailable wish quietly becomes the
nearest thing installed.
"""
saved = prefs.get("now_playing_font")
if saved == "":
return None
if saved: # a previously chosen family
return saved if saved in installed_families() else auto_now_playing_family()
return auto_now_playing_family()