Files
glassMxrph/quickshell/bar/modules/CavaModule.qml
T

86 lines
2.8 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import "../../components"
import "../../core"
import "../../services"
import "../../theme"
// Cava audio visualizer in the bar. Click opens the dashboard's Media tab,
// right-click the Bar settings (its options sit on the module cog). Cava only runs while something plays.
GlassPill {
id: root
objectName: "cavaPill"
readonly property var options: ConfigService.ready ? ConfigService.data.bar.cava : ({
bars: 12,
style: "bars",
hideWhenIdle: true
})
readonly property bool mirror: options.style === "mirror"
readonly property bool shown: !CavaService.failed && (MediaService.playing || !options.hideWhenIdle)
readonly property int barWidth: 3
readonly property int gap: 2
implicitHeight: ModuleRegistry.pillHeight
implicitWidth: shown ? options.bars * barWidth + (options.bars - 1) * gap + 22 : 0
visible: implicitWidth > 0.5
opacity: shown ? 1 : 0
clip: true
Behavior on implicitWidth {
NumberAnimation {
duration: Theme.duration * 1.4
easing.type: Easing.OutCubic
}
}
Behavior on opacity {
NumberAnimation {
duration: Theme.duration
}
}
Demand {
service: CavaService
counter: "widgetConsumers"
}
// Cava emits 24 bands; each pill bar averages its share of them.
function level(index) {
const all = CavaService.bars, n = options.bars, from = Math.floor(index * all.length / n), to = Math.max(from + 1, Math.floor((index + 1) * all.length / n));
let sum = 0;
for (let i = from; i < to; i++)
sum += all[i] || 0;
return sum / (to - from);
}
Row {
anchors.centerIn: parent
height: parent.height - 12
spacing: root.gap
Repeater {
model: root.options.bars
Rectangle {
required property int index
readonly property real value: MediaService.playing ? Math.min(1, root.level(index)) : 0
width: root.barWidth
radius: width / 2
height: Math.max(width, parent.height * value)
y: root.mirror ? (parent.height - height) / 2 : parent.height - height
color: Theme.accentVivid
opacity: 0.55 + 0.45 * value
Behavior on height {
NumberAnimation {
duration: 80
}
}
}
}
}
TapHandler {
acceptedButtons: Qt.LeftButton
onTapped: ShellState.toggleDashboard("media")
}
TapHandler {
acceptedButtons: Qt.RightButton
onTapped: ShellState.openSettings("bar")
}
HoverHandler {
cursorShape: Qt.PointingHandCursor
}
Accessible.name: "Audio visualizer"
}