Fix a bug where album art and track info could get out of sync if you advance twice quickly

This commit is contained in:
James Babcock committed 2026-10-13 16:17:07 -07:00
1 parent eb91d46657
commit 13da595de0
6 files changed
+38 -13

No files matched your search

+4 -5
View File
@@ -49,7 +49,8 @@ export class MusicWidget implements GlanceWidget {
private unsubscribe: (() => void) | null = null;
private progressTimer: ReturnType<typeof setInterval> | null = null;
private art: GrayImage | null = null;
private artKey = "";
/** The artVersion `art` was fetched for; -1 = not fetched yet. */
private artVersion = -1;
start(requestRender: () => void): void {
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
@@ -129,10 +130,8 @@ export class MusicWidget implements GlanceWidget {
}
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
const key = `${media.packageName}|${media.title}|${media.album}`;
// Retry while null: players often publish metadata before the bitmap.
if (key !== this.artKey || this.art === null) {
this.artKey = key;
if (media.artVersion !== this.artVersion) {
this.artVersion = media.artVersion;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
}
if (this.art) {
+8 -7
View File
@@ -83,7 +83,8 @@ class MusicAppLayer implements Layer {
*/
private libraryAppsFresh = false;
private art: GrayImage | null = null;
private artKey = "";
/** The artVersion `art` was fetched for; -1 = not fetched yet. */
private artVersion = -1;
isPlaylistFocused(): boolean {
return this.focusColumn === "playlist";
@@ -371,12 +372,12 @@ class MusicAppLayer implements Layer {
}
private drawArt(image: GrayImage, media: MediaControllerState): void {
const key = `${media.packageName}|${media.title}|${media.album}`;
// Retry while null: players (e.g. Spotify) often publish metadata before
// the art bitmap is downloaded, then re-emit the same strings with the
// bitmap attached. The no-art fetch path is a cheap near-no-op.
if (key !== this.artKey || this.art === null) {
this.artKey = key;
// Keyed on artVersion, not the strings: players (e.g. Spotify) publish
// metadata before the art bitmap is downloaded, or with the previous
// track's bitmap after a quick double skip, then re-emit the same
// strings with the right bitmap attached.
if (media.artVersion !== this.artVersion) {
this.artVersion = media.artVersion;
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
}
if (this.art) {
+9
View File
@@ -37,6 +37,12 @@ export type MediaControllerState = {
canPlayPause: boolean;
canSkipNext: boolean;
canSkipPrevious: boolean;
/**
* Changes whenever getAlbumArt's result may have changed (any metadata
* update or session switch). Key cached art on this, not the title:
* players re-publish the same strings with a different bitmap.
*/
artVersion: number;
};
const DEFAULT_MEDIA_STATE: MediaControllerState = {
@@ -55,6 +61,7 @@ const DEFAULT_MEDIA_STATE: MediaControllerState = {
canPlayPause: false,
canSkipNext: false,
canSkipPrevious: false,
artVersion: 0,
};
export class FaceclawMediaControllerBridge {
@@ -206,6 +213,7 @@ export class FaceclawMediaControllerBridge {
canSkipPrevious: boolean,
accessEnabled: boolean,
status: string,
artVersion: number,
) => {
this.state = {
available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle",
@@ -223,6 +231,7 @@ export class FaceclawMediaControllerBridge {
canPlayPause: Boolean(canPlayPause),
canSkipNext: Boolean(canSkipNext),
canSkipPrevious: Boolean(canSkipPrevious),
artVersion: Number(artVersion),
};
this.stateUpdatedAtMs = Date.now();
for (const listener of this.stateListeners) {