quickshell/modules/bar/Volume.qml

71 lines
2.0 KiB
QML
Raw Normal View History

import Quickshell.Io
2026-02-01 17:51:55 +01:00
import QtQuick.Layouts
import QtQuick
import Quickshell.Services.Pipewire
import qs.settings
import qs.reusables
import qs
2026-01-18 18:26:49 +01:00
Rectangle {
2026-02-01 17:51:55 +01:00
id: root
2026-01-18 18:26:49 +01:00
radius: implicitHeight / 2
2026-01-26 18:20:36 +01:00
color: pavuArea.containsMouse ? Colors.primaryContainer : Colors.surfaceContainer
2026-02-01 17:51:55 +01:00
implicitWidth: textRow.implicitWidth + 20
2026-02-06 14:13:29 +01:00
implicitHeight: Settings.config.barHeight - 10
2026-02-01 17:51:55 +01:00
property var sink: Pipewire.defaultAudioSink
function getVolumeIcon() {
// Safety check: if Pipewire is dead or sink is missing
if (!sink)
return "volume_off";
2026-01-16 14:44:10 +01:00
2026-02-01 17:51:55 +01:00
// If muted, show the hush icon
if (sink.audio.muted)
return "volume_off";
2026-01-16 14:44:10 +01:00
2026-02-01 17:51:55 +01:00
// Volume is usually 0.0 to 1.0 (0% to 100%)
const vol = sink.audio.volume;
2026-01-16 14:44:10 +01:00
2026-02-01 17:51:55 +01:00
if (vol <= 0.25)
return "volume_mute";
if (vol < 0.75)
return "volume_down";
if (vol <= 1.00)
2026-01-18 18:26:49 +01:00
return "volume_up";
2026-02-01 17:51:55 +01:00
// If it's loud, prepare the ears!
return "volume_up";
}
RowLayout {
id: textRow
spacing: 2
2026-02-01 17:51:55 +01:00
anchors.centerIn: parent
2026-02-03 23:59:00 +01:00
height: parent.height
2026-02-01 17:51:55 +01:00
CustomText {
id: volumeText
Layout.topMargin: 1
2026-02-01 17:51:55 +01:00
PwObjectTracker {
objects: Pipewire.ready ? Pipewire.defaultAudioSink : []
2026-01-18 18:26:49 +01:00
}
2026-02-01 17:51:55 +01:00
text: Pipewire.ready ? Math.round(root.sink.audio.volume * 100) + "%" : "failure"
opacity: Pipewire.ready ? root.sink.audio.muted ? 0.5 : 1 : 0
}
2026-02-01 17:51:55 +01:00
CustomIcon {
id: volumeIcon
opacity: Pipewire.ready ? root.sink.audio.muted ? 0.5 : 1 : 0
text: Pipewire.ready ? root.getVolumeIcon() : null
}
}
MouseArea {
id: pavuArea
Process {
id: pavuLauncher
command: ["sh", "-c", "pavucontrol"]
}
2026-02-01 17:51:55 +01:00
anchors.fill: parent
onClicked: pavuLauncher.exec(pavuLauncher.command)
acceptedButtons: Qt.LeftButton
cursorShape: Qt.PointingHandCursor
hoverEnabled: true
}
}