diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index e587ea11..50df78d3 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -23,12 +23,13 @@ class Battery : public ALabel { void worker(); std::tuple getInfos(); - std::string getState(uint16_t, bool); + std::string getState(uint16_t); util::SleeperThread thread_; util::SleeperThread threadTimer_; std::vector batteries_; int fd_; + std::string old_status_; }; } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 70892fa8..83ebd08f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -66,7 +66,7 @@ std::tuple waybar::modules::Battery::getInfos() { try { uint16_t total = 0; - std::string status; + std::string status = "Unknown"; for (auto const& bat : batteries_) { uint16_t capacity; std::string _status; @@ -85,7 +85,7 @@ std::tuple waybar::modules::Battery::getInfos() } } -std::string waybar::modules::Battery::getState(uint16_t capacity, bool charging) +std::string waybar::modules::Battery::getState(uint16_t capacity) { // Get current state std::vector> states; @@ -102,7 +102,7 @@ std::string waybar::modules::Battery::getState(uint16_t capacity, bool charging) }); std::string validState = ""; for (auto state : states) { - if (capacity <= state.second && !charging && validState.empty()) { + if (capacity <= state.second && validState.empty()) { label_.get_style_context()->add_class(state.first); validState = state.first; } else { @@ -116,21 +116,17 @@ auto waybar::modules::Battery::update() -> void { auto [capacity, status] = getInfos(); label_.set_tooltip_text(status); - bool charging = status == "Charging"; + std::transform(status.begin(), status.end(), status.begin(), ::tolower); auto format = format_; - if (charging) { - label_.get_style_context()->add_class("charging"); - if (config_["format-charging"].isString()) { - format = config_["format-charging"].asString(); - } - } else { - label_.get_style_context()->remove_class("charging"); - if (status == "Full" && config_["format-full"].isString()) { - format = config_["format-full"].asString(); - } - } - auto state = getState(capacity, charging); - if (!state.empty() && config_["format-" + state].isString()) { + auto state = getState(capacity); + label_.get_style_context()->remove_class(old_status_); + label_.get_style_context()->add_class(status); + old_status_ = status; + if (!state.empty() && config_["format-" + status + "-" + state].isString()) { + format = config_["format-" + status + "-" + state].asString(); + } else if (config_["format-" + status].isString()) { + format = config_["format-" + status].asString(); + } else if (!state.empty() && config_["format-" + state].isString()) { format = config_["format-" + state].asString(); } if (format.empty()) {