Files
NowPlaying/package/contents/ui/Player.qml

112 lines
4.1 KiB
QML

/**
Credits: https://github.com/ccatterina/plasmusic-toolbar
looked handy, ty
*/
import QtQml.Models
import QtQuick
import org.kde.plasma.private.mpris as Mpris
QtObject {
id: root
property string sourceName: "any"
property var mpris2Model
readonly property bool ready: {
if (!mpris2Model.currentPlayer)
return false;
return mpris2Model.currentPlayer.desktopEntry === sourceName || sourceName === "any";
}
readonly property string artists: ready ? mpris2Model.currentPlayer.artist : ""
readonly property string title: ready ? mpris2Model.currentPlayer.track : ""
readonly property int playbackStatus: ready ? mpris2Model.currentPlayer.playbackStatus : Mpris.PlaybackStatus.Unknown
readonly property int shuffle: ready ? mpris2Model.currentPlayer.shuffle : Mpris.ShuffleStatus.Unknown
readonly property string artUrl: ready ? mpris2Model.currentPlayer.artUrl : ""
readonly property int loopStatus: ready ? mpris2Model.currentPlayer.loopStatus : Mpris.LoopStatus.Unknown
readonly property double songPosition: ready ? mpris2Model.currentPlayer.position : 0
readonly property double songLength: ready ? mpris2Model.currentPlayer.length : 0
readonly property real volume: ready ? mpris2Model.currentPlayer.volume : 0
readonly property string identity: ready ? mpris2Model.currentPlayer.identity : ""
readonly property bool canGoNext: ready ? mpris2Model.currentPlayer.canGoNext : false
readonly property bool canGoPrevious: ready ? mpris2Model.currentPlayer.canGoPrevious : false
readonly property bool canPlay: ready ? mpris2Model.currentPlayer.canPlay : false
readonly property bool canPause: ready ? mpris2Model.currentPlayer.canPause : false
readonly property bool canSeek: ready ? mpris2Model.currentPlayer.canSeek : false
readonly property bool canRaise: ready ? mpris2Model.currentPlayer.canRaise : false
// To know whether Shuffle and Loop can be changed we have to check if the property is defined,
// unlike the other commands, LoopStatus and Shuffle do not have specific properties such as
// CanPause, CanSeek, etc.
readonly property bool canChangeShuffle: ready ? mpris2Model.currentPlayer.shuffle !== undefined : false
readonly property bool canChangeLoopStatus: ready ? mpris2Model.currentPlayer.loopStatus !== undefined : false
function togglePlayPause() {
if (mpris2Model.currentPlayer)
mpris2Model.currentPlayer.PlayPause();
}
function setPosition(position) {
mpris2Model.currentPlayer.position = position;
}
function next() {
if (mpris2Model.currentPlayer)
mpris2Model.currentPlayer.Next();
}
function previous() {
if (mpris2Model.currentPlayer)
mpris2Model.currentPlayer.Previous();
}
function updatePosition() {
if (mpris2Model.currentPlayer)
mpris2Model.currentPlayer.updatePosition();
}
function setVolume(volume) {
mpris2Model.currentPlayer.volume = volume;
}
function changeVolume(delta, showOSD) {
mpris2Model.currentPlayer.changeVolume(delta, showOSD);
}
function setShuffle(shuffle) {
mpris2Model.currentPlayer.shuffle = shuffle;
}
function setLoopStatus(loopStatus) {
mpris2Model.currentPlayer.loopStatus = loopStatus;
}
function raise() {
mpris2Model.currentPlayer.Raise();
}
function formatTrackTime(s) {
const hours = Math.floor(s / 3600);
let minutes = Math.floor((s - hours * 3600) / 60);
let seconds = Math.ceil(s - hours * 3600 - minutes * 60);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
return minutes + ":" + seconds;
}
mpris2Model: Mpris.Mpris2Model {
onRowsInserted: (_, rowIndex) => {
const CONTAINER_ROLE = Qt.UserRole + 1;
const player = this.data(this.index(rowIndex, 0), CONTAINER_ROLE);
if (player.desktopEntry === root.sourceName)
this.currentIndex = rowIndex;
}
}
}