service/mpris: add shorthand for playback state changes

This commit is contained in:
outfoxxed 2024-06-21 16:31:02 -07:00
parent b6612bd56c
commit c56a3ec966
Signed by: outfoxxed
GPG Key ID: 4C88A185FB89301E
2 changed files with 38 additions and 0 deletions

View File

@ -71,6 +71,10 @@ MprisPlayer::MprisPlayer(const QString& address, QObject* parent): QObject(paren
QObject::connect(&this->pCanGoPrevious, &AbstractDBusProperty::changed, this, &MprisPlayer::canGoPreviousChanged);
QObject::connect(&this->pCanPlay, &AbstractDBusProperty::changed, this, &MprisPlayer::canPlayChanged);
QObject::connect(&this->pCanPause, &AbstractDBusProperty::changed, this, &MprisPlayer::canPauseChanged);
QObject::connect(&this->pCanPlay, &AbstractDBusProperty::changed, this, &MprisPlayer::canTogglePlayingChanged);
QObject::connect(&this->pCanPause, &AbstractDBusProperty::changed, this, &MprisPlayer::canTogglePlayingChanged);
QObject::connect(&this->pPosition, &AbstractDBusProperty::changed, this, &MprisPlayer::onPositionChanged);
QObject::connect(this->player, &DBusMprisPlayer::Seeked, this, &MprisPlayer::onSeek);
QObject::connect(&this->pVolume, &AbstractDBusProperty::changed, this, &MprisPlayer::volumeChanged);
@ -148,6 +152,12 @@ QString MprisPlayer::address() const { return this->player->service(); }
bool MprisPlayer::canControl() const { return this->pCanControl.get(); }
bool MprisPlayer::canPlay() const { return this->canControl() && this->pCanPlay.get(); }
bool MprisPlayer::canPause() const { return this->canControl() && this->pCanPause.get(); }
bool MprisPlayer::canTogglePlaying() const {
if (this->mPlaybackState == MprisPlaybackState::Playing) return this->canPlay();
else return this->canPause();
}
bool MprisPlayer::canSeek() const { return this->canControl() && this->pCanSeek.get(); }
bool MprisPlayer::canGoNext() const { return this->canControl() && this->pCanGoNext.get(); }
bool MprisPlayer::canGoPrevious() const { return this->canControl() && this->pCanGoPrevious.get(); }
@ -327,6 +337,20 @@ void MprisPlayer::setPlaybackState(MprisPlaybackState::Enum playbackState) {
}
}
void MprisPlayer::play() { this->setPlaybackState(MprisPlaybackState::Playing); }
void MprisPlayer::pause() { this->setPlaybackState(MprisPlaybackState::Paused); }
void MprisPlayer::stop() { this->setPlaybackState(MprisPlaybackState::Stopped); }
void MprisPlayer::togglePlaying() {
if (this->mPlaybackState == MprisPlaybackState::Playing) {
this->pause();
} else {
this->play();
}
}
void MprisPlayer::onPlaybackStatusChanged() {
const auto& status = this->pPlaybackStatus.get();

View File

@ -60,6 +60,7 @@ class MprisPlayer: public QObject {
Q_PROPERTY(bool canControl READ canControl NOTIFY canControlChanged);
Q_PROPERTY(bool canPlay READ canPlay NOTIFY canPlayChanged);
Q_PROPERTY(bool canPause READ canPause NOTIFY canPauseChanged);
Q_PROPERTY(bool canTogglePlaying READ canTogglePlaying NOTIFY canTogglePlayingChanged);
Q_PROPERTY(bool canSeek READ canSeek NOTIFY canSeekChanged);
Q_PROPERTY(bool canGoNext READ canGoNext NOTIFY canGoNextChanged);
Q_PROPERTY(bool canGoPrevious READ canGoPrevious NOTIFY canGoPreviousChanged);
@ -191,6 +192,17 @@ public:
///
/// May only be called if `canSeek` is true.
Q_INVOKABLE void seek(qreal offset);
/// Equivalent to setting `playbackState` to `Playing`.
Q_INVOKABLE void play();
/// Equivalent to setting `playbackState` to `Paused`.
Q_INVOKABLE void pause();
/// Equivalent to setting `playbackState` to `Stopped`.
Q_INVOKABLE void stop();
/// Equivalent to calling `play()` if not playing or `pause()` if playing.
///
/// May only be called if `canTogglePlaying` is true, which is equivalent to
/// `canPlay` or `canPause` depending on the current playback state.
Q_INVOKABLE void togglePlaying();
[[nodiscard]] bool isValid() const;
[[nodiscard]] QString address() const;
@ -201,6 +213,7 @@ public:
[[nodiscard]] bool canGoPrevious() const;
[[nodiscard]] bool canPlay() const;
[[nodiscard]] bool canPause() const;
[[nodiscard]] bool canTogglePlaying() const;
[[nodiscard]] bool canQuit() const;
[[nodiscard]] bool canRaise() const;
[[nodiscard]] bool canSetFullscreen() const;
@ -251,6 +264,7 @@ signals:
void canControlChanged();
void canPlayChanged();
void canPauseChanged();
void canTogglePlayingChanged();
void canSeekChanged();
void canGoNextChanged();
void canGoPreviousChanged();