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
+49 -16
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();
}
}
}