feat: Playtube Stream Dock Plugin 1.0.0 (Ajazz AKP153E) mit Icon-Pack
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* "Aktueller Titel" fuer die Info-Felder rechts am Geraet (Controller "Information"): ein
|
||||
* einzelnes quadratisches Bild - Cover als Hintergrund, unten Titel, Interpret und Fortschritt.
|
||||
*/
|
||||
|
||||
const { wrapTitle } = require('./actions');
|
||||
|
||||
const SIZE = 144;
|
||||
const BACKGROUND = '#171b24';
|
||||
const ACCENT = '#ff4d5e';
|
||||
const TRACK = '#3a4152';
|
||||
const MUTED_TEXT = '#c3c9d6';
|
||||
const BAR_X = 10;
|
||||
const BAR_WIDTH = SIZE - 2 * BAR_X;
|
||||
const BAR_Y = 132;
|
||||
const BAR_HEIGHT = 6;
|
||||
const TITLE_LINE_HEIGHT = 25;
|
||||
const TITLE_BASELINE_OFFSET = 7; // Schrifthoehe/3: bringt die optische Mitte der Zeile auf die Blockmitte
|
||||
const FONT = 'font-family="Segoe UI, Arial, sans-serif"';
|
||||
const XML_ESCAPES = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
||||
|
||||
function escapeXml(text) {
|
||||
return String(text).replace(/[&<>"']/g, (char) => XML_ESCAPES[char]);
|
||||
}
|
||||
|
||||
function formatTime(seconds) {
|
||||
if (!Number.isFinite(seconds) || seconds < 0) return '0:00';
|
||||
const whole = Math.floor(seconds);
|
||||
return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function textLines(text, x, y, lineHeight, size, color, weight, anchor = 'middle') {
|
||||
return String(text).split('\n').filter(Boolean).map((line, index) => (
|
||||
`<text x="${x}" y="${y + index * lineHeight}" ${FONT} font-size="${size}" font-weight="${weight}" `
|
||||
+ `fill="${color}" text-anchor="${anchor}">${escapeXml(line)}</text>`)).join('');
|
||||
}
|
||||
|
||||
function noteMarkup(cx, cy, scale) {
|
||||
const s = scale;
|
||||
return `<circle cx="${cx - 9 * s}" cy="${cy + 14 * s}" r="${10 * s}" fill="${ACCENT}"/>`
|
||||
+ `<rect x="${cx - 1 * s}" y="${cy - 22 * s}" width="${6 * s}" height="${38 * s}" fill="${ACCENT}"/>`
|
||||
+ `<polygon points="${cx + 5 * s},${cy - 22 * s} ${cx + 24 * s},${cy - 12 * s} ${cx + 5 * s},${cy - 4 * s}" fill="${ACCENT}"/>`;
|
||||
}
|
||||
|
||||
function backgroundMarkup(coverDataUri) {
|
||||
if (!coverDataUri) return `<rect width="${SIZE}" height="${SIZE}" fill="${BACKGROUND}"/>${noteMarkup(72, 44, 1.1)}`;
|
||||
return `<rect width="${SIZE}" height="${SIZE}" fill="${BACKGROUND}"/>`
|
||||
+ `<image x="0" y="0" width="${SIZE}" height="${SIZE}" preserveAspectRatio="xMidYMid slice" href="${coverDataUri}"/>`;
|
||||
}
|
||||
|
||||
/** Abdunklung unten, damit Text auf jedem Cover lesbar bleibt. */
|
||||
function shadeMarkup() {
|
||||
return '<defs><linearGradient id="shade" x1="0" y1="0" x2="0" y2="1">'
|
||||
+ '<stop offset="0" stop-color="#000" stop-opacity="0"/><stop offset="0.35" stop-color="#000" stop-opacity="0.72"/>'
|
||||
+ '<stop offset="1" stop-color="#000" stop-opacity="0.9"/></linearGradient></defs>'
|
||||
+ `<rect x="0" y="30" width="${SIZE}" height="${SIZE - 30}" fill="url(#shade)"/>`;
|
||||
}
|
||||
|
||||
function playStateMarkup(playing) {
|
||||
const cx = 118;
|
||||
const cy = 26;
|
||||
const disc = `<circle cx="${cx}" cy="${cy}" r="19" fill="#000" fill-opacity="0.6"/>`;
|
||||
if (playing) {
|
||||
return `${disc}<rect x="${cx - 9}" y="${cy - 10}" width="6" height="20" fill="#fff"/>`
|
||||
+ `<rect x="${cx + 3}" y="${cy - 10}" width="6" height="20" fill="#fff"/>`;
|
||||
}
|
||||
return `${disc}<polygon points="${cx - 7},${cy - 11} ${cx + 11},${cy} ${cx - 7},${cy + 11}" fill="#fff"/>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object|null} view - {title, subtitle, playing, currentTime, duration, coverDataUri}
|
||||
* oder null, wenn Playtube nicht verbunden ist
|
||||
* @returns {string} SVG-Text (144x144)
|
||||
*/
|
||||
function buildInfoSvg(view) {
|
||||
const head = `<svg xmlns="http://www.w3.org/2000/svg" width="${SIZE}" height="${SIZE}" viewBox="0 0 ${SIZE} ${SIZE}">`;
|
||||
if (!view) {
|
||||
return `${head}<rect width="${SIZE}" height="${SIZE}" fill="${BACKGROUND}"/>${noteMarkup(72, 44, 1.1)}`
|
||||
+ `${textLines('Playtube offline', 72, 120, 0, 14, '#ffffff', 'bold')}</svg>`;
|
||||
}
|
||||
const hasTrack = Boolean(view.title);
|
||||
const ratio = view.duration > 0 ? Math.max(0, Math.min(1, view.currentTime / view.duration)) : 0;
|
||||
const parts = [backgroundMarkup(view.coverDataUri), shadeMarkup()];
|
||||
if (hasTrack) {
|
||||
// Titel (und darunter der Interpret) als Block genau in der Bildmitte, mittig ausgerichtet.
|
||||
const title = wrapTitle(view.title, 10, 2);
|
||||
const lineCount = title.split('\n').length;
|
||||
const blockCenter = view.subtitle ? SIZE / 2 - 12 : SIZE / 2;
|
||||
const firstBaseline = blockCenter - ((lineCount - 1) * TITLE_LINE_HEIGHT) / 2 + TITLE_BASELINE_OFFSET;
|
||||
parts.push(textLines(title, SIZE / 2, firstBaseline, TITLE_LINE_HEIGHT, 22, '#ffffff', 'bold'));
|
||||
if (view.subtitle) {
|
||||
const subtitleY = firstBaseline + (lineCount - 1) * TITLE_LINE_HEIGHT + 27;
|
||||
parts.push(textLines(wrapTitle(view.subtitle, 13, 1), SIZE / 2, subtitleY, 0, 16, MUTED_TEXT, 'normal'));
|
||||
}
|
||||
parts.push(playStateMarkup(view.playing));
|
||||
} else {
|
||||
parts.push(textLines('Nichts spielt', 72, 120, 0, 14, '#ffffff', 'bold'));
|
||||
}
|
||||
parts.push(`<rect x="${BAR_X}" y="${BAR_Y}" width="${BAR_WIDTH}" height="${BAR_HEIGHT}" rx="3" fill="${TRACK}"/>`);
|
||||
parts.push(`<rect x="${BAR_X}" y="${BAR_Y}" width="${(BAR_WIDTH * ratio).toFixed(1)}" height="${BAR_HEIGHT}" rx="3" fill="${ACCENT}"/>`);
|
||||
return `${head}${parts.join('')}</svg>`;
|
||||
}
|
||||
|
||||
function svgToDataUri(svg) {
|
||||
return `data:image/svg+xml;base64,${Buffer.from(svg, 'utf8').toString('base64')}`;
|
||||
}
|
||||
|
||||
module.exports = { buildInfoSvg, svgToDataUri, formatTime, SIZE };
|
||||
Reference in New Issue
Block a user