From 6e71e8a4f3e7683cc3867e41e5f78883c44f3bfc Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Sat, 28 Oct 2023 23:29:27 +0200 Subject: [PATCH] Add battery charging indication --- css/style.css | 4 ++++ css/style.scss | 5 +++++ src/Bar.cpp | 15 +++++++++++++++ src/System.cpp | 19 +++++++++++++++++++ src/System.h | 1 + 5 files changed, 44 insertions(+) diff --git a/css/style.css b/css/style.css index 5237b61..e3752ba 100644 --- a/css/style.css +++ b/css/style.css @@ -198,6 +198,10 @@ highlight { font-size: 16px; } +.battery-charging { + color: #ffb86c; +} + .network-data-text { color: #50fa7b; font-size: 16px; diff --git a/css/style.scss b/css/style.scss index 66ea431..3f3d739 100644 --- a/css/style.scss +++ b/css/style.scss @@ -229,11 +229,16 @@ highlight { background-color: $inactive; font-size: $textsize; } + .battery-data-text { color: $pink; font-size: $textsize; } +.battery-charging { + color: $orange +} + .network-data-text { color: $green; font-size: $textsize; diff --git a/src/Bar.cpp b/src/Bar.cpp index c022406..a5ff239 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -42,6 +42,7 @@ namespace Bar } static Text* batteryText; + static bool wasCharging = false; static TimerResult UpdateBattery(Sensor& sensor) { double percentage = System::GetBatteryPercentage(); @@ -56,6 +57,20 @@ namespace Bar batteryText->SetText(text); } sensor.SetValue(percentage); + + bool isCharging = System::IsBatteryCharging(); + if (isCharging && !wasCharging && sensor.Get() != nullptr) + { + sensor.AddClass("battery-charging"); + batteryText->AddClass("battery-charging"); + wasCharging = true; + } + else if (!isCharging && wasCharging) + { + sensor.RemoveClass("battery-charging"); + batteryText->RemoveClass("battery-charging"); + wasCharging = false; + } return TimerResult::Ok; } diff --git a/src/System.cpp b/src/System.cpp index 35b69c2..8de4b36 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -89,6 +89,25 @@ namespace System return temp; } + bool IsBatteryCharging() + { + std::ifstream batteryStatus(Config::Get().batteryFolder + "/status"); + if (batteryStatus.is_open()) + { + std::string status; + std::getline(batteryStatus, status); + if (status == "Charging" || status == "Full") + { + return true; + } + else + { + return false; + } + } + return false; + } + double GetBatteryPercentage() { std::ifstream fullChargeFile(Config::Get().batteryFolder + "/charge_full"); diff --git a/src/System.h b/src/System.h index 73cc915..7061544 100644 --- a/src/System.h +++ b/src/System.h @@ -11,6 +11,7 @@ namespace System // Tctl double GetCPUTemp(); + bool IsBatteryCharging(); double GetBatteryPercentage(); struct RAMInfo