From 6931b0f8b56faef25335543289aeedd8383c3579 Mon Sep 17 00:00:00 2001 From: cybrneko Date: Thu, 17 Sep 2026 00:32:35 -0500 Subject: [PATCH] added lightning bolt --- Main.qml | 35 ++++++++++++++++++++++++--- SystemInfo.cpp | 65 +++++++++++++++++++++++++++++++++++++------------- SystemInfo.h | 14 +++++++++-- 3 files changed, 93 insertions(+), 21 deletions(-) diff --git a/Main.qml b/Main.qml index a5f9df1..6758dbf 100644 --- a/Main.qml +++ b/Main.qml @@ -54,11 +54,16 @@ Window { // ───────────────────────────────────────────────────────────── property int batteryPercent: systemInfo.batteryPercent + property bool charging: systemInfo.charging Item { id: battery - width: batteryIcon.width + 18 + batteryText.width + width: batteryIcon.width + + (root.charging ? chargingIcon.width + 12 : 0) + + 18 + + batteryText.width + height: 72 anchors { @@ -134,14 +139,38 @@ Window { } Text { - id: batteryText + id: chargingIcon anchors { left: batteryIcon.right - leftMargin: 18 + leftMargin: 12 verticalCenter: parent.verticalCenter } + visible: root.charging + text: "⚡" + + color: "#ffffff" + + font { + pixelSize: 38 + weight: Font.Bold + } + } + + Text { + id: batteryText + + anchors { + left: root.charging + ? chargingIcon.right + : batteryIcon.right + + leftMargin: root.charging ? 12 : 18 + + verticalCenter: parent.verticalCenter + } + text: root.batteryPercent + "%" color: "#ffffff" diff --git a/SystemInfo.cpp b/SystemInfo.cpp index fec7d2d..5ffa93b 100644 --- a/SystemInfo.cpp +++ b/SystemInfo.cpp @@ -2,6 +2,7 @@ #include #include +#include #include SystemInfo::SystemInfo(QObject *parent) @@ -16,7 +17,8 @@ SystemInfo::SystemInfo(QObject *parent) this, &SystemInfo::updateBattery); - // Battery percentage doesn't need millisecond precision. + // Battery percentage and charging state don't need + // millisecond precision. m_batteryTimer.start(10000); } @@ -25,6 +27,11 @@ int SystemInfo::batteryPercent() const return m_batteryPercent; } +bool SystemInfo::charging() const +{ + return m_charging; +} + QString SystemInfo::findBatteryCapacityPath() const { QDir powerSupply("/sys/class/power_supply"); @@ -64,26 +71,52 @@ void SystemInfo::updateBattery() if (m_capacityPath.isEmpty()) return; + // ───────────────────────────────────────────────────────────── + // Battery percentage + // ───────────────────────────────────────────────────────────── + QFile capacityFile(m_capacityPath); - if (!capacityFile.open(QIODevice::ReadOnly | QIODevice::Text)) - return; + if (capacityFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + bool ok = false; - bool ok = false; + int percent = + QString::fromUtf8(capacityFile.readAll()) + .trimmed() + .toInt(&ok); - int percent = - QString::fromUtf8(capacityFile.readAll()) - .trimmed() - .toInt(&ok); + if (ok) { + percent = qBound(0, percent, 100); - if (!ok) - return; + if (percent != m_batteryPercent) { + m_batteryPercent = percent; + emit batteryPercentChanged(); + } + } + } - percent = qBound(0, percent, 100); + // ───────────────────────────────────────────────────────────── + // Charging state + // ───────────────────────────────────────────────────────────── - if (percent == m_batteryPercent) - return; + // m_capacityPath is something like: + // /sys/class/power_supply/battery/capacity + // + // The status file lives beside it. + const QString batteryDirectory = + QFileInfo(m_capacityPath).absolutePath(); - m_batteryPercent = percent; - emit batteryPercentChanged(); -} + QFile statusFile(batteryDirectory + "/status"); + + if (statusFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + const QString status = + QString::fromUtf8(statusFile.readAll()).trimmed(); + + const bool charging = (status == "Charging"); + + if (charging != m_charging) { + m_charging = charging; + emit chargingChanged(); + } + } +} \ No newline at end of file diff --git a/SystemInfo.h b/SystemInfo.h index 4815d30..216d49b 100644 --- a/SystemInfo.h +++ b/SystemInfo.h @@ -8,17 +8,25 @@ class SystemInfo : public QObject { Q_OBJECT Q_PROPERTY(int batteryPercent - READ batteryPercent - NOTIFY batteryPercentChanged) + READ batteryPercent + NOTIFY batteryPercentChanged) + + Q_PROPERTY(bool charging + READ charging + NOTIFY chargingChanged) public: explicit SystemInfo(QObject *parent = nullptr); int batteryPercent() const; + bool charging() const; + signals: void batteryPercentChanged(); + void chargingChanged(); + private slots: void updateBattery(); @@ -28,4 +36,6 @@ private: int m_batteryPercent = -1; QString m_capacityPath; QTimer m_batteryTimer; + + bool m_charging = false; };