Feat icons vector (#716)

This commit is contained in:
Alex 2020-05-22 21:23:04 +02:00 committed by GitHub
parent 6b32aca094
commit 6ca4e14b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View File

@ -14,6 +14,7 @@ class ALabel : public AModule {
virtual ~ALabel() = default; virtual ~ALabel() = default;
virtual auto update() -> void; virtual auto update() -> void;
virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0); virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0);
virtual std::string getIcon(uint16_t, std::vector<std::string> &alts, uint16_t max = 0);
protected: protected:
Gtk::Label label_; Gtk::Label label_;

View File

@ -54,6 +54,29 @@ std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_
return ""; return "";
} }
std::string ALabel::getIcon(uint16_t percentage, std::vector<std::string>& 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) { bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) { if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) {
alt_ = !alt_; alt_ = !alt_;

View File

@ -1,4 +1,5 @@
#include "modules/battery.hpp" #include "modules/battery.hpp"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config) 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); hoursRemaining = std::fabs(hoursRemaining);
uint16_t full_hours = static_cast<uint16_t>(hoursRemaining); uint16_t full_hours = static_cast<uint16_t>(hoursRemaining);
uint16_t minutes = static_cast<uint16_t>(60 * (hoursRemaining - full_hours)); uint16_t minutes = static_cast<uint16_t>(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()) { if (config_["format-time"].isString()) {
format = config_["format-time"].asString(); format = config_["format-time"].asString();
} }
@ -173,11 +174,9 @@ auto waybar::modules::Battery::update() -> void {
} }
label_.set_tooltip_text(tooltip_text); label_.set_tooltip_text(tooltip_text);
} }
// Transform to lowercase // Transform to lowercase and replace space with dash
std::transform(status.begin(), status.end(), status.begin(), ::tolower);
// Replace space with dash
std::transform(status.begin(), status.end(), status.begin(), [](char ch) { std::transform(status.begin(), status.end(), status.begin(), [](char ch) {
return ch == ' ' ? '-' : ch; return ch == ' ' ? '-' : std::tolower(ch);
}); });
auto format = format_; auto format = format_;
auto state = getState(capacity, true); auto state = getState(capacity, true);
@ -197,9 +196,10 @@ auto waybar::modules::Battery::update() -> void {
event_box_.hide(); event_box_.hide();
} else { } else {
event_box_.show(); event_box_.show();
auto icons = std::vector<std::string>{status + "-" + state, status, state};
label_.set_markup(fmt::format(format, label_.set_markup(fmt::format(format,
fmt::arg("capacity", capacity), fmt::arg("capacity", capacity),
fmt::arg("icon", getIcon(capacity, state)), fmt::arg("icon", getIcon(capacity, icons)),
fmt::arg("time", formatTimeRemaining(time_remaining)))); fmt::arg("time", formatTimeRemaining(time_remaining))));
} }
// Call parent update // Call parent update