Added battery health and made it configurable

This commit is contained in:
EinBowser
2025-11-01 20:50:39 +01:00
parent daf7d2c9dc
commit b267b74e8b
4 changed files with 58 additions and 0 deletions
@@ -63,6 +63,29 @@ StyledPopup {
text: Battery.isCharging ? Translation.tr("Time to full:") : Translation.tr("Time to empty:")
color: Appearance.colors.colOnSurfaceVariant
}
RowLayout {
spacing: 5
visible: Config.options.battery.showHealth
Layout.fillWidth: true
MaterialSymbol {
text: "healing"
color: Appearance.colors.colOnSurfaceVariant
iconSize: Appearance.font.pixelSize.large
}
StyledText {
text: Translation.tr("Health:")
color: Appearance.colors.colOnSurfaceVariant
}
StyledText {
Layout.fillWidth: true
horizontalAlignment: Text.AlignRight
color: Appearance.colors.colOnSurfaceVariant
text: `${(Battery.health).toFixed(1)}%`
}
}
StyledText {
Layout.fillWidth: true
horizontalAlignment: Text.AlignRight
@@ -257,6 +257,7 @@ Singleton {
property int full: 101
property bool automaticSuspend: true
property int suspend: 3
property bool showHealth: true
}
property JsonObject conflictKiller: JsonObject {
@@ -0,0 +1,11 @@
#!/bin/bash
# This just guesses that the BAT0 is the actuall battery
full=$(cat /sys/class/power_supply/BAT0/charge_full)
design=$(cat /sys/class/power_supply/BAT0/charge_full_design)
if [ "$design" -gt 0 ]; then # basically if design > 0
awk "BEGIN { printf \"%.1f\n\", ($full/$design)*100 }"
else
echo "Error"
fi
@@ -31,6 +31,29 @@ Singleton {
property real timeToEmpty: UPower.displayDevice.timeToEmpty
property real timeToFull: UPower.displayDevice.timeToFull
property real health: 0
Process {
id: batteryProcess
running: Config.options.battery.showHealth
command: [
"bash",
`${FileUtils.trimFileProtocol(Directories.scriptPath)}/battery/calculate-health.sh`
]
stdout: StdioCollector {
onStreamFinished: {
const output = text.trim()
const value = Number(output)
if (!isNaN(value)) {
root.health = value
console.log("Battery health:", value)
} else {
console.warn("Battery script output invalid:", output)
}
}
}
}
onIsLowAndNotChargingChanged: {
if (!root.available || !isLowAndNotCharging) return;
Quickshell.execDetached([