From 2b4d3cd911d96c6a559bc586384df9c992cd9967 Mon Sep 17 00:00:00 2001 From: Honney Date: Fri, 21 Aug 2026 22:07:56 +0200 Subject: [PATCH] Using Quickshell Build in stuff for battery --- modules/bar/states/Battery/BatteryState.qml | 96 ++++----------------- modules/bar/widgets/Battery.qml | 27 +++--- 2 files changed, 27 insertions(+), 96 deletions(-) diff --git a/modules/bar/states/Battery/BatteryState.qml b/modules/bar/states/Battery/BatteryState.qml index fe1aae5..cd68678 100644 --- a/modules/bar/states/Battery/BatteryState.qml +++ b/modules/bar/states/Battery/BatteryState.qml @@ -1,85 +1,21 @@ pragma Singleton import QtQuick import Quickshell -import Quickshell.Io +import Quickshell.Services.UPower Item { - property bool available: false - property real percentage: 0 - property bool charging: false - property real energy: 0 - property real voltage: 0 - property int charge_cycle: 0 - property real time_to_empty: 0 - property real energy_rate: 0 - - function parseUpower(line) { - line = line.trim() - if (line.startsWith("percentage:")) { - percentage = parseInt(line.split(":")[1]) - // console.log("percentage =", percentage) - } - 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 - } -} \ No newline at end of file + property bool available: UPower.displayDevice.isLaptopBattery + property real percentage: UPower.displayDevice.percentage*100 + property string state: UPowerDeviceState.toString(UPower.displayDevice.state) + property bool charging: state == "Charging" + property bool full: state == "FullyCharged" + property real energy: UPower.displayDevice.energy + property real energy_formated: Math.round(energy*10)/10 + property real time_to_empty: UPower.displayDevice.timeToEmpty + property real hour_to_empty: Math.floor(time_to_empty/3600) + property real min_to_empty: Math.floor((time_to_empty%3600)/60) + property real energy_rate: UPower.displayDevice.changeRate + property real energy_rate_formated: Math.round(energy_rate*10)/10 + property bool healthSupported: UPower.displayDevice.healthSupported + property real health: healthSupported ? UPower.displayDevice.healthPercentage : NaN +} diff --git a/modules/bar/widgets/Battery.qml b/modules/bar/widgets/Battery.qml index de39971..9cf6af4 100644 --- a/modules/bar/widgets/Battery.qml +++ b/modules/bar/widgets/Battery.qml @@ -1,7 +1,6 @@ import QtQuick import Quickshell import Quickshell.Hyprland -import Quickshell.Io import "../../../config" import "../states/Battery" @@ -38,7 +37,7 @@ MouseArea { font.family: Config.font color: ( BatteryState.charging ? ( - BatteryState.percentage == 100 ? Colors.secondary : + BatteryState.full ? Colors.secondary : Colors.tertiary ) : BatteryState.percentage <= 5 ? Colors.errorContainer : @@ -90,7 +89,7 @@ MouseArea { color: ( BatteryState.percentage <= 5 ? Colors.errorContainer : BatteryState.charging ? ( - BatteryState.percentage == 100 ? Colors.secondary : + BatteryState.full ? Colors.secondary : Colors.tertiary ) : BatteryState.percentage < 10 ? "yellow" : @@ -136,7 +135,7 @@ MouseArea { font.family: Config.font } Text { - text: "charging: " + BatteryState.charging + text: "State: " + BatteryState.state wrapMode: Text.WordWrap color: Colors.primary anchors.horizontalCenter: parent.horizontalCenter @@ -144,7 +143,7 @@ MouseArea { font.family: Config.font } Text { - text: "energy: " + BatteryState.energy + text: "energy: " + Math.round(BatteryState.energy*10)/10 + " W" wrapMode: Text.WordWrap color: Colors.primary anchors.horizontalCenter: parent.horizontalCenter @@ -152,7 +151,10 @@ MouseArea { font.family: Config.font } 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 color: Colors.primary anchors.horizontalCenter: parent.horizontalCenter @@ -160,7 +162,7 @@ MouseArea { font.family: Config.font } Text { - text: "charge_cycle: " + BatteryState.charge_cycle + text: "Usage: " + BatteryState.energy_rate_formated + " W" wrapMode: Text.WordWrap color: Colors.primary anchors.horizontalCenter: parent.horizontalCenter @@ -168,15 +170,8 @@ MouseArea { font.family: Config.font } Text { - text: "time_to_empty: " + BatteryState.time_to_empty + "h" - wrapMode: Text.WordWrap - 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" + text: "Health: " + BatteryState.health + " %" + visible: BatteryState.healthSupported wrapMode: Text.WordWrap color: Colors.primary anchors.horizontalCenter: parent.horizontalCenter