me like nix
1pragma Singleton
2
3import QtQuick
4import Quickshell
5import Quickshell.Services.Mpris as MprisService
6
7QtObject {
8 id: root
9
10 property MprisService.MprisPlayer _trackedPlayer: null
11
12 readonly property MprisService.MprisPlayer activePlayer: {
13 // If tracked player is valid and playing, prefer it
14 if (_trackedPlayer && _trackedPlayer.isPlaying) return _trackedPlayer
15 // Otherwise find any playing player
16 var players = MprisService.Mpris.players.values
17 for (var i = 0; i < players.length; i++) {
18 if (players[i].isPlaying) return players[i]
19 }
20 // Fall back to tracked or first available
21 if (_trackedPlayer) return _trackedPlayer
22 return players.length > 0 ? players[0] : null
23 }
24
25 readonly property string title: activePlayer ? activePlayer.trackTitle : ""
26 readonly property string artist: activePlayer ? activePlayer.trackArtist : ""
27 readonly property string album: activePlayer ? activePlayer.trackAlbum : ""
28 readonly property string artUrl: activePlayer ? activePlayer.trackArtUrl : ""
29 readonly property bool isPlaying: activePlayer ? activePlayer.isPlaying : false
30 readonly property real position: activePlayer ? activePlayer.position : 0
31 readonly property real length: activePlayer ? activePlayer.length : 0
32 readonly property bool hasPlayer: activePlayer !== null
33 readonly property bool canNext: activePlayer ? activePlayer.canGoNext : false
34 readonly property bool canPrev: activePlayer ? activePlayer.canGoPrevious : false
35
36 // Track player changes reactively via Instantiator
37 property var _tracker: Instantiator {
38 model: MprisService.Mpris.players
39 delegate: QtObject {
40 required property MprisService.MprisPlayer modelData
41
42 property var _conn: Connections {
43 target: modelData
44
45 Component.onCompleted: {
46 if (root._trackedPlayer === null || modelData.isPlaying) {
47 root._trackedPlayer = modelData
48 }
49 }
50
51 function onPlaybackStateChanged() {
52 if (modelData.isPlaying) {
53 root._trackedPlayer = modelData
54 }
55 }
56 }
57 }
58 }
59
60 function togglePlaying() { if (activePlayer) activePlayer.togglePlaying() }
61 function next() { if (activePlayer) activePlayer.next() }
62 function previous() { if (activePlayer) activePlayer.previous() }
63}