diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-07-11 20:55:23 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-07-11 20:55:23 +0800 |
| commit | bf44811dced54246317c51183b2c77b551fafb79 (patch) | |
| tree | ee873254f93a5992b39df0932a581e1564bd153b /bar/Mpris.qml | |
| parent | 7cdb76f8310ffba13ea6dbd02416c76ac81f9add (diff) | |
mpris
Diffstat (limited to 'bar/Mpris.qml')
| -rw-r--r-- | bar/Mpris.qml | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/bar/Mpris.qml b/bar/Mpris.qml new file mode 100644 index 0000000..880456b --- /dev/null +++ b/bar/Mpris.qml @@ -0,0 +1,57 @@ +import QtQuick.Layouts +import QtQuick +import Quickshell +import Quickshell.Services.Mpris +import qs.services + +// import Quickshell.Utils + +Item { + id: root + implicitWidth: parent.width + height: Bar.height + readonly property MprisPlayer activePlayer: SMpris.activePlayer + readonly property string cleanedTitle: cleanMusicTitle(activePlayer?.trackTitle) || qsTr("No media") + // length broken (https://github.com/quickshell-mirror/quickshell/issues/109) + readonly property string position: friendlyTimeForSeconds(activePlayer?.position) || "0:00" + readonly property string length: friendlyTimeForSeconds(activePlayer?.length) || "0:00" + readonly property string progress: activePlayer.positionSupported && position != length ? position + "/" + length : activePlayer.positionSupported && !activePlayer.lengthSupported ? !activePlayer.positionSupported && activePlayer.lengthSupported ? "" : position : activePlayer.lengthSupported ? length : "" + readonly property string state: activePlayer?.isPlaying ? "Playing" : "Paused" + + function cleanMusicTitle(title: string): string { + if (!title) + return ""; + title = title.replace(/\s*\([^)]*\)/g, " "); // Round brackets + title = title.replace(/\s*\[[^\]]*\]/g, " "); // Square brackets + title = title.replace(/\s*\{[^\}]*\}/g, " "); // Curly brackets + title = title.replace(/\s*【[^】]*】/g, " "); // Touhou + title = title.replace(/\s*《[^》]*》/g, " "); + title = title.replace(/\s*「[^」]*」/g, " "); + title = title.replace(/\s*『[^』]*』/g, " "); + + return title.trim(); + } + + function friendlyTimeForSeconds(seconds) { + if (isNaN(seconds) || seconds < 0) + return "0:00"; + seconds = Math.floor(seconds); + const h = Math.floor(seconds / 3600); + const m = Math.floor((seconds % 3600) / 60); + const s = seconds % 60; + if (h > 0) { + return `${h}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`; + } else { + return `${m}:${s.toString().padStart(2, '0')}`; + } + } + + Text { + anchors.centerIn: parent + text: root.state + " " + root.cleanedTitle + " [" + root.progress + "]" + } + // hover + // Text { + // text: root.activePlayer.trackTitle + " by " + root.activePlayer.trackArtist + " - " + root.activePlayer.trackAlbum + root.activePlayer.position + "/" + root.activePlayer.length + // } +} |
