Very minimal volume widget and PWAudioService

This commit is contained in:
quadbyte 2025-08-10 18:28:36 -04:00
parent 56777fc9fd
commit 5c7268aaee
7 changed files with 326 additions and 234 deletions

View file

@ -1,161 +1,162 @@
pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Services.Mpris
import qs.Services
import qs.Modules.Audio
Singleton {
id: root
// property var currentPlayer: null
// property real currentPosition: 0
// property int selectedPlayerIndex: 0
// property bool isPlaying: currentPlayer ? currentPlayer.isPlaying : false
// property string trackTitle: currentPlayer ? (currentPlayer.trackTitle || "Unknown Track") : ""
// property string trackArtist: currentPlayer ? (currentPlayer.trackArtist || "Unknown Artist") : ""
// property string trackAlbum: currentPlayer ? (currentPlayer.trackAlbum || "Unknown Album") : ""
// property string trackArtUrl: currentPlayer ? (currentPlayer.trackArtUrl || "") : ""
// property real trackLength: currentPlayer ? currentPlayer.length : 0
// property bool canPlay: currentPlayer ? currentPlayer.canPlay : false
// property bool canPause: currentPlayer ? currentPlayer.canPause : false
// property bool canGoNext: currentPlayer ? currentPlayer.canGoNext : false
// property bool canGoPrevious: currentPlayer ? currentPlayer.canGoPrevious : false
// property bool canSeek: currentPlayer ? currentPlayer.canSeek : false
// property bool hasPlayer: getAvailablePlayers().length > 0
property var currentPlayer: null
property real currentPosition: 0
property int selectedPlayerIndex: 0
property bool isPlaying: currentPlayer ? currentPlayer.isPlaying : false
property string trackTitle: currentPlayer ? (currentPlayer.trackTitle || "Unknown Track") : ""
property string trackArtist: currentPlayer ? (currentPlayer.trackArtist || "Unknown Artist") : ""
property string trackAlbum: currentPlayer ? (currentPlayer.trackAlbum || "Unknown Album") : ""
property string trackArtUrl: currentPlayer ? (currentPlayer.trackArtUrl || "") : ""
property real trackLength: currentPlayer ? currentPlayer.length : 0
property bool canPlay: currentPlayer ? currentPlayer.canPlay : false
property bool canPause: currentPlayer ? currentPlayer.canPause : false
property bool canGoNext: currentPlayer ? currentPlayer.canGoNext : false
property bool canGoPrevious: currentPlayer ? currentPlayer.canGoPrevious : false
property bool canSeek: currentPlayer ? currentPlayer.canSeek : false
property bool hasPlayer: getAvailablePlayers().length > 0
// Item {
// Component.onCompleted: {
// updateCurrentPlayer()
// }
// }
// Expose cava values
property alias cavaValues: cava.values
// function getAvailablePlayers() {
// if (!Mpris.players || !Mpris.players.values) {
// return []
// }
Item {
Component.onCompleted: {
updateCurrentPlayer()
}
}
// let allPlayers = Mpris.players.values
// let controllablePlayers = []
function getAvailablePlayers() {
if (!Mpris.players || !Mpris.players.values) {
return []
}
// for (var i = 0; i < allPlayers.length; i++) {
// let player = allPlayers[i]
// if (player && player.canControl) {
// controllablePlayers.push(player)
// }
// }
let allPlayers = Mpris.players.values
let controllablePlayers = []
// return controllablePlayers
// }
for (var i = 0; i < allPlayers.length; i++) {
let player = allPlayers[i]
if (player && player.canControl) {
controllablePlayers.push(player)
}
}
// function findActivePlayer() {
// let availablePlayers = getAvailablePlayers()
// if (availablePlayers.length === 0) {
// return null
// }
return controllablePlayers
}
// if (selectedPlayerIndex < availablePlayers.length) {
// return availablePlayers[selectedPlayerIndex]
// } else {
// selectedPlayerIndex = 0
// return availablePlayers[0]
// }
// }
function findActivePlayer() {
let availablePlayers = getAvailablePlayers()
if (availablePlayers.length === 0) {
return null
}
// // Switch to the most recently active player
// function updateCurrentPlayer() {
// let newPlayer = findActivePlayer()
// if (newPlayer !== currentPlayer) {
// currentPlayer = newPlayer
// currentPosition = currentPlayer ? currentPlayer.position : 0
// }
// }
if (selectedPlayerIndex < availablePlayers.length) {
return availablePlayers[selectedPlayerIndex]
} else {
selectedPlayerIndex = 0
return availablePlayers[0]
}
}
// function playPause() {
// if (currentPlayer) {
// if (currentPlayer.isPlaying) {
// currentPlayer.pause()
// } else {
// currentPlayer.play()
// }
// }
// }
// Switch to the most recently active player
function updateCurrentPlayer() {
let newPlayer = findActivePlayer()
if (newPlayer !== currentPlayer) {
currentPlayer = newPlayer
currentPosition = currentPlayer ? currentPlayer.position : 0
}
}
// function play() {
// if (currentPlayer && currentPlayer.canPlay) {
// currentPlayer.play()
// }
// }
function playPause() {
if (currentPlayer) {
if (currentPlayer.isPlaying) {
currentPlayer.pause()
} else {
currentPlayer.play()
}
}
}
// function pause() {
// if (currentPlayer && currentPlayer.canPause) {
// currentPlayer.pause()
// }
// }
function play() {
if (currentPlayer && currentPlayer.canPlay) {
currentPlayer.play()
}
}
// function next() {
// if (currentPlayer && currentPlayer.canGoNext) {
// currentPlayer.next()
// }
// }
function pause() {
if (currentPlayer && currentPlayer.canPause) {
currentPlayer.pause()
}
}
// function previous() {
// if (currentPlayer && currentPlayer.canGoPrevious) {
// currentPlayer.previous()
// }
// }
function next() {
if (currentPlayer && currentPlayer.canGoNext) {
currentPlayer.next()
}
}
// function seek(position) {
// if (currentPlayer && currentPlayer.canSeek) {
// currentPlayer.position = position
// currentPosition = position
// }
// }
function previous() {
if (currentPlayer && currentPlayer.canGoPrevious) {
currentPlayer.previous()
}
}
// // Seek to position based on ratio (0.0 to 1.0)
// function seekByRatio(ratio) {
// if (currentPlayer && currentPlayer.canSeek && currentPlayer.length > 0) {
// let seekPosition = ratio * currentPlayer.length
// currentPlayer.position = seekPosition
// currentPosition = seekPosition
// }
// }
function seek(position) {
if (currentPlayer && currentPlayer.canSeek) {
currentPlayer.position = position
currentPosition = position
}
}
// // Update progress bar every second while playing
// Timer {
// id: positionTimer
// interval: 1000
// running: currentPlayer && currentPlayer.isPlaying && currentPlayer.length > 0
// && currentPlayer.playbackState === MprisPlaybackState.Playing
// repeat: true
// onTriggered: {
// if (currentPlayer && currentPlayer.isPlaying && currentPlayer.playbackState === MprisPlaybackState.Playing) {
// currentPosition = currentPlayer.position
// } else {
// running = false
// }
// }
// }
// Seek to position based on ratio (0.0 to 1.0)
function seekByRatio(ratio) {
if (currentPlayer && currentPlayer.canSeek && currentPlayer.length > 0) {
let seekPosition = ratio * currentPlayer.length
currentPlayer.position = seekPosition
currentPosition = seekPosition
}
}
// // Reset position when switching to inactive player
// onCurrentPlayerChanged: {
// if (!currentPlayer || !currentPlayer.isPlaying || currentPlayer.playbackState !== MprisPlaybackState.Playing) {
// currentPosition = 0
// }
// }
// Update progress bar every second while playing
Timer {
id: positionTimer
interval: 1000
running: currentPlayer && currentPlayer.isPlaying && currentPlayer.length > 0
&& currentPlayer.playbackState === MprisPlaybackState.Playing
repeat: true
onTriggered: {
if (currentPlayer && currentPlayer.isPlaying && currentPlayer.playbackState === MprisPlaybackState.Playing) {
currentPosition = currentPlayer.position
} else {
running = false
}
}
}
// // Update current player when available players change
// Connections {
// target: Mpris.players
// function onValuesChanged() {
// updateCurrentPlayer()
// }
// }
// Reset position when switching to inactive player
onCurrentPlayerChanged: {
if (!currentPlayer || !currentPlayer.isPlaying || currentPlayer.playbackState !== MprisPlaybackState.Playing) {
currentPosition = 0
}
}
// Cava {
// id: cava
// count: 44
// }
// Update current player when available players change
Connections {
target: Mpris.players
function onValuesChanged() {
updateCurrentPlayer()
}
}
// // Expose cava values
// property alias cavaValues: cava.values
Cava {
id: cava
}
}

View file

@ -0,0 +1,30 @@
pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Pipewire
Singleton {
id: root
// Ensure the volume is readonly from outside
readonly property alias volume: root._volume
property real _volume: 0
readonly property alias muted: root._muted
property bool _muted: false
PwObjectTracker {
objects: [Pipewire.defaultAudioSink]
}
Connections {
target: Pipewire.defaultAudioSink?.audio ? Pipewire.defaultAudioSink?.audio : null
function onVolumeChanged() {
root._volume = (Pipewire.defaultAudioSink?.audio.volume ?? 0)
console.log("onVolumeChanged: " + volume)
}
}
}