Skip to content

Commit

Permalink
Fix battery icon not updating when charging
Browse files Browse the repository at this point in the history
Use property BatteryState instead of OnBattery which does not update
when the battery is plugged into a power source (tried on a Matebook D
14).
Battery is considered charging if BatteryState is 1 (charging) or
4 (fully charged).

Change-Id: I5a0cf7bf8ee4cd313af885322d1edb67868c555a
  • Loading branch information
carloalbertobarbano committed May 6, 2019
1 parent b6fa433 commit 4dfd6ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions plugins/power/powerplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ QWidget *PowerPlugin::itemTipsWidget(const QString &itemKey)

const uint percentage = qMin(100.0, qMax(0.0, data.value("Display")));
const QString value = QString("%1%").arg(std::round(percentage));
const bool charging = !m_powerInter->onBattery();
const int batteryState = m_powerInter->batteryState()["Display"];
const bool charging = (batteryState == BatteryState::CHARGING || batteryState == BatteryState::FULLY_CHARGED);

if (!charging) {
qint64 timeToEmpty = -1;
Expand All @@ -115,8 +116,6 @@ QWidget *PowerPlugin::itemTipsWidget(const QString &itemKey)
.arg(QDateTime::fromTime_t(timeToEmpty).toUTC().toString("hh:mm:ss"))
);
} else {
const int batteryState = m_powerInter->batteryState()["Display"];

if (batteryState == BatteryState::FULLY_CHARGED || percentage == 100.)
m_tipsLabel->setText(tr("Charged %1 Battery Is Charged").arg(value));
else {
Expand Down
4 changes: 3 additions & 1 deletion plugins/power/powerstatuswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "powerstatuswidget.h"
#include "powerplugin.h"

#include <QPainter>
#include <QIcon>
Expand Down Expand Up @@ -65,7 +66,8 @@ QPixmap PowerStatusWidget::getBatteryIcon()
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
const uint value = qMin(100.0, qMax(0.0, data.value("Display")));
const int percentage = std::round(value);
const bool plugged = !m_powerInter->onBattery();
const int batteryState = m_powerInter->batteryState()["Display"];
const bool plugged = (batteryState == BatteryState::CHARGING || batteryState == BatteryState::FULLY_CHARGED);

QString percentageStr;
if (percentage < 10 && percentage >= 0) {
Expand Down

0 comments on commit 4dfd6ca

Please sign in to comment.