use Instantiator for mpris state watchers instead of manual createObject
This commit is contained in:
parent
0ca190a4a6
commit
1b6c2fee91
1 changed files with 13 additions and 31 deletions
|
|
@ -1,6 +1,7 @@
|
|||
pragma Singleton
|
||||
|
||||
import QtQuick
|
||||
import QtQml.Models
|
||||
import Quickshell.Services.Mpris
|
||||
|
||||
QtObject {
|
||||
|
|
@ -13,7 +14,6 @@ QtObject {
|
|||
|
||||
property string _selectedIdentity: ""
|
||||
property var _lastPlayedTime: ({})
|
||||
property var _watchers: []
|
||||
|
||||
onPlayerChanged: {
|
||||
if (player)
|
||||
|
|
@ -33,33 +33,18 @@ QtObject {
|
|||
}
|
||||
}
|
||||
|
||||
// Watch raw Mpris.players for add/remove - this does NOT fire on state changes
|
||||
property Connections _mprisConn: Connections {
|
||||
target: Mpris
|
||||
function onPlayersChanged() {
|
||||
root._reconnectWatchers();
|
||||
}
|
||||
}
|
||||
|
||||
function _reconnectWatchers() {
|
||||
for (const w of _watchers)
|
||||
w.destroy();
|
||||
|
||||
const raw = Mpris.players.values ?? [];
|
||||
const newWatchers = [];
|
||||
for (const p of raw) {
|
||||
const conn = _watcherComp.createObject(root, {
|
||||
target: p
|
||||
});
|
||||
newWatchers.push(conn);
|
||||
}
|
||||
_watchers = newWatchers;
|
||||
}
|
||||
|
||||
property Component _watcherComp: Component {
|
||||
Connections {
|
||||
function onPlaybackStateChanged() {
|
||||
root._onPlaybackStateChanged(target);
|
||||
// Watch all raw players for state changes via Instantiator -
|
||||
// delegates persist across state changes since the model only
|
||||
// changes on player add/remove, not property changes
|
||||
property Instantiator _stateWatchers: Instantiator {
|
||||
model: Mpris.players
|
||||
delegate: QtObject {
|
||||
required property MprisPlayer modelData
|
||||
property Connections _conn: Connections {
|
||||
target: modelData
|
||||
function onPlaybackStateChanged() {
|
||||
root._onPlaybackStateChanged(modelData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -84,7 +69,6 @@ QtObject {
|
|||
}
|
||||
}
|
||||
} else if (p.playbackState === MprisPlaybackState.Paused && p === player) {
|
||||
// Current player paused - switch to a playing one if available
|
||||
_switchToPlaying();
|
||||
}
|
||||
}
|
||||
|
|
@ -139,6 +123,4 @@ QtObject {
|
|||
if (players[idx])
|
||||
_selectedIdentity = players[idx].identity ?? "";
|
||||
}
|
||||
|
||||
Component.onCompleted: _reconnectWatchers()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue