From 6275aa8a064c433e5130e51268b4d937ccf2b631 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 18 Nov 2026 16:43:55 +0100 Subject: feat(dashboard): the music card shows the game being played Co-Authored-By: Claude Opus 5.5 --- static/dashboard.js | 9 +++++-- static/deck.css | 3 +++ static/modules/music.js | 70 ++++++++++++++++++++++++++++++++++++++++--------- tests/test_music.mjs | 35 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 tests/test_music.mjs diff --git a/static/dashboard.js b/static/dashboard.js index 1f89da8..828d36b 100644 --- a/static/dashboard.js +++ b/static/dashboard.js @@ -262,10 +262,15 @@ export default { w.rssi == null ? 0 : 2 * (w.rssi + 100)); // -100 dBm empty, -50 full }); - if (on.has("music")) import("./modules/music.js").then(({ default: music }) => { + if (on.has("music")) import("./modules/music.js").then(({ default: music, pickGame }) => { const el = $(".dash-music"); music.mount(el); - poll("music", music.interval ?? 2, (st) => music.update(st, el)); + // The card shows a running game (PC or Xbox) instead of the music. + const last = { music: {}, games: null, xbox: null }; + const draw = () => music.update(last.music, el, pickGame(last.games, last.xbox)); + poll("music", music.interval ?? 2, (st) => { last.music = st; draw(); }); + if (on.has("games")) poll("games", 5, (st) => { last.games = st; draw(); }); + if (on.has("xbox")) poll("xbox", 30, (st) => { last.xbox = st; draw(); }); }); // The running row: a chip per module, created now in RUNNING order and diff --git a/static/deck.css b/static/deck.css index b2ffcf3..2f81b94 100644 --- a/static/deck.css +++ b/static/deck.css @@ -714,6 +714,9 @@ select { .dash-music .music .controls { flex-wrap: nowrap; } .dash-music .music .controls button { min-height: 38px; min-width: 40px; font-size: 1rem; padding: 0; } .dash-music .music .vol { min-width: 0; } +.music[hidden], .music [hidden] { display: none; } /* .art.empty sets display: flex */ +.music.game .times { display: block; color: var(--udt-subtext0); } +.dash-music .music.game .art { width: 72px; height: 96px; } .dash .prog { display: flex; flex-direction: column; gap: 6px; } .dash .prog header { display: flex; align-items: baseline; gap: 10px; } diff --git a/static/modules/music.js b/static/modules/music.js index e127969..4a4ce05 100644 --- a/static/modules/music.js +++ b/static/modules/music.js @@ -19,6 +19,25 @@ function clock(s) { return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; } +// Minutes as "2h 05m", or "59m" under an hour. +export function hours(min) { + min = Math.max(0, Math.floor(min || 0)); + return min < 60 ? `${min}m` : `${Math.floor(min / 60)}h ${String(min % 60).padStart(2, "0")}m`; +} + +// The game the card shows instead of the music, from /api/games and +// /api/xbox: a PC game first, then the Xbox's. +export function pickGame(pc, xbox) { + if (pc?.playing) return pc.playing; + const g = xbox?.playing; + if (!g) return null; + const t = (xbox.titles ?? []).find((x) => x.id === g.id); + return { platform: "xbox", name: g.name, cover: t?.cover ?? null, minutes: t?.minutes ?? null, + session: xbox.session }; +} + +const PLATFORM = { steam: "Steam", ps1: "PlayStation", ps2: "PlayStation 2", xbox: "Xbox" }; + async function post(action, body = {}) { try { await fetch(`/api/music/${action}`, { @@ -69,6 +88,16 @@ export default { + + `; el.querySelector(".controls").prepend( button(G.prev, "prev", "Previous"), button(G.play, "play", "Play/pause"), @@ -87,18 +116,33 @@ export default { seek.addEventListener("change", () => post("seek", { position: Number(seek.value) })); }, - update(st, el) { + update(st, el, game = null) { + const [musicEl, gameEl] = el.querySelectorAll(":scope > .music"); + musicEl.hidden = Boolean(game); + gameEl.hidden = !game; + if (game) { + const img = gameEl.querySelector("img.art"); + img.hidden = !game.cover; + gameEl.querySelector(".art.empty").hidden = Boolean(game.cover); + if (game.cover && img.getAttribute("src") !== game.cover) img.src = game.cover; + gameEl.querySelector(".plat").textContent = PLATFORM[game.platform] ?? ""; + gameEl.querySelector(".title").textContent = game.name ?? ""; + const session = game.session ? (Date.now() / 1000 - game.session) / 60 : 0; + gameEl.querySelector(".times").textContent = game.minutes != null + ? `${hours(session)} now · ${hours(game.minutes + session)} total` : `${hours(session)} now`; + return; + } if (st.art) { - let img = el.querySelector("img.art"); + let img = musicEl.querySelector("img.art"); if (!img) { img = document.createElement("img"); img.className = "art"; img.alt = ""; - el.querySelector(".art").replaceWith(img); + musicEl.querySelector(".art").replaceWith(img); } if (img.getAttribute("src") !== st.art) img.src = st.art; } else { - const img = el.querySelector("img.art"); + const img = musicEl.querySelector("img.art"); if (img) { const empty = document.createElement("div"); empty.className = "art empty"; @@ -106,24 +150,24 @@ export default { img.replaceWith(empty); } } - el.querySelector(".title").textContent = st.title || "Nothing playing"; - el.querySelector(".artist").textContent = st.artist || ""; - el.querySelector(".album").textContent = st.album || ""; - el.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play; + musicEl.querySelector(".title").textContent = st.title || "Nothing playing"; + musicEl.querySelector(".artist").textContent = st.artist || ""; + musicEl.querySelector(".album").textContent = st.album || ""; + musicEl.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play; const muted = Boolean(st.volume?.muted); - const mute = el.querySelector("button.mute"); + const mute = musicEl.querySelector("button.mute"); mute.classList.toggle("on", muted); mute.textContent = muted ? G.muted : G.vol; mute.setAttribute("aria-label", muted ? "Unmute" : "Mute"); - const vol = el.querySelector(".vol input"); + const vol = musicEl.querySelector(".vol input"); vol.classList.toggle("muted", muted); if (!el._sliding) { vol.value = st.volume?.level ?? 0; - el.querySelector(".level").textContent = `${vol.value}%`; + musicEl.querySelector(".level").textContent = `${vol.value}%`; } - const seek = el.querySelector(".seek input"); + const seek = musicEl.querySelector(".seek input"); seek.max = Math.max(1, Math.floor(st.length || 0)); if (!el._dragging) seek.value = Math.min(Number(seek.max), Math.floor(st.position || 0)); - el.querySelector(".time").textContent = `${clock(st.position)} / ${clock(st.length)}`; + musicEl.querySelector(".time").textContent = `${clock(st.position)} / ${clock(st.length)}`; }, }; diff --git a/tests/test_music.mjs b/tests/test_music.mjs new file mode 100644 index 0000000..0fb8ddd --- /dev/null +++ b/tests/test_music.mjs @@ -0,0 +1,35 @@ +// deck-it - node checks for the music card's game pick +// Copyright (C) 2026 Danilo M. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import assert from "node:assert/strict"; +import { pickGame, hours } from "../static/modules/music.js"; + +const pc = { playing: { platform: "steam", name: "Worms", cover: null, minutes: 5, session: 100 } }; +const xbox = { playing: { id: "7", name: "Forza" }, session: 200, + titles: [{ id: "7", cover: "https://x/c.png", minutes: 90 }] }; + +assert.equal(pickGame(null, null), null); +assert.equal(pickGame({ playing: null }, { playing: null }), null); +assert.deepEqual(pickGame(pc, xbox), pc.playing); +assert.deepEqual(pickGame({ playing: null }, xbox), + { platform: "xbox", name: "Forza", cover: "https://x/c.png", minutes: 90, session: 200 }); +assert.deepEqual(pickGame(undefined, { playing: { id: "8", name: "New" }, session: 1 }), + { platform: "xbox", name: "New", cover: null, minutes: null, session: 1 }); + +assert.equal(hours(0), "0m"); +assert.equal(hours(59), "59m"); +assert.equal(hours(125), "2h 05m"); +console.log("music ok"); -- cgit v1.2.3