headsetcontrol-battery-widget/package/contents/ui/main.qml

78 lines
2.1 KiB
QML
Raw Normal View History

2024-01-17 19:06:16 +00:00
import QtQuick 2.0
import QtQuick.Layouts 1.3
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
2024-01-19 18:45:11 +00:00
import org.kde.kirigami 2.14 as Kirigami
2024-01-17 19:06:16 +00:00
2024-01-20 02:03:58 +00:00
import "./lib/helper.js" as Helper
2024-01-17 19:06:16 +00:00
Item {
id: main
2024-01-19 00:54:33 +00:00
property int pollingrate: Plasmoid.configuration.pollingrate
2024-01-17 19:06:16 +00:00
property int batteryPercent: -2
PlasmaCore.DataSource {
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -bc"]
2024-01-19 00:54:33 +00:00
interval: pollingrate * 1e3
2024-01-17 19:06:16 +00:00
onNewData: {
2024-01-20 02:20:23 +00:00
if(data['exit code'] > 0) {
2024-01-20 02:03:42 +00:00
batteryPercent = -2;
return;
}
2024-01-17 19:06:16 +00:00
batteryPercent = data['stdout'];
}
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.compactRepresentation: Item {
2024-01-20 02:03:42 +00:00
Layout.minimumWidth: units.iconSizes.medium
2024-01-19 18:45:11 +00:00
Layout.alignment: Qt.AlignHCenter
2024-01-17 19:06:16 +00:00
2024-01-19 18:45:11 +00:00
Rectangle { // battery
id: container
2024-01-17 19:06:16 +00:00
anchors.fill: parent
2024-01-20 02:03:42 +00:00
anchors.rightMargin: 2
2024-01-19 18:45:11 +00:00
anchors.topMargin: 2
anchors.bottomMargin: 2
color: "transparent"
border.color: PlasmaCore.Theme.textColor
radius: 4
2024-01-20 02:03:58 +00:00
Plasmoid.toolTipMainText: "battery level: " + Helper.batteryLevel(batteryPercent)
2024-01-19 01:57:49 +00:00
Plasmoid.toolTipSubText: "polling rate: " + pollingrate + " seconds"
2024-01-19 18:45:11 +00:00
Item {
anchors.fill: parent
anchors.margins: 2
Rectangle { // battery fill
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
2024-01-20 02:03:58 +00:00
color: Helper.batteryColor(batteryPercent)
2024-01-19 18:45:11 +00:00
width: parent.width * Math.max(0, Math.min(batteryPercent, 100)) / 100
}
2024-01-20 02:03:42 +00:00
Kirigami.Icon { // headphones icon
anchors.bottom: parent.bottom
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 1
anchors.bottomMargin: 1
source: "audio-headphones-symbolic"
}
2024-01-19 18:45:11 +00:00
}
}
Rectangle { // battery cap
anchors.left: container.right
anchors.leftMargin: 1
anchors.verticalCenter: parent.verticalCenter
radius: 4
height: parent.height / 3
width: 2
color: PlasmaCore.Theme.textColor
2024-01-17 19:06:16 +00:00
}
}
}