2018-08-18 09:43:48 +00:00
|
|
|
#include "ALabel.hpp"
|
2019-05-02 14:56:45 +00:00
|
|
|
#include <fmt/format.h>
|
2019-05-02 15:51:01 +00:00
|
|
|
#include <util/command.hpp>
|
2018-08-26 23:36:25 +00:00
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
namespace waybar {
|
|
|
|
|
|
|
|
ALabel::ALabel(const Json::Value& config, const std::string& name, const std::string& id,
|
2020-08-13 02:46:51 +00:00
|
|
|
const std::string& format, uint16_t interval, bool ellipsize, bool enable_click,
|
|
|
|
bool enable_scroll)
|
|
|
|
: AModule(config, name, id, config["format-alt"].isString() || enable_click, enable_scroll),
|
2019-04-18 15:52:00 +00:00
|
|
|
format_(config_["format"].isString() ? config_["format"].asString() : format),
|
|
|
|
interval_(config_["interval"] == "once"
|
2019-04-26 19:57:15 +00:00
|
|
|
? std::chrono::seconds(100000000)
|
2019-04-18 15:52:00 +00:00
|
|
|
: std::chrono::seconds(
|
|
|
|
config_["interval"].isUInt() ? config_["interval"].asUInt() : interval)),
|
|
|
|
default_format_(format_) {
|
2019-05-22 10:06:24 +00:00
|
|
|
label_.set_name(name);
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-08-26 23:36:25 +00:00
|
|
|
event_box_.add(label_);
|
2019-04-18 15:52:00 +00:00
|
|
|
if (config_["max-length"].isUInt()) {
|
2018-08-18 09:43:48 +00:00
|
|
|
label_.set_max_width_chars(config_["max-length"].asUInt());
|
|
|
|
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
|
2020-08-19 21:11:11 +00:00
|
|
|
label_.set_single_line_mode(true);
|
2019-06-28 12:16:09 +00:00
|
|
|
} else if (ellipsize && label_.get_max_width_chars() == -1) {
|
2019-06-28 11:49:04 +00:00
|
|
|
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
|
2020-08-19 21:11:11 +00:00
|
|
|
label_.set_single_line_mode(true);
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2019-04-25 20:47:58 +00:00
|
|
|
|
|
|
|
if (config_["rotate"].isUInt()) {
|
2019-04-25 20:56:14 +00:00
|
|
|
label_.set_angle(config["rotate"].asUInt());
|
2019-04-25 20:47:58 +00:00
|
|
|
}
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
auto ALabel::update() -> void {
|
2020-04-12 16:30:21 +00:00
|
|
|
AModule::update();
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) {
|
2018-08-29 21:50:41 +00:00
|
|
|
auto format_icons = config_["format-icons"];
|
|
|
|
if (format_icons.isObject()) {
|
2018-12-25 20:03:13 +00:00
|
|
|
if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) {
|
2018-08-29 21:50:41 +00:00
|
|
|
format_icons = format_icons[alt];
|
|
|
|
} else {
|
|
|
|
format_icons = format_icons["default"];
|
|
|
|
}
|
2018-08-26 23:36:25 +00:00
|
|
|
}
|
2018-08-29 21:50:41 +00:00
|
|
|
if (format_icons.isArray()) {
|
|
|
|
auto size = format_icons.size();
|
2019-05-13 09:31:05 +00:00
|
|
|
auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1);
|
2018-08-29 21:50:41 +00:00
|
|
|
format_icons = format_icons[idx];
|
|
|
|
}
|
|
|
|
if (format_icons.isString()) {
|
|
|
|
return format_icons.asString();
|
|
|
|
}
|
|
|
|
return "";
|
2018-08-26 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 19:23:04 +00:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
|
|
|
|
if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) {
|
|
|
|
alt_ = !alt_;
|
|
|
|
if (alt_ && config_["format-alt"].isString()) {
|
|
|
|
format_ = config_["format-alt"].asString();
|
|
|
|
} else {
|
|
|
|
format_ = default_format_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AModule::handleToggle(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ALabel::getState(uint8_t value, bool lesser) {
|
2019-05-13 09:31:05 +00:00
|
|
|
if (!config_["states"].isObject()) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-05-03 01:39:34 +00:00
|
|
|
// Get current state
|
|
|
|
std::vector<std::pair<std::string, uint8_t>> 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
|
2019-05-16 07:39:06 +00:00
|
|
|
std::sort(states.begin(), states.end(), [&lesser](auto& a, auto& b) {
|
|
|
|
return lesser ? a.second < b.second : a.second > b.second;
|
2019-05-13 09:36:34 +00:00
|
|
|
});
|
2019-05-03 01:39:34 +00:00
|
|
|
std::string valid_state;
|
|
|
|
for (auto const& state : states) {
|
2019-05-16 03:14:51 +00:00
|
|
|
if ((lesser ? value <= state.second : value >= state.second) && valid_state.empty()) {
|
2019-05-03 01:39:34 +00:00
|
|
|
label_.get_style_context()->add_class(state.first);
|
|
|
|
valid_state = state.first;
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class(state.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid_state;
|
|
|
|
}
|
|
|
|
|
2019-06-15 12:57:52 +00:00
|
|
|
} // namespace waybar
|