108 lines
3.7 KiB
C++
108 lines
3.7 KiB
C++
|
#include "AModule.hpp"
|
||
|
#include <fmt/format.h>
|
||
|
#include <util/command.hpp>
|
||
|
|
||
|
namespace waybar {
|
||
|
|
||
|
AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
|
||
|
bool enable_click, bool enable_scroll)
|
||
|
: config_(std::move(config)) {
|
||
|
// configure events' user commands
|
||
|
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() || enable_click) {
|
||
|
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
|
||
|
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle));
|
||
|
}
|
||
|
if (config_["on-scroll-up"].isString() || config_["on-scroll-down"].isString() || enable_scroll) {
|
||
|
event_box_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
||
|
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
AModule::~AModule() {
|
||
|
for (const auto& pid : pid_) {
|
||
|
if (pid != -1) {
|
||
|
kill(-pid, 9);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
auto AModule::update() -> void {
|
||
|
// Nothing here
|
||
|
}
|
||
|
|
||
|
bool AModule::handleToggle(GdkEventButton* const& e) {
|
||
|
std::string format;
|
||
|
if (config_["on-click"].isString() && e->button == 1) {
|
||
|
format = config_["on-click"].asString();
|
||
|
} else if (config_["on-click-middle"].isString() && e->button == 2) {
|
||
|
format = config_["on-click-middle"].asString();
|
||
|
} else if (config_["on-click-right"].isString() && e->button == 3) {
|
||
|
format = config_["on-click-right"].asString();
|
||
|
} else if (config_["on-click-forward"].isString() && e->button == 8) {
|
||
|
format = config_["on-click-backward"].asString();
|
||
|
} else if (config_["on-click-backward"].isString() && e->button == 9) {
|
||
|
format = config_["on-click-forward"].asString();
|
||
|
}
|
||
|
if (!format.empty()) {
|
||
|
pid_.push_back(util::command::forkExec(fmt::format(format, fmt::arg("arg", click_param_))));
|
||
|
}
|
||
|
dp.emit();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
AModule::SCROLL_DIR AModule::getScrollDir(GdkEventScroll* e) {
|
||
|
SCROLL_DIR dir{SCROLL_DIR::NONE};
|
||
|
if (e->direction == GDK_SCROLL_UP) {
|
||
|
dir = SCROLL_DIR::UP;
|
||
|
} else if (e->direction == GDK_SCROLL_DOWN) {
|
||
|
dir = SCROLL_DIR::DOWN;
|
||
|
} else if (e->direction == GDK_SCROLL_LEFT) {
|
||
|
dir = SCROLL_DIR::LEFT;
|
||
|
} else if (e->direction == GDK_SCROLL_RIGHT) {
|
||
|
dir = SCROLL_DIR::RIGHT;
|
||
|
} else if (e->direction == GDK_SCROLL_SMOOTH) {
|
||
|
gdouble delta_x, delta_y;
|
||
|
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent*>(e), &delta_x, &delta_y);
|
||
|
distance_scrolled_ += delta_y;
|
||
|
// TODO: handle X axis
|
||
|
gdouble threshold = 0;
|
||
|
if (config_["smooth-scrolling-threshold"].isNumeric()) {
|
||
|
threshold = config_["smooth-scrolling-threshold"].asDouble();
|
||
|
}
|
||
|
|
||
|
if (distance_scrolled_ < -threshold) {
|
||
|
dir = SCROLL_DIR::UP;
|
||
|
} else if (distance_scrolled_ > threshold) {
|
||
|
dir = SCROLL_DIR::DOWN;
|
||
|
}
|
||
|
if (abs(distance_scrolled_) > threshold) {
|
||
|
distance_scrolled_ = 0;
|
||
|
} else {
|
||
|
// Don't execute the action if we haven't met the threshold!
|
||
|
return SCROLL_DIR::NONE;
|
||
|
}
|
||
|
}
|
||
|
return dir;
|
||
|
}
|
||
|
|
||
|
bool AModule::handleScroll(GdkEventScroll* e) {
|
||
|
auto dir = getScrollDir(e);
|
||
|
if (dir == SCROLL_DIR::UP && config_["on-scroll-up"].isString()) {
|
||
|
pid_.push_back(util::command::forkExec(config_["on-scroll-up"].asString()));
|
||
|
} else if (dir == SCROLL_DIR::DOWN && config_["on-scroll-down"].isString()) {
|
||
|
pid_.push_back(util::command::forkExec(config_["on-scroll-down"].asString()));
|
||
|
}
|
||
|
dp.emit();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool AModule::tooltipEnabled() {
|
||
|
return config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true;
|
||
|
}
|
||
|
|
||
|
AModule::operator Gtk::Widget&() { return event_box_; }
|
||
|
|
||
|
} // namespace waybar
|