feat: Playlist-Tasten zeigen das Cover der Playlist (1.1.0)
Das Plugin holt fuer jede Playlist-Taste einmal das Cover ueber den neuen Playtube-Befehl playlist_cover (ab Playtube 2.12.0) und speichert es in den Tasten-Einstellungen; das Cover gilt nur fuer die Playlist, fuer die es geholt wurde. Aeltere Playtube: Taste zeigt wie bisher den Namen. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+18
-1
@@ -17,6 +17,8 @@ const DEFAULTS = Object.freeze({
|
||||
tab: 'toggle', // Aktion "Tab wechseln": "toggle" | "youtube" | "music"
|
||||
playlistId: '',
|
||||
playlistTitle: '',
|
||||
playlistCover: '', // Cover-Adresse, von Playtube geholt (Befehl "playlist_cover")
|
||||
playlistCoverId: '', // zu welcher Playlist das gespeicherte Cover gehoert
|
||||
});
|
||||
|
||||
const TARGETS = ['auto', 'youtube', 'music'];
|
||||
@@ -40,9 +42,18 @@ function normalizeSettings(raw, titleDefault = false) {
|
||||
settings.showTitle = show === true || show === 'true';
|
||||
settings.playlistId = typeof settings.playlistId === 'string' ? settings.playlistId.trim() : '';
|
||||
settings.playlistTitle = typeof settings.playlistTitle === 'string' ? settings.playlistTitle : '';
|
||||
// Ein gespeichertes Cover gilt nur fuer die Playlist, fuer die es geholt wurde.
|
||||
const cover = typeof settings.playlistCover === 'string' ? settings.playlistCover : '';
|
||||
settings.playlistCover = cover.startsWith('https://') && settings.playlistCoverId === settings.playlistId ? cover : '';
|
||||
return settings;
|
||||
}
|
||||
|
||||
/** Braucht diese Playlist-Taste noch ein Cover von Playtube? */
|
||||
function needsPlaylistCover(rawSettings) {
|
||||
const settings = normalizeSettings(rawSettings);
|
||||
return Boolean(settings.playlistId) && !settings.playlistCover;
|
||||
}
|
||||
|
||||
/** Welcher Tab ist gemeint? "auto" = der Tab, den Playtube als aktiv meldet. */
|
||||
function pickTab(state, target) {
|
||||
if (!state || !state.tabs) return null;
|
||||
@@ -173,7 +184,12 @@ const DEFINITIONS = {
|
||||
},
|
||||
playlist: {
|
||||
request: (s) => (s.playlistId ? { cmd: 'play_playlist', fields: { playlist: s.playlistId } } : null),
|
||||
render: (s) => ({ title: s.playlistTitle ? wrapTitle(s.playlistTitle) : '' }),
|
||||
// Mit Cover zeigt die Taste das Bild (Titel nur, wenn "Titel anzeigen" an ist); ohne Cover den
|
||||
// Namen wie bisher.
|
||||
render: (s) => ({
|
||||
coverUrl: s.playlistCover || null,
|
||||
title: s.playlistTitle && (!s.playlistCover || s.showTitle) ? wrapTitle(s.playlistTitle) : '',
|
||||
}),
|
||||
},
|
||||
switchtab: {
|
||||
request: (s) => ({ cmd: 'switch_tab', fields: { target: s.tab === 'toggle' ? 'auto' : s.tab } }),
|
||||
@@ -212,6 +228,7 @@ module.exports = {
|
||||
DEFAULTS,
|
||||
DEFINITIONS,
|
||||
normalizeSettings,
|
||||
needsPlaylistCover,
|
||||
pickTab,
|
||||
wrapTitle,
|
||||
actionIdFromUuid,
|
||||
|
||||
+36
-2
@@ -5,11 +5,12 @@
|
||||
* - haelt fuer jede sichtbare Taste Aktion + Einstellungen,
|
||||
* - fuehrt Tastendruecke als Playtube-Befehle aus (Lautstaerke: Dauerdruck mit Wiederholung),
|
||||
* - fragt den Status regelmaessig ab und aktualisiert Titel/Zustand/Cover der Tasten,
|
||||
* - beantwortet die Playlist-Anfrage des Property Inspectors.
|
||||
* - beantwortet die Playlist-Anfrage des Property Inspectors,
|
||||
* - holt fuer Playlist-Tasten einmal das Cover bei Playtube und speichert es in den Einstellungen.
|
||||
*/
|
||||
|
||||
const {
|
||||
DEFINITIONS, actionIdFromUuid, buildRequest, normalizeSettings, pickTab, renderAction,
|
||||
DEFINITIONS, actionIdFromUuid, buildRequest, needsPlaylistCover, normalizeSettings, pickTab, renderAction,
|
||||
} = require('./actions');
|
||||
const { buildInfoSvg, svgToDataUri } = require('./infoPanel');
|
||||
|
||||
@@ -52,6 +53,7 @@ class Controller {
|
||||
this.state = null;
|
||||
this.covers = new Map(); // url -> dataUri (Einfuegereihenfolge = Alter)
|
||||
this.coverLoading = new Set();
|
||||
this.playlistCoverTried = new Set(); // "context:playlistId" - pro Verbindung nur einmal fragen
|
||||
this.repeaters = new Map(); // context -> {delayTimer, intervalTimer}
|
||||
this.pollTimer = null;
|
||||
this.refreshTimer = null;
|
||||
@@ -62,6 +64,8 @@ class Controller {
|
||||
this.host.on('event', (message) => this.handleEvent(message));
|
||||
this.client.on('connected', (name) => {
|
||||
this.logger.info(`Mit Playtube verbunden (${name})`);
|
||||
this.playlistCoverTried.clear();
|
||||
for (const context of this.keys.keys()) this.ensurePlaylistCover(context);
|
||||
this.poll();
|
||||
});
|
||||
this.client.on('disconnected', () => {
|
||||
@@ -113,6 +117,7 @@ class Controller {
|
||||
this.logger.info(`willAppear ${actionId} controller=${payload.controller} info=${info}`);
|
||||
this.keys.set(message.context, { actionId, settings, info, last: null });
|
||||
this.renderKey(message.context);
|
||||
this.ensurePlaylistCover(message.context);
|
||||
if (this.client.connected && !this.state) this.poll();
|
||||
}
|
||||
|
||||
@@ -122,6 +127,35 @@ class Controller {
|
||||
key.settings = (message.payload && message.payload.settings) || {};
|
||||
key.last = null;
|
||||
this.renderKey(message.context);
|
||||
this.ensurePlaylistCover(message.context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Playlist-Tasten: Cover einmal bei Playtube holen (Befehl "playlist_cover", ab Playtube 2.12.0)
|
||||
* und in den Tasten-Einstellungen speichern - beim naechsten Start ist es sofort da. Eine aeltere
|
||||
* Playtube kennt den Befehl nicht; dann bleibt die Taste wie bisher beim Namen.
|
||||
*/
|
||||
async ensurePlaylistCover(context) {
|
||||
const key = this.keys.get(context);
|
||||
if (!key || key.actionId !== 'playlist' || !this.client.connected) return;
|
||||
if (!needsPlaylistCover(key.settings)) return;
|
||||
const playlistId = normalizeSettings(key.settings).playlistId;
|
||||
const attempt = `${context}:${playlistId}`;
|
||||
if (this.playlistCoverTried.has(attempt)) return;
|
||||
this.playlistCoverTried.add(attempt);
|
||||
try {
|
||||
const response = await this.client.request('playlist_cover', { playlist: playlistId });
|
||||
const cover = response && typeof response.cover === 'string' && response.cover.startsWith('https://') ? response.cover : '';
|
||||
const current = this.keys.get(context);
|
||||
// Inzwischen eine andere Playlist gewaehlt oder Taste entfernt -> verwerfen.
|
||||
if (!cover || !current || normalizeSettings(current.settings).playlistId !== playlistId) return;
|
||||
current.settings = { ...current.settings, playlistCover: cover, playlistCoverId: playlistId };
|
||||
current.last = null;
|
||||
this.host.setSettings(context, current.settings);
|
||||
this.renderKey(context);
|
||||
} catch (error) {
|
||||
this.logger.warn(`Playlist-Cover nicht lesbar: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
onKeyDown(context) {
|
||||
|
||||
Reference in New Issue
Block a user