diff --git a/shell/lock/LockWidgets.qml b/shell/lock/LockWidgets.qml index bee4c52..1d51ac3 100644 --- a/shell/lock/LockWidgets.qml +++ b/shell/lock/LockWidgets.qml @@ -1,5 +1,4 @@ import QtQuick -import Quickshell.Services.Mpris import Quickshell.Services.Pipewire import "../services" as S import "../applets" as C @@ -50,12 +49,7 @@ Item { color: Qt.rgba(S.Theme.base01.r, S.Theme.base01.g, S.Theme.base01.b, 0.7) border.color: Qt.rgba(S.Theme.base03.r, S.Theme.base03.g, S.Theme.base03.b, 0.3) border.width: 1 - visible: (S.Modules.lock.mpris ?? true) && _mprisPlayer !== null - - readonly property var _mprisPlayers: (Mpris.players.values ?? []).filter(p => p.trackTitle || p.playbackState === MprisPlaybackState.Playing || p.playbackState === MprisPlaybackState.Paused) - property int _playerIdx: 0 - readonly property var _mprisPlayer: _mprisPlayers[_playerIdx] ?? _mprisPlayers[0] ?? null - readonly property bool _playing: _mprisPlayer?.playbackState === MprisPlaybackState.Playing + visible: (S.Modules.lock.mpris ?? true) && S.MprisService.player !== null C.MprisApplet { id: _mprisContent @@ -63,15 +57,13 @@ Item { anchors.right: parent.right anchors.top: parent.top anchors.topMargin: 8 - player: _mprisCard._mprisPlayer - players: _mprisCard._mprisPlayers - playing: _mprisCard._playing - playerIdx: _mprisCard._playerIdx + player: S.MprisService.player + players: S.MprisService.players + playing: S.MprisService.playing + playerIdx: S.MprisService.playerIdx accentColor: S.Theme.base0D - cachedArt: _mprisCard._mprisPlayer?.trackArtUrl ?? "" - onPlayerSwitched: idx => { - _mprisCard._playerIdx = idx; - } + cachedArt: S.MprisService.player?.trackArtUrl ?? "" + onPlayerSwitched: idx => S.MprisService.switchPlayer(idx) } } diff --git a/shell/modules/MprisModule.qml b/shell/modules/MprisModule.qml index 2072bce..0edbf0e 100644 --- a/shell/modules/MprisModule.qml +++ b/shell/modules/MprisModule.qml @@ -13,14 +13,9 @@ M.BarSection { visible: opacity > 0 tooltip: "" - property int _playerIdx: 0 - readonly property var _players: (Mpris.players.values ?? []).filter(p => p.trackTitle || p.playbackState === MprisPlaybackState.Playing || p.playbackState === MprisPlaybackState.Paused) - readonly property MprisPlayer player: _players[_playerIdx] ?? _players[0] ?? null - readonly property bool playing: player?.playbackState === MprisPlaybackState.Playing - - // Reset index if current player disappears - on_PlayersChanged: if (_playerIdx >= _players.length) - _playerIdx = 0 + readonly property var _players: S.MprisService.players + readonly property MprisPlayer player: S.MprisService.player + readonly property bool playing: S.MprisService.playing property string _cachedArt: "" property string _artTrack: "" @@ -132,9 +127,9 @@ M.BarSection { accentColor: root.accentColor cachedArt: root._cachedArt cavaBars: root._cavaBars - playerIdx: root._playerIdx + playerIdx: S.MprisService.playerIdx onPlayerSwitched: idx => { - root._playerIdx = idx; + S.MprisService.switchPlayer(idx); hoverPanel.keepOpen(400); } } diff --git a/shell/services/MprisService.qml b/shell/services/MprisService.qml new file mode 100644 index 0000000..9f06ead --- /dev/null +++ b/shell/services/MprisService.qml @@ -0,0 +1,21 @@ +pragma Singleton + +import QtQuick +import Quickshell.Services.Mpris + +QtObject { + id: root + + readonly property var players: (Mpris.players.values ?? []).filter(p => p.trackTitle || p.playbackState === MprisPlaybackState.Playing || p.playbackState === MprisPlaybackState.Paused) + property int playerIdx: 0 + readonly property MprisPlayer player: players[playerIdx] ?? players[0] ?? null + readonly property bool playing: player?.playbackState === MprisPlaybackState.Playing + + // Reset index if current player disappears + onPlayersChanged: if (playerIdx >= players.length) + playerIdx = 0 + + function switchPlayer(idx) { + playerIdx = idx; + } +} diff --git a/shell/services/qmldir b/shell/services/qmldir index a5af319..37969bb 100644 --- a/shell/services/qmldir +++ b/shell/services/qmldir @@ -8,3 +8,4 @@ singleton NotifService 1.0 NotifService.qml NotifItem 1.0 NotifItem.qml singleton LockService 1.0 LockService.qml singleton BacklightService 1.0 BacklightService.qml +singleton MprisService 1.0 MprisService.qml