From cee1f5837ee3716129adc865af0b5e646933d3aa Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Sun, 13 Jul 2025 20:32:51 -0700 Subject: [PATCH] service/mpris: make lengthSupported bindable and notify for changes Fixes #109 --- src/services/mpris/player.cpp | 8 ++++---- src/services/mpris/player.hpp | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/services/mpris/player.cpp b/src/services/mpris/player.cpp index 67c562d3..45d5cd4a 100644 --- a/src/services/mpris/player.cpp +++ b/src/services/mpris/player.cpp @@ -99,6 +99,8 @@ MprisPlayer::MprisPlayer(const QString& address, QObject* parent): QObject(paren } else return static_cast(-1); }); + this->bLengthSupported.setBinding([this]() { return this->bInternalLength != -1; }); + this->bPlaybackState.setBinding([this]() { const auto& status = this->bpPlaybackStatus.value(); @@ -258,21 +260,19 @@ void MprisPlayer::setPosition(qlonglong position) { } void MprisPlayer::onExportedPositionChanged() { - if (!this->lengthSupported()) emit this->lengthChanged(); + if (!this->bLengthSupported) emit this->lengthChanged(); } void MprisPlayer::onSeek(qlonglong time) { this->setPosition(time); } qreal MprisPlayer::length() const { - if (this->bInternalLength == -1) { + if (!this->bLengthSupported) { return this->position(); // unsupported } else { return static_cast(this->bInternalLength / 1000) / 1000; // NOLINT } } -bool MprisPlayer::lengthSupported() const { return this->bInternalLength != -1; } - bool MprisPlayer::volumeSupported() const { return this->pVolume.exists(); } void MprisPlayer::setVolume(qreal volume) { diff --git a/src/services/mpris/player.hpp b/src/services/mpris/player.hpp index a2ea59be..89bc27a2 100644 --- a/src/services/mpris/player.hpp +++ b/src/services/mpris/player.hpp @@ -117,7 +117,7 @@ class MprisPlayer: public QObject { /// The length of the playing track, as seconds, with millisecond precision, /// or the value of @@position if @@lengthSupported is false. Q_PROPERTY(qreal length READ length NOTIFY lengthChanged); - Q_PROPERTY(bool lengthSupported READ lengthSupported NOTIFY lengthSupportedChanged); + Q_PROPERTY(bool lengthSupported READ default NOTIFY lengthSupportedChanged BINDABLE bindableLengthSupported); /// The volume of the playing track from 0.0 to 1.0, or 1.0 if @@volumeSupported is false. /// /// May only be written to if @@canControl and @@volumeSupported are true. @@ -274,7 +274,7 @@ public: void setPosition(qreal position); [[nodiscard]] qreal length() const; - [[nodiscard]] bool lengthSupported() const; + [[nodiscard]] QBindable bindableLengthSupported() const { return &this->bLengthSupported; } [[nodiscard]] qreal volume() const { return this->bVolume; }; [[nodiscard]] bool volumeSupported() const; @@ -447,6 +447,7 @@ private: Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, QString, bTrackAlbumArtist, &MprisPlayer::trackAlbumArtistChanged); Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, QString, bTrackArtUrl, &MprisPlayer::trackArtUrlChanged); Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, qlonglong, bInternalLength, &MprisPlayer::lengthChanged); + Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, bool, bLengthSupported, &MprisPlayer::lengthSupportedChanged); Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, bool, bShuffle, &MprisPlayer::shuffleChanged); QS_DBUS_BINDABLE_PROPERTY_GROUP(MprisPlayer, playerProperties);