quickshell/modules/bar/Volume.qml
2025-12-28 17:36:11 +01:00

86 lines
2.4 KiB
QML

import QtQuick
import Quickshell.Services.Pipewire
import Quickshell.Widgets
import QtQuick.Layouts
import Quickshell.Io
import "../../"
import "../settings/"
Item {
id: root
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: mouse => {
if (mouse.button === Qt.LeftButton) {
pavu.startDetached();
}
}
}
implicitWidth: styleLayout.implicitWidth + 10
implicitHeight: 34
property var sink: Pipewire.defaultAudioSink
Process {
id: pavu
command: ["pavucontrol"] // The command and args list
}
// Logic to pick the correct icon name
function getVolumeIcon() {
// Safety check: if Pipewire is dead or sink is missing
if (!sink)
return "audio-volume-muted-blocking";
// If muted, show the hush icon
if (sink.audio.muted)
return "audio-volume-muted";
// Volume is usually 0.0 to 1.0 (0% to 100%)
const vol = sink.audio.volume;
if (vol <= 0.0)
return "audio-volume-low";
if (vol < 0.33)
return "audio-volume-low";
if (vol < 0.66)
return "audio-volume-medium";
// If it's loud, prepare the ears!
return "audio-volume-high";
}
ColumnLayout {
id: styleLayout
anchors.centerIn: parent
spacing: 0
Row {
spacing: 10
Text {
PwObjectTracker {
objects: Pipewire.ready ? root.sink : []
}
font.weight: 900
color: Colors.foreground
font.family: Settings.font
font.pixelSize: Settings.fontSize
text: Pipewire.ready ? Math.round(root.sink.audio.volume * 100) + "%" : "0%"
}
IconImage {
anchors.verticalCenter: parent.verticalCenter
width: 12
height: 12
source: "root:/icons/" + root.getVolumeIcon() + "-symbolic.svg"
}
}
Text {
font.weight: 900
color: Colors.foreground
font.family: Settings.font
font.pixelSize: Settings.fontSize - 2
opacity: 0.7
text: Pipewire.ready ? Pipewire.defaultAudioSink.nickname : "failure"
}
}
}