headsetcontrol-battery-widget/package/contents/ui/main.qml
2024-01-17 20:06:16 +01:00

66 lines
1.6 KiB
QML

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
Item {
id: main
property int batteryPercent: -2
PlasmaCore.DataSource {
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -bc"]
interval: 2000
onNewData: {
if(data['exit_code'] > 0)
return console.log('error lol');
batteryPercent = data['stdout'];
}
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.compactRepresentation: Item {
Layout.minimumWidth: units.iconSizes.medium
Image {
anchors.fill: parent
smooth: true
fillMode: Image.PreserveAspectFit
source: batteryIcon()
Plasmoid.toolTipMainText: "battery status:"
Plasmoid.toolTipSubText: batteryStatus()
}
function batteryIcon() {
let iconName = "battery_100"
if(batteryPercent == -1) // charing
iconName = "battery_charging"
else if(batteryPercent == -2) // not connected
iconName = "battery_nc";
else if(batteryPercent > 80)
iconName = "battery_100";
else if(batteryPercent > 60)
iconName = "battery_80";
else if(batteryPercent > 40)
iconName = "battery_60";
else if(batteryPercent > 20)
iconName = "battery_40";
else
iconName = "battery_20";
return "../icons/" + iconName + ".png";
}
function batteryStatus() {
if(batteryPercent == -1)
return "charging";
else if(batteryPercent == -2)
return "not connected";
else
return batteryPercent + " %";
}
}
}