added lightning bolt

This commit is contained in:
2026-09-17 00:32:35 -05:00
parent 114d7d546c
commit 6931b0f8b5
3 changed files with 93 additions and 21 deletions
+32 -3
View File
@@ -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,11 +139,35 @@ 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
}
+48 -15
View File
@@ -2,6 +2,7 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
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();
}
}
}
+12 -2
View File
@@ -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;
};