added lightning bolt
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
+43
-10
@@ -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,11 +71,13 @@ 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;
|
||||
|
||||
int percent =
|
||||
@@ -76,14 +85,38 @@ void SystemInfo::updateBattery()
|
||||
.trimmed()
|
||||
.toInt(&ok);
|
||||
|
||||
if (!ok)
|
||||
return;
|
||||
|
||||
if (ok) {
|
||||
percent = qBound(0, percent, 100);
|
||||
|
||||
if (percent == m_batteryPercent)
|
||||
return;
|
||||
|
||||
if (percent != m_batteryPercent) {
|
||||
m_batteryPercent = percent;
|
||||
emit batteryPercentChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// Charging state
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
|
||||
// m_capacityPath is something like:
|
||||
// /sys/class/power_supply/battery/capacity
|
||||
//
|
||||
// The status file lives beside it.
|
||||
const QString batteryDirectory =
|
||||
QFileInfo(m_capacityPath).absolutePath();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,22 @@ class SystemInfo : public QObject
|
||||
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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user