waybar/src/ALabel.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

#include "ALabel.hpp"
2018-08-27 01:36:25 +02:00
#include <iostream>
waybar::ALabel::ALabel(const Json::Value& config, const std::string format)
: config_(config),
2018-10-26 09:27:16 +02:00
format_(config_["format"].isString() ? config_["format"].asString() : format),
2018-08-27 01:36:25 +02:00
default_format_(format_)
{
2018-08-27 01:36:25 +02:00
event_box_.add(label_);
2018-10-26 09:27:16 +02:00
if (config_["max-length"].isUInt()) {
label_.set_max_width_chars(config_["max-length"].asUInt());
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
}
2018-10-26 09:27:16 +02:00
if (config_["format-alt"].isString()) {
2018-08-27 01:36:25 +02:00
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event()
.connect(sigc::mem_fun(*this, &ALabel::handleToggle));
}
}
auto waybar::ALabel::update() -> void
{
// Nothing here
}
2018-09-10 11:16:57 +02:00
bool waybar::ALabel::handleToggle(GdkEventButton* const& /*ev*/)
2018-08-27 01:36:25 +02:00
{
alt = !alt;
if (alt) {
format_ = config_["format-alt"].asString();
} else {
format_ = default_format_;
}
dp.emit();
return true;
}
2018-08-29 23:50:41 +02:00
std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt)
2018-08-27 01:36:25 +02:00
{
2018-08-29 23:50:41 +02:00
auto format_icons = config_["format-icons"];
if (format_icons.isObject()) {
if (!alt.empty() && format_icons[alt]) {
format_icons = format_icons[alt];
} else {
format_icons = format_icons["default"];
}
2018-08-27 01:36:25 +02:00
}
2018-08-29 23:50:41 +02:00
if (format_icons.isArray()) {
auto size = format_icons.size();
auto idx = std::clamp(percentage / (100 / size), 0U, size - 1);
format_icons = format_icons[idx];
}
if (format_icons.isString()) {
return format_icons.asString();
}
return "";
2018-08-27 01:36:25 +02:00
}
waybar::ALabel::operator Gtk::Widget &() {
2018-08-27 01:36:25 +02:00
return event_box_;
}