Skip to content

Commit e2ef7b7

Browse files
committed
service/mpris: add isPlaying
1 parent db9e633 commit e2ef7b7

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/services/mpris/player.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ MprisPlayer::MprisPlayer(const QString& address, QObject* parent): QObject(paren
6565
this->bCanGoPrevious.setBinding([this]() { return this->bCanControl && this->bpCanGoPrevious; });
6666

6767
this->bCanTogglePlaying.setBinding([this]() {
68-
return this->bPlaybackState == MprisPlaybackState::Playing ? this->bCanPause.value()
69-
: this->bCanPlay.value();
68+
return this->bIsPlaying ? this->bCanPause.value() : this->bCanPlay.value();
7069
});
7170

7271
this->bTrackTitle.setBinding([this]() {
@@ -116,6 +115,10 @@ MprisPlayer::MprisPlayer(const QString& address, QObject* parent): QObject(paren
116115
}
117116
});
118117

118+
this->bIsPlaying.setBinding([this]() {
119+
return this->bPlaybackState == MprisPlaybackState::Playing;
120+
});
121+
119122
this->bLoopState.setBinding([this]() {
120123
const auto& status = this->bpLoopStatus.value();
121124

@@ -368,13 +371,18 @@ void MprisPlayer::pause() { this->setPlaybackState(MprisPlaybackState::Paused);
368371
void MprisPlayer::stop() { this->setPlaybackState(MprisPlaybackState::Stopped); }
369372

370373
void MprisPlayer::togglePlaying() {
371-
if (this->bPlaybackState == MprisPlaybackState::Playing) {
374+
if (this->bIsPlaying) {
372375
this->pause();
373376
} else {
374377
this->play();
375378
}
376379
}
377380

381+
void MprisPlayer::setPlaying(bool playing) {
382+
if (playing == this->bIsPlaying) return;
383+
this->togglePlaying();
384+
}
385+
378386
bool MprisPlayer::loopSupported() const { return this->pLoopStatus.exists(); }
379387

380388
void MprisPlayer::setLoopState(MprisLoopState::Enum loopState) {

src/services/mpris/player.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class MprisPlayer: public QObject {
154154
/// - If @@canControl is false, you cannot assign the `Stopped` state.
155155
/// (or any of the others, though their repsective properties will also be false)
156156
Q_PROPERTY(qs::service::mpris::MprisPlaybackState::Enum playbackState READ playbackState WRITE setPlaybackState NOTIFY playbackStateChanged BINDABLE bindablePlaybackState);
157+
/// True if @@playbackState == `MprisPlaybackState.Playing`.
158+
///
159+
/// Setting this property is equivalent to calling @@play() or @@pause().
160+
/// You cannot set this property if @@canTogglePlaying is false.
161+
Q_PROPERTY(bool isPlaying READ isPlaying WRITE setPlaying NOTIFY isPlayingChanged BINDABLE bindableIsPlaying);
157162
/// The loop state of the media player, or `None` if @@loopSupported is false.
158163
///
159164
/// May only be written to if @@canControl and @@loopSupported are true.
@@ -274,6 +279,9 @@ class MprisPlayer: public QObject {
274279

275280
void setPlaybackState(MprisPlaybackState::Enum playbackState);
276281

282+
QS_BINDABLE_GETTER(bool, bIsPlaying, isPlaying, bindableIsPlaying);
283+
void setPlaying(bool playing);
284+
277285
QS_BINDABLE_GETTER(MprisLoopState::Enum, bLoopState, loopState, bindableLoopState);
278286
[[nodiscard]] bool loopSupported() const;
279287
void setLoopState(MprisLoopState::Enum loopState);
@@ -351,6 +359,7 @@ class MprisPlayer: public QObject {
351359
void trackAlbumArtistChanged();
352360
void trackArtUrlChanged();
353361
void playbackStateChanged();
362+
void isPlayingChanged();
354363
void loopStateChanged();
355364
void loopSupportedChanged();
356365
void rateChanged();
@@ -412,6 +421,7 @@ private slots:
412421
Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, bool, bCanGoPrevious, &MprisPlayer::canGoPreviousChanged);
413422
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MprisPlayer, qreal, bVolume, 1, &MprisPlayer::volumeChanged);
414423
Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, MprisPlaybackState::Enum, bPlaybackState, &MprisPlayer::playbackStateChanged);
424+
Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, bool, bIsPlaying, &MprisPlayer::isPlayingChanged);
415425
QS_BINDING_SUBSCRIBE_METHOD(MprisPlayer, bPlaybackState, requestPositionUpdate, onValueChanged);
416426
Q_OBJECT_BINDABLE_PROPERTY(MprisPlayer, MprisLoopState::Enum, bLoopState, &MprisPlayer::loopStateChanged);
417427
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(MprisPlayer, qreal, bRate, 1, &MprisPlayer::rateChanged);

0 commit comments

Comments
 (0)