57 lines
1.7 KiB
QML
57 lines
1.7 KiB
QML
// BatteryWidget.qml - battery icon + percentage
|
|
import QtQuick
|
|
import Quickshell.Io
|
|
import ".."
|
|
|
|
Pill {
|
|
id: root
|
|
property int capacity: 100
|
|
property string status: "Unknown"
|
|
property bool charging: status === "Charging"
|
|
property bool plugged: status === "Full" || status === "Not charging"
|
|
property bool critical: capacity <= 15 && !charging
|
|
|
|
property string icon: {
|
|
if (charging) return " "
|
|
if (plugged) return " "
|
|
if (capacity > 80) return ""
|
|
if (capacity > 60) return ""
|
|
if (capacity > 40) return ""
|
|
if (capacity > 20) return ""
|
|
return ""
|
|
}
|
|
|
|
Text {
|
|
text: root.icon + " " + root.capacity + "%"
|
|
font.family: Theme.fontSans
|
|
font.pixelSize: Theme.fontSize
|
|
color: root.critical ? Theme.color1 : Theme.foreground
|
|
}
|
|
|
|
Process {
|
|
id: batProc
|
|
running: false
|
|
command: ["bash", "-c",
|
|
"cat /sys/class/power_supply/BAT0/capacity 2>/dev/null; " +
|
|
"echo ---; " +
|
|
"cat /sys/class/power_supply/BAT0/status 2>/dev/null"]
|
|
stdout: StdioCollector {
|
|
onStreamFinished: {
|
|
const parts = this.text.split("---")
|
|
if (parts.length >= 2) {
|
|
const cap = parseInt(parts[0].trim())
|
|
if (!isNaN(cap)) root.capacity = cap
|
|
root.status = parts[1].trim()
|
|
}
|
|
}
|
|
}
|
|
onExited: batProc.running = false
|
|
}
|
|
|
|
Timer {
|
|
interval: 5000; running: true; repeat: true
|
|
triggeredOnStart: true
|
|
onTriggered: batProc.running = true
|
|
}
|
|
}
|