diff --git a/include/ALabel.hpp b/include/ALabel.hpp index f4ca8af7..d4ad94d3 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -14,6 +14,7 @@ class ALabel : public AModule { virtual ~ALabel() = default; virtual auto update() -> void; virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0); + virtual std::string getIcon(uint16_t, std::vector &alts, uint16_t max = 0); protected: Gtk::Label label_; diff --git a/src/ALabel.cpp b/src/ALabel.cpp index e251f0db..68313e09 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -54,6 +54,29 @@ std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_ return ""; } +std::string ALabel::getIcon(uint16_t percentage, std::vector& alts, uint16_t max) { + auto format_icons = config_["format-icons"]; + if (format_icons.isObject()) { + for (const auto& alt : alts) { + if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) { + format_icons = format_icons[alt]; + break; + } else { + format_icons = format_icons["default"]; + } + } + } + if (format_icons.isArray()) { + auto size = format_icons.size(); + auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1); + format_icons = format_icons[idx]; + } + if (format_icons.isString()) { + return format_icons.asString(); + } + return ""; +} + bool waybar::ALabel::handleToggle(GdkEventButton* const& e) { if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) { alt_ = !alt_; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 6e86eaf4..406b4a33 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -1,4 +1,5 @@ #include "modules/battery.hpp" + #include waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config) @@ -151,7 +152,7 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai hoursRemaining = std::fabs(hoursRemaining); uint16_t full_hours = static_cast(hoursRemaining); uint16_t minutes = static_cast(60 * (hoursRemaining - full_hours)); - auto format = std::string("{H} h {M} min"); + auto format = std::string("{H} h {M} min"); if (config_["format-time"].isString()) { format = config_["format-time"].asString(); } @@ -173,11 +174,9 @@ auto waybar::modules::Battery::update() -> void { } label_.set_tooltip_text(tooltip_text); } - // Transform to lowercase - std::transform(status.begin(), status.end(), status.begin(), ::tolower); - // Replace space with dash + // Transform to lowercase and replace space with dash std::transform(status.begin(), status.end(), status.begin(), [](char ch) { - return ch == ' ' ? '-' : ch; + return ch == ' ' ? '-' : std::tolower(ch); }); auto format = format_; auto state = getState(capacity, true); @@ -197,9 +196,10 @@ auto waybar::modules::Battery::update() -> void { event_box_.hide(); } else { event_box_.show(); + auto icons = std::vector{status + "-" + state, status, state}; label_.set_markup(fmt::format(format, fmt::arg("capacity", capacity), - fmt::arg("icon", getIcon(capacity, state)), + fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", formatTimeRemaining(time_remaining)))); } // Call parent update