From 788df7773c0af8c66c6c49dc333abd7a3aeeba3a Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Sat, 5 Apr 2025 00:20:40 -0400 Subject: [PATCH] mpris working! --- widget/Mpris.tsx | 59 ++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/widget/Mpris.tsx b/widget/Mpris.tsx index de1ce90..653d373 100644 --- a/widget/Mpris.tsx +++ b/widget/Mpris.tsx @@ -1,4 +1,4 @@ -import { bind, Variable } from "astal"; +import { bind, derive, Variable } from "astal"; import AstalMpris from "gi://AstalMpris"; function IntegerToMinuteSeconds(value: number) { @@ -7,40 +7,49 @@ function IntegerToMinuteSeconds(value: number) { return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; } -function FormatLabel(player: AstalMpris.Player) { +function FormatLabel(player: { + title: string; + artist: string; + position: number; + length: number; +}) { return `${player.title} - ${player.artist} [${IntegerToMinuteSeconds(player.position)}/${IntegerToMinuteSeconds(player.length)}]`; } const Mpris = function () { const mpris = AstalMpris.get_default(); - mpris.connect("player-added", () => { - players.set(mpris.players); - }); - mpris.connect("player-closed", () => { - players.set(mpris.players); - }); + const players = bind(mpris, "players"); + const activeIndex = Variable(0); - const players = Variable>(mpris.players); - - const label = Variable.derive([players], (players) => { - const player = players[0]; - if (!player.title || !player.artist || !player.position || !player.length) { - return ""; - } - return FormatLabel(player); - }); - - label.subscribe((value) => print(value)); + bind( + derive([players, activeIndex], (players, index) => players[index]), + ).subscribe((value) => print(value.title)); return ( - ); };