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

104 lines
2.7 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
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: {
if(data['exit_code'] > 0)
return console.log('error lol');
batteryPercent = data['stdout'];
}
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.compactRepresentation: Item {
2024-01-19 18:45:11 +00:00
Layout.minimumWidth: units.iconSizes.large
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-19 18:45:11 +00:00
anchors.rightMargin: 15
anchors.topMargin: 2
anchors.bottomMargin: 2
color: "transparent"
border.color: PlasmaCore.Theme.textColor
radius: 4
2024-01-19 01:57:49 +00:00
Plasmoid.toolTipMainText: "battery level: " + batteryLevel()
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
color: batteryColor()
width: parent.width * Math.max(0, Math.min(batteryPercent, 100)) / 100
}
}
}
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
}
2024-01-19 18:45:11 +00:00
Rectangle { // symbol: headset
anchors.fill: parent
anchors.rightMargin: 23
color: "transparent"
Kirigami.Icon {
anchors.bottom: parent.bottom
anchors.left: parent.right
height: container.height * 0.45
source: "audio-headset-symbolic"
}
}
2024-01-17 19:06:16 +00:00
2024-01-19 18:45:11 +00:00
function batteryColor() {
let color = "red"
2024-01-17 19:45:08 +00:00
if(batteryPercent == -1) // charging
2024-01-19 18:45:11 +00:00
color = "green";
2024-01-17 19:06:16 +00:00
else if(batteryPercent == -2) // not connected
2024-01-19 18:45:11 +00:00
color = "red";
2024-01-17 19:06:16 +00:00
else if(batteryPercent > 80)
2024-01-19 18:45:11 +00:00
color = "green";
2024-01-17 19:06:16 +00:00
else if(batteryPercent > 60)
2024-01-19 18:45:11 +00:00
color = "green";
2024-01-17 19:06:16 +00:00
else if(batteryPercent > 40)
2024-01-19 18:45:11 +00:00
color = "yellow";
2024-01-17 19:06:16 +00:00
else if(batteryPercent > 20)
2024-01-19 18:45:11 +00:00
color = "red";
return color;
2024-01-17 19:06:16 +00:00
}
2024-01-19 01:57:49 +00:00
function batteryLevel() {
2024-01-17 19:06:16 +00:00
if(batteryPercent == -1)
return "charging";
else if(batteryPercent == -2)
return "not connected";
else
2024-01-19 01:57:49 +00:00
return batteryPercent + "%";
2024-01-17 19:06:16 +00:00
}
}
}