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 4b397446..9787a8dc 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -34,7 +34,6 @@ 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; 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/backlight.cpp b/src/modules/backlight.cpp index ea09b4ef..76892996 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -181,6 +181,7 @@ auto waybar::modules::Backlight::update() -> void { const auto percent = best->get_max() == 0 ? 100 : best->get_actual() * 100 / best->get_max(); label_.set_markup(fmt::format( format_, fmt::arg("percent", std::to_string(percent)), fmt::arg("icon", getIcon(percent)))); + getState(percent); } else { if (!previous_best_.has_value()) { return; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index f2e6a88c..607efdd4 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -109,30 +109,6 @@ 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(); if (status == "Unknown") { diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp index 880a05a5..7c75cb90 100644 --- a/src/modules/cpu.cpp +++ b/src/modules/cpu.cpp @@ -20,6 +20,7 @@ auto waybar::modules::Cpu::update() -> void { label_.set_tooltip_text(tooltip); } label_.set_markup(fmt::format(format_, fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage))); + getState(cpu_usage); } uint16_t waybar::modules::Cpu::getCpuLoad() { diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 1ebaef39..de5fb24a 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -16,6 +16,7 @@ auto waybar::modules::Memory::update() -> void { parseMeminfo(); if (memtotal_ > 0 && memfree_ >= 0) { int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; + getState(used_ram_percentage); label_.set_markup(fmt::format(format_, used_ram_percentage)); auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2); if (tooltipEnabled()) { diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 0a041db1..94f34cd0 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -170,6 +170,7 @@ auto waybar::modules::Network::update() -> void { if (!alt_) { format_ = default_format_; } + getState(signal_strength_); auto text = fmt::format(format_, fmt::arg("essid", essid_), fmt::arg("signaldBm", signal_strength_dbm_), diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index ed2fd67c..3c768445 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -202,6 +202,7 @@ auto waybar::modules::Pulseaudio::update() -> void { } label_.set_markup(fmt::format( format, fmt::arg("volume", volume_), fmt::arg("icon", getIcon(volume_, getPortIcon())))); + getState(volume_); if (tooltipEnabled()) { label_.set_tooltip_text(desc_); }