From ce70addd5b17da95a6e05c1fb1e94cb03944d99d Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 10 Nov 2026 18:28:20 +0100 Subject: feat(music): a volume slider with a mute toggle, icon buttons The Vol-, Mute and Vol+ buttons give way to a mute toggle (its icon shows the state) and a 0-100 slider that sets the default sink with wpctl as it moves. Previous, play/pause and next are Nerd Font glyphs instead of the tablet's Unicode symbols. Co-Authored-By: Claude Opus 5.5 --- deckit/modules/music.py | 9 +++++++-- static/deck.css | 8 ++++++-- static/modules/music.js | 48 +++++++++++++++++++++++++++++++++++++----------- tests/test_music.py | 9 +++++++++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/deckit/modules/music.py b/deckit/modules/music.py index aa30798..ef19cd0 100644 --- a/deckit/modules/music.py +++ b/deckit/modules/music.py @@ -23,9 +23,7 @@ from ..module import Module, ModuleError SEP = "\x1f" BASE_ACTIONS = { - "voldown": ["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%-"], "mute": ["wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle"], - "volup": ["wpctl", "set-volume", "-l", "1.0", "@DEFAULT_AUDIO_SINK@", "5%+"], "prev": ["playerctl", "previous"], "play": ["playerctl", "play-pause"], "next": ["playerctl", "next"], @@ -73,6 +71,13 @@ def make(cfg: dict) -> Module: raise ModuleError("position must be a number") popen(["playerctl", "position", str(round(float(pos), 1))]) + def volume(body): + level = body.get("level") + if isinstance(level, bool) or not isinstance(level, (int, float)) or not 0 <= level <= 100: + raise ModuleError("level must be 0-100") + popen(["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", f"{level / 100:.2f}"]) + actions = {name: (lambda cmd: (lambda body: popen(cmd)))(cmd) for name, cmd in BASE_ACTIONS.items()} actions["seek"] = seek + actions["volume"] = volume return Module(name="music", title="Music", state=state, actions=actions) diff --git a/static/deck.css b/static/deck.css index b16ac7f..1aedc6f 100644 --- a/static/deck.css +++ b/static/deck.css @@ -214,13 +214,17 @@ body { text-overflow: ellipsis; white-space: nowrap; } -.music .controls button { min-height: 40px; min-width: 44px; } +.music .controls button { min-height: 44px; min-width: 48px; font-size: 1.2rem; } +.music .vol { flex: 1; min-width: 180px; display: flex; align-items: center; gap: 10px; } +.music .vol input { flex: 1; accent-color: var(--udt-accent); } +.music .vol input.muted { opacity: .4; } +.music .level { font-size: 0.857rem; color: var(--udt-subtext0); min-width: 3.5ch; text-align: right; } .music .artist, .music .album { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .music .album { font-size: 0.857rem; color: var(--udt-overlay0); } .music .seek { display: flex; align-items: center; gap: 10px; margin: 6px 0 12px; } .music .seek input { flex: 1; accent-color: var(--udt-accent); } .music .time { font-size: 0.857rem; color: var(--udt-subtext0); white-space: nowrap; } -.music .controls { display: flex; flex-wrap: wrap; gap: 8px; } +.music .controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } .sbb .sbb-now { margin-bottom: 8px; font-size: 0.857rem; font-weight: 600; } diff --git a/static/modules/music.js b/static/modules/music.js index b78a401..5ba3386 100644 --- a/static/modules/music.js +++ b/static/modules/music.js @@ -31,9 +31,15 @@ async function post(action, body = {}) { } } -function button(label, action) { +// Nerd Font glyphs, as on the other tabs. +const G = { prev: "\uf048", play: "\uf04b", pause: "\uf04c", next: "\uf051", + vol: "\uf028", muted: "\uf026" }; + +function button(glyph, action, label) { const b = document.createElement("button"); - b.textContent = label; + b.className = `nf ${action}`; + b.textContent = glyph; + b.setAttribute("aria-label", label); b.onclick = () => post(action); return b; } @@ -57,11 +63,24 @@ export default { 0:00 -
+
+
+ + +
+
`; - el.querySelector(".controls").append( - button("⏮", "prev"), button("⏯", "play"), button("⏭", "next"), - button("Vol−", "voldown"), button("Mute", "mute"), button("Vol+", "volup")); + el.querySelector(".controls").prepend( + button(G.prev, "prev", "Previous"), button(G.play, "play", "Play/pause"), + button(G.next, "next", "Next")); + el.querySelector(".vol").prepend(button(G.vol, "mute", "Mute")); + const vol = el.querySelector(".vol input"); + vol.addEventListener("pointerdown", () => { el._sliding = true; }); + vol.addEventListener("pointerup", () => { el._sliding = false; }); + vol.addEventListener("input", () => { + el.querySelector(".level").textContent = `${vol.value}%`; + post("volume", { level: Number(vol.value) }); + }); const seek = el.querySelector(".seek input"); seek.addEventListener("pointerdown", () => { el._dragging = true; }); seek.addEventListener("pointerup", () => { el._dragging = false; }); @@ -82,11 +101,18 @@ export default { el.querySelector(".title").textContent = st.title || "Nothing playing"; el.querySelector(".artist").textContent = st.artist || ""; el.querySelector(".album").textContent = st.album || ""; - el.querySelector(".controls button:nth-child(2)").textContent = - st.status === "Playing" ? "⏸" : "▶"; - const mute = el.querySelector(".controls button:nth-child(5)"); - mute.classList.toggle("on", Boolean(st.volume?.muted)); - mute.textContent = st.volume?.muted ? "Unmute" : "Mute"; + el.querySelector("button.play").textContent = st.status === "Playing" ? G.pause : G.play; + const muted = Boolean(st.volume?.muted); + const mute = el.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"); + vol.classList.toggle("muted", muted); + if (!el._sliding) { + vol.value = st.volume?.level ?? 0; + el.querySelector(".level").textContent = `${vol.value}%`; + } const seek = el.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)); diff --git a/tests/test_music.py b/tests/test_music.py index ccf0539..aadfcc5 100644 --- a/tests/test_music.py +++ b/tests/test_music.py @@ -53,6 +53,15 @@ class Music(unittest.TestCase): with self.assertRaises(music.ModuleError, msg=bad): m.actions["seek"](bad) + def test_volume_sets_the_sink(self): + calls = [] + m = music.make({"run": lambda *a, **k: "", "popen": calls.append}) + m.actions["volume"]({"level": 45}) + self.assertEqual(calls[0], ["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "0.45"]) + for bad in ({}, {"level": 101}, {"level": -1}, {"level": "5"}, {"level": True}): + with self.assertRaises(music.ModuleError, msg=bad): + m.actions["volume"](bad) + if __name__ == "__main__": unittest.main() -- cgit v1.2.3