Using Quickshell Build in stuff for battery

This commit is contained in:
Honney
2026-08-21 22:07:56 +02:00
parent f11ddfb7fe
commit 2b4d3cd911
2 changed files with 27 additions and 96 deletions
+16 -80
View File
@@ -1,85 +1,21 @@
pragma Singleton pragma Singleton
import QtQuick import QtQuick
import Quickshell import Quickshell
import Quickshell.Io import Quickshell.Services.UPower
Item { Item {
property bool available: false property bool available: UPower.displayDevice.isLaptopBattery
property real percentage: 0 property real percentage: UPower.displayDevice.percentage*100
property bool charging: false property string state: UPowerDeviceState.toString(UPower.displayDevice.state)
property real energy: 0 property bool charging: state == "Charging"
property real voltage: 0 property bool full: state == "FullyCharged"
property int charge_cycle: 0 property real energy: UPower.displayDevice.energy
property real time_to_empty: 0 property real energy_formated: Math.round(energy*10)/10
property real energy_rate: 0 property real time_to_empty: UPower.displayDevice.timeToEmpty
property real hour_to_empty: Math.floor(time_to_empty/3600)
function parseUpower(line) { property real min_to_empty: Math.floor((time_to_empty%3600)/60)
line = line.trim() property real energy_rate: UPower.displayDevice.changeRate
if (line.startsWith("percentage:")) { property real energy_rate_formated: Math.round(energy_rate*10)/10
percentage = parseInt(line.split(":")[1]) property bool healthSupported: UPower.displayDevice.healthSupported
// console.log("percentage =", percentage) property real health: healthSupported ? UPower.displayDevice.healthPercentage : NaN
} }
else if (line.startsWith("state:")) {
charging = !line.includes("discharging")
// console.log("charging =", charging)
}
else if (line.startsWith("energy:")) {
energy = parseFloat(line.split(":")[1])
// console.log("energy =", energy)
}
else if (line.startsWith("voltage:")) {
voltage = parseFloat(line.split(":")[1])
// console.log("voltage =", voltage)
}
else if (line.startsWith("energy-rate:")) {
energy_rate = parseFloat(line.split(":")[1])
// console.log("energy_rate =", energy_rate)
}
else if (line.startsWith("charge-cycles:")) {
charge_cycle = parseInt(line.split(":")[1])
// console.log("charge_cycle =", charge_cycle)
}
else if (line.startsWith("time to empty:")) {
time_to_empty = parseFloat(line.split(":")[1])
// console.log("time_to_empty =", time_to_empty)
}
}
Process {
id: upowerProc
command: ["bash", "-c", "upower -i $(upower -e | grep '^/org/freedesktop/UPower/devices/battery')"]
stdout: SplitParser {
onRead: function(data) {
parseUpower(data)
}
}
}
Process {
id: batteryAvailable
command: ["sh", "-c", "upower -e | grep -q '^/org/freedesktop/UPower/devices/battery' && echo '1' || echo '0'"]
stdout: StdioCollector {
onStreamFinished: {
const hasBattery = this.text.trim() === "1"
available = hasBattery
if (hasBattery) {
pollTimer.start()
}
}
}
}
Timer {
id: pollTimer
interval: 1000
running: false
repeat: true
triggeredOnStart: false
onTriggered: upowerProc.running = true
}
Component.onCompleted: {
batteryAvailable.running = true
}
}
+11 -16
View File
@@ -1,7 +1,6 @@
import QtQuick import QtQuick
import Quickshell import Quickshell
import Quickshell.Hyprland import Quickshell.Hyprland
import Quickshell.Io
import "../../../config" import "../../../config"
import "../states/Battery" import "../states/Battery"
@@ -38,7 +37,7 @@ MouseArea {
font.family: Config.font font.family: Config.font
color: ( color: (
BatteryState.charging ? ( BatteryState.charging ? (
BatteryState.percentage == 100 ? Colors.secondary : BatteryState.full ? Colors.secondary :
Colors.tertiary Colors.tertiary
) : ) :
BatteryState.percentage <= 5 ? Colors.errorContainer : BatteryState.percentage <= 5 ? Colors.errorContainer :
@@ -90,7 +89,7 @@ MouseArea {
color: ( color: (
BatteryState.percentage <= 5 ? Colors.errorContainer : BatteryState.percentage <= 5 ? Colors.errorContainer :
BatteryState.charging ? ( BatteryState.charging ? (
BatteryState.percentage == 100 ? Colors.secondary : BatteryState.full ? Colors.secondary :
Colors.tertiary Colors.tertiary
) : ) :
BatteryState.percentage < 10 ? "yellow" : BatteryState.percentage < 10 ? "yellow" :
@@ -136,7 +135,7 @@ MouseArea {
font.family: Config.font font.family: Config.font
} }
Text { Text {
text: "charging: " + BatteryState.charging text: "State: " + BatteryState.state
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
color: Colors.primary color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@@ -144,7 +143,7 @@ MouseArea {
font.family: Config.font font.family: Config.font
} }
Text { Text {
text: "energy: " + BatteryState.energy text: "energy: " + Math.round(BatteryState.energy*10)/10 + " W"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
color: Colors.primary color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@@ -152,7 +151,10 @@ MouseArea {
font.family: Config.font font.family: Config.font
} }
Text { Text {
text: "voltage: " + BatteryState.voltage text: (
BatteryState.hour_to_empty > 0 ? "Remaining: " + BatteryState.hour_to_empty + "h " + BatteryState.min_to_empty + "min" :
BatteryState.min_to_empty > 0 ? "Remaining: " + BatteryState.min_to_empty + "min" :
"NaN")
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
color: Colors.primary color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@@ -160,7 +162,7 @@ MouseArea {
font.family: Config.font font.family: Config.font
} }
Text { Text {
text: "charge_cycle: " + BatteryState.charge_cycle text: "Usage: " + BatteryState.energy_rate_formated + " W"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
color: Colors.primary color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@@ -168,15 +170,8 @@ MouseArea {
font.family: Config.font font.family: Config.font
} }
Text { Text {
text: "time_to_empty: " + BatteryState.time_to_empty + "h" text: "Health: " + BatteryState.health + " %"
wrapMode: Text.WordWrap visible: BatteryState.healthSupported
color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: Config.font_size_tiny(bar.height)
font.family: Config.font
}
Text {
text: "energy_rate: " + BatteryState.energy_rate + "W"
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
color: Colors.primary color: Colors.primary
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter