92 lines
3.8 KiB
Python
92 lines
3.8 KiB
Python
"""The little version readout in the status bar's bottom-left corner.
|
|
|
|
Normally an inert label ("v0.1.0"; tooltip shows the git commit so identical
|
|
versions on different machines are still distinguishable). When the Updater
|
|
finds upstream commits it grows a "*" and becomes a button — still flat, but
|
|
the button face shows under the cursor and sinks while held: click → confirm →
|
|
git pull → the main window restarts the app on `update_applied`.
|
|
"""
|
|
|
|
from PyQt6.QtCore import Qt, QTimer, pyqtSignal
|
|
from PyQt6.QtGui import QPalette
|
|
from PyQt6.QtWidgets import QApplication, QMessageBox, QPushButton
|
|
|
|
from lintunes import __version__, theme
|
|
|
|
|
|
class VersionButton(QPushButton):
|
|
status_message = pyqtSignal(str)
|
|
|
|
def __init__(self, updater, parent=None):
|
|
super().__init__(parent)
|
|
self._updater = updater
|
|
self._update_ready = False
|
|
self.setFlat(True)
|
|
self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
|
|
self.refresh_theme()
|
|
self.setText(f"v{__version__}")
|
|
self._base_tooltip = f"LinTunes v{__version__}"
|
|
self.setToolTip(self._base_tooltip)
|
|
# short_hash() shells out to git; do it once the window is up rather
|
|
# than on the startup path (it only decorates the tooltip).
|
|
QTimer.singleShot(0, self._load_commit_tooltip)
|
|
|
|
updater.update_available.connect(self._on_update_available)
|
|
updater.update_failed.connect(self._on_update_failed)
|
|
self.clicked.connect(self._on_clicked)
|
|
|
|
def _load_commit_tooltip(self):
|
|
commit = self._updater.short_hash()
|
|
if not commit:
|
|
return
|
|
self._base_tooltip = f"LinTunes v{__version__} ({commit})"
|
|
if not self._update_ready:
|
|
self.setToolTip(self._base_tooltip)
|
|
|
|
def refresh_theme(self):
|
|
# Subtle but readable: the theme's text color at ~55% opacity
|
|
# (palette(mid) is too close to the light backgrounds to read).
|
|
# From the theme, never self.palette(): this stylesheet pins that,
|
|
# so the color it gave at launch survived every light/dark switch.
|
|
text = theme.text_color()
|
|
qss = ("QPushButton { border: 1px solid transparent; padding: 0 6px; "
|
|
f"border-radius: {theme.BUTTON_RADIUS}px; "
|
|
f"color: rgba({text.red()}, {text.green()}, {text.blue()}, 140); }}")
|
|
if self._update_ready:
|
|
face = QApplication.palette().color(QPalette.ColorRole.Button)
|
|
hover = face.darker(theme.HOVER_DARKER)
|
|
down = face.darker(theme.PRESSED_DARKER)
|
|
qss += (f"QPushButton:hover {{ {theme.emboss_edges(hover)}"
|
|
f" background: {theme.emboss_gradient(hover)}; }}"
|
|
f"QPushButton:pressed {{ {theme.emboss_edges(down)}"
|
|
f" background: {theme.emboss_gradient(down, pressed=True)}; }}")
|
|
self.setStyleSheet(qss)
|
|
|
|
def _on_update_available(self, commits: int):
|
|
self._update_ready = True
|
|
self.refresh_theme()
|
|
self.setText(f"v{__version__}*")
|
|
noun = "commit" if commits == 1 else "commits"
|
|
self.setToolTip(f"Update available ({commits} {noun}) — "
|
|
"click to update and restart")
|
|
|
|
def _on_clicked(self):
|
|
if not self._update_ready or not self.isEnabled():
|
|
return
|
|
if not self._confirm():
|
|
return
|
|
self.setEnabled(False)
|
|
self.setText("updating…")
|
|
self._updater.update_async()
|
|
|
|
def _confirm(self) -> bool:
|
|
answer = QMessageBox.question(
|
|
self.window(), "Update LinTunes",
|
|
"Pull the latest version and restart LinTunes?")
|
|
return answer == QMessageBox.StandardButton.Yes
|
|
|
|
def _on_update_failed(self, message: str):
|
|
self.setEnabled(True)
|
|
self.setText(f"v{__version__}*")
|
|
self.status_message.emit(f"Update failed: {message}")
|