Media controls (MPRIS, Windows SMTC); stop old tracks when switching

This commit is contained in:
sarah committed 2026-10-09 18:57:03 +02:00
1 parent e2d7515e08
commit d7f5eba1c2
14 files changed
+799 -26

No files matched your search

+20 -5
View File
@@ -31,18 +31,33 @@ def Thumbnail(Data: bytes, Size: int) -> Optional[bytes]:
_Memory: dict[tuple[str, int], bytes] = {}
def _CachePath(FilePath: str, Size: int) -> Optional[Path]:
try:
Stamp = Path(FilePath).stat().st_mtime_ns
except OSError:
return None
Name = hashlib.sha1(f"{FilePath}|{Size}|{Stamp}".encode("utf-8", "surrogateescape")).hexdigest()
return CoversDir / f"{Name}.jpg"
def CoverPath(FilePath: str, Size: int) -> Optional[Path]:
"""The cached thumbnail's file (made if needed), for the system media
controls, which want a picture file rather than bytes. Blocking."""
if not CoverFor(FilePath, Size):
return None
CachePath = _CachePath(FilePath, Size)
return CachePath if CachePath and CachePath.exists() else None
def CoverFor(FilePath: str, Size: int) -> Optional[bytes]:
"""FilePath's cover at most Size pixels wide. Blocking (run it in a
thread). Cached on disk by path, size and the file's mtime."""
Key = (FilePath, Size)
if Key in _Memory:
return _Memory[Key] or None
try:
Stamp = Path(FilePath).stat().st_mtime_ns
except OSError:
CachePath = _CachePath(FilePath, Size)
if CachePath is None:
return None
Name = hashlib.sha1(f"{FilePath}|{Size}|{Stamp}".encode("utf-8", "surrogateescape")).hexdigest()
CachePath = CoversDir / f"{Name}.jpg"
if CachePath.exists():
Data = CachePath.read_bytes()
else: