2018-08-18 11:43:48 +02:00
|
|
|
#include "ALabel.hpp"
|
2019-05-02 16:56:45 +02:00
|
|
|
#include <fmt/format.h>
|
2019-05-02 17:51:01 +02:00
|
|
|
#include <util/command.hpp>
|
2018-08-27 01:36:25 +02:00
|
|
|
|
2019-04-24 12:37:24 +02:00
|
|
|
waybar::ALabel::ALabel(const Json::Value& config, const std::string& format, uint16_t interval)
|
2019-04-18 17:52:00 +02:00
|
|
|
: config_(config),
|
|
|
|
format_(config_["format"].isString() ? config_["format"].asString() : format),
|
|
|
|
interval_(config_["interval"] == "once"
|
2019-04-26 21:57:15 +02:00
|
|
|
? std::chrono::seconds(100000000)
|
2019-04-18 17:52:00 +02:00
|
|
|
: std::chrono::seconds(
|
|
|
|
config_["interval"].isUInt() ? config_["interval"].asUInt() : interval)),
|
|
|
|
default_format_(format_) {
|
2018-08-27 01:36:25 +02:00
|
|
|
event_box_.add(label_);
|
2019-04-18 17:52:00 +02:00
|
|
|
if (config_["max-length"].isUInt()) {
|
2018-08-18 11:43:48 +02:00
|
|
|
label_.set_max_width_chars(config_["max-length"].asUInt());
|
|
|
|
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
|
|
|
|
}
|
2019-04-25 22:47:58 +02:00
|
|
|
|
|
|
|
if (config_["rotate"].isUInt()) {
|
2019-04-25 22:56:14 +02:00
|
|
|
label_.set_angle(config["rotate"].asUInt());
|
2019-04-25 22:47:58 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2019-04-18 17:52:00 +02:00
|
|
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &ALabel::handleToggle));
|
2018-10-29 18:04:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// configure events' user commands
|
2019-05-02 17:51:01 +02:00
|
|
|
if (config_["on-click"].isString() || config_["on-click-middle"].isString() ||
|
|
|
|
config_["on-click-backward"].isString() || config_["on-click-forward"].isString() ||
|
|
|
|
config_["on-click-right"].isString()) {
|
2018-10-29 18:04:09 +01:00
|
|
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
2019-04-18 17:52:00 +02:00
|
|
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &ALabel::handleToggle));
|
2018-10-29 18:04:09 +01:00
|
|
|
}
|
2018-11-24 15:56:16 +01:00
|
|
|
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString()) {
|
2019-02-16 09:56:53 +01:00
|
|
|
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
2019-04-18 17:52:00 +02:00
|
|
|
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &ALabel::handleScroll));
|
2018-08-27 01:36:25 +02:00
|
|
|
}
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:56:38 +02:00
|
|
|
waybar::ALabel::~ALabel() {
|
2019-04-24 12:37:24 +02:00
|
|
|
for (const auto& pid : pid_) {
|
2019-04-23 15:56:38 +02:00
|
|
|
if (pid != -1) {
|
|
|
|
kill(-pid, 9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:04:09 +01:00
|
|
|
auto waybar::ALabel::update() -> void {
|
2018-08-18 11:43:48 +02:00
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:04:09 +01:00
|
|
|
bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
|
2019-05-02 16:56:45 +02:00
|
|
|
std::string format;
|
2018-11-01 09:27:00 +01:00
|
|
|
if (config_["on-click"].isString() && e->button == 1) {
|
2019-05-02 16:56:45 +02:00
|
|
|
format = config_["on-click"].asString();
|
2019-03-02 20:07:12 +01:00
|
|
|
} else if (config_["on-click-middle"].isString() && e->button == 2) {
|
2019-05-02 16:56:45 +02:00
|
|
|
format = config_["on-click-middle"].asString();
|
2018-11-24 15:56:16 +01:00
|
|
|
} else if (config_["on-click-right"].isString() && e->button == 3) {
|
2019-05-02 16:56:45 +02:00
|
|
|
format = config_["on-click-right"].asString();
|
2019-03-02 20:07:12 +01:00
|
|
|
} else if (config_["on-click-forward"].isString() && e->button == 8) {
|
2019-05-02 16:56:45 +02:00
|
|
|
format = config_["on-click-backward"].asString();
|
2019-03-02 20:07:12 +01:00
|
|
|
} else if (config_["on-click-backward"].isString() && e->button == 9) {
|
2019-05-02 16:56:45 +02:00
|
|
|
format = config_["on-click-forward"].asString();
|
|
|
|
}
|
|
|
|
if (!format.empty()) {
|
|
|
|
pid_.push_back(
|
|
|
|
waybar::util::command::forkExec(fmt::format(format, fmt::arg("arg", click_param))));
|
2019-03-04 21:00:44 +01:00
|
|
|
}
|
|
|
|
if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) {
|
2019-01-13 22:36:37 +01:00
|
|
|
alt_ = !alt_;
|
2019-03-02 20:07:12 +01:00
|
|
|
if (alt_ && config_["format-alt"].isString()) {
|
2018-10-29 18:04:09 +01:00
|
|
|
format_ = config_["format-alt"].asString();
|
|
|
|
} else {
|
|
|
|
format_ = default_format_;
|
|
|
|
}
|
2018-08-27 01:36:25 +02:00
|
|
|
}
|
2018-10-29 18:04:09 +01:00
|
|
|
|
2018-08-27 01:36:25 +02:00
|
|
|
dp.emit();
|
2019-05-02 17:51:01 +02:00
|
|
|
return true;
|
2018-08-27 01:36:25 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:04:09 +01:00
|
|
|
bool waybar::ALabel::handleScroll(GdkEventScroll* e) {
|
|
|
|
// Avoid concurrent scroll event
|
2018-11-01 09:27:00 +01:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2019-04-18 17:52:00 +02:00
|
|
|
bool direction_up = false;
|
2018-10-29 18:04:09 +01:00
|
|
|
|
2018-11-01 09:27:00 +01:00
|
|
|
if (e->direction == GDK_SCROLL_UP) {
|
|
|
|
direction_up = true;
|
|
|
|
}
|
|
|
|
if (e->direction == GDK_SCROLL_DOWN) {
|
|
|
|
direction_up = false;
|
|
|
|
}
|
|
|
|
if (e->direction == GDK_SCROLL_SMOOTH) {
|
|
|
|
gdouble delta_x, delta_y;
|
2019-04-18 17:52:00 +02:00
|
|
|
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent*>(e), &delta_x, &delta_y);
|
2018-11-01 09:27:00 +01:00
|
|
|
if (delta_y < 0) {
|
2018-10-29 18:04:09 +01:00
|
|
|
direction_up = true;
|
2018-11-01 09:27:00 +01:00
|
|
|
} else if (delta_y > 0) {
|
2018-10-29 18:04:09 +01:00
|
|
|
direction_up = false;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 09:27:00 +01:00
|
|
|
if (direction_up && config_["on-scroll-up"].isString()) {
|
2019-04-23 15:56:38 +02:00
|
|
|
pid_.push_back(waybar::util::command::forkExec(config_["on-scroll-up"].asString()));
|
2018-11-01 09:27:00 +01:00
|
|
|
} else if (config_["on-scroll-down"].isString()) {
|
2019-04-23 15:56:38 +02:00
|
|
|
pid_.push_back(waybar::util::command::forkExec(config_["on-scroll-down"].asString()));
|
2018-11-01 09:27:00 +01:00
|
|
|
}
|
|
|
|
dp.emit();
|
2018-10-29 18:04:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-13 11:31:05 +02:00
|
|
|
std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) {
|
2018-08-29 23:50:41 +02:00
|
|
|
auto format_icons = config_["format-icons"];
|
|
|
|
if (format_icons.isObject()) {
|
2018-12-25 21:03:13 +01:00
|
|
|
if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) {
|
2018-08-29 23:50:41 +02:00
|
|
|
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();
|
2019-05-13 11:31:05 +02:00
|
|
|
auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1);
|
2018-08-29 23:50:41 +02:00
|
|
|
format_icons = format_icons[idx];
|
|
|
|
}
|
|
|
|
if (format_icons.isString()) {
|
|
|
|
return format_icons.asString();
|
|
|
|
}
|
|
|
|
return "";
|
2018-08-27 01:36:25 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 11:36:34 +02:00
|
|
|
std::string waybar::ALabel::getState(uint8_t value, bool reverse) {
|
2019-05-13 11:31:05 +02:00
|
|
|
if (!config_["states"].isObject()) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-05-03 03:39:34 +02: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-13 11:36:34 +02:00
|
|
|
std::sort(states.begin(), states.end(), [&reverse](auto& a, auto& b) {
|
|
|
|
return reverse ? a.second < b.second : a.second > b.second;
|
|
|
|
});
|
2019-05-03 03:39:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
bool waybar::ALabel::tooltipEnabled() {
|
2019-02-24 09:25:34 +01:00
|
|
|
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
|
2019-02-22 11:35:26 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:04:09 +01:00
|
|
|
waybar::ALabel::operator Gtk::Widget&() { return event_box_; }
|