From e158a3e1324e610ae48bbfc85d551beac2e9dd5f Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 2 May 2019 21:39:34 -0400 Subject: [PATCH] feat(states): add generic 'states' to all labels --- include/ALabel.hpp | 1 + include/modules/battery.hpp | 2 +- src/ALabel.cpp | 24 ++++++++++++++++++++++++ src/modules/battery.cpp | 24 +----------------------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/ALabel.hpp b/include/ALabel.hpp index a6a9a5f1..d4d10878 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -31,6 +31,7 @@ class ALabel : public IModule { virtual bool handleToggle(GdkEventButton *const &ev); virtual bool handleScroll(GdkEventScroll *); + virtual std::string getState(uint8_t value); private: std::vector pid_; diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 09601f7b..f57170f5 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -34,7 +34,7 @@ class Battery : public ALabel { void worker(); const std::string getAdapterStatus(uint8_t capacity) const; const std::tuple getInfos() const; - const std::string getState(uint8_t) const; + // const std::string getState(uint8_t) const; util::SleeperThread thread_; util::SleeperThread thread_timer_; diff --git a/src/ALabel.cpp b/src/ALabel.cpp index a42bea74..9435e03b 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -129,6 +129,30 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) return ""; } +std::string waybar::ALabel::getState(uint8_t value) { + // Get current state + std::vector> states; + if (config_["states"].isObject()) { + for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) { + if (it->isUInt() && it.key().isString()) { + states.emplace_back(it.key().asString(), it->asUInt()); + } + } + } + // Sort states + std::sort(states.begin(), states.end(), [](auto& a, auto& b) { return a.second < b.second; }); + std::string valid_state; + for (auto const& state : states) { + if (value <= state.second && valid_state.empty()) { + label_.get_style_context()->add_class(state.first); + valid_state = state.first; + } else { + label_.get_style_context()->remove_class(state.first); + } + } + return valid_state; +} + bool waybar::ALabel::tooltipEnabled() { return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true; } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 148865e6..785b9071 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -109,29 +109,7 @@ const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) c return "Unknown"; } -const std::string waybar::modules::Battery::getState(uint8_t capacity) const { - // Get current state - std::vector> states; - if (config_["states"].isObject()) { - for (auto it = config_["states"].begin(); it != config_["states"].end(); ++it) { - if (it->isUInt() && it.key().isString()) { - states.emplace_back(it.key().asString(), it->asUInt()); - } - } - } - // Sort states - std::sort(states.begin(), states.end(), [](auto& a, auto& b) { return a.second < b.second; }); - std::string valid_state; - for (auto const& state : states) { - if (capacity <= state.second && valid_state.empty()) { - label_.get_style_context()->add_class(state.first); - valid_state = state.first; - } else { - label_.get_style_context()->remove_class(state.first); - } - } - return valid_state; -} + auto waybar::modules::Battery::update() -> void { auto [capacity, status] = getInfos();