diff --git a/include/AModule.hpp b/include/AModule.hpp index c9f1ae21..91606f55 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -36,6 +36,23 @@ class AModule : public IModule { std::vector pid_; gdouble distance_scrolled_y_; gdouble distance_scrolled_x_; + static const inline std::map, std::string> eventMap_{ + {std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"}, + {std::make_pair(1, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click"}, + {std::make_pair(1, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click"}, + {std::make_pair(2, GdkEventType::GDK_BUTTON_PRESS), "on-click-middle"}, + {std::make_pair(2, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-middle"}, + {std::make_pair(2, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-middle"}, + {std::make_pair(3, GdkEventType::GDK_BUTTON_PRESS), "on-click-right"}, + {std::make_pair(3, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-right"}, + {std::make_pair(3, GdkEventType::GDK_3BUTTON_PRESS), "on-triple-click-right"}, + {std::make_pair(8, GdkEventType::GDK_BUTTON_PRESS), "on-click-backward"}, + {std::make_pair(8, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-backward"}, + {std::make_pair(8, GdkEventType::GDK_2BUTTON_PRESS), "on-triple-click-backward"}, + {std::make_pair(9, GdkEventType::GDK_BUTTON_PRESS), "on-click-forward"}, + {std::make_pair(9, GdkEventType::GDK_2BUTTON_PRESS), "on-double-click-forward"}, + {std::make_pair(9, GdkEventType::GDK_2BUTTON_PRESS), "on-triple-click-forward"} + }; }; } // namespace waybar diff --git a/src/AModule.cpp b/src/AModule.cpp index 0e0f00ec..e65067e0 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -10,16 +10,24 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: , distance_scrolled_y_(0.0) , distance_scrolled_x_(0.0) { // 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) { + if (enable_click) { event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle)); + } else { + std::map, std::string>::const_iterator it{eventMap_.cbegin()}; + while(it != eventMap_.cend()) { + if (config_[it->second].isString()) { + event_box_.add_events(Gdk::BUTTON_PRESS_MASK); + event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle)); + break; + } + ++it; + } } 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() { @@ -30,7 +38,6 @@ AModule::~AModule() { } } - auto AModule::update() -> void { // Run user-provided update handler if configured if (config_["on-update"].isString()) { @@ -39,18 +46,14 @@ auto AModule::update() -> void { } 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-backward"].isString() && e->button == 8) { - format = config_["on-click-backward"].asString(); - } else if (config_["on-click-forward"].isString() && e->button == 9) { - format = config_["on-click-forward"].asString(); + const std::map, std::string>::const_iterator& rec{eventMap_.find(std::pair(e->button, e->type))}; + std::string format{ (rec != eventMap_.cend()) ? rec->second : std::string{""}}; + + if (!format.empty()) { + if (config_[format].isString()) format = config_[format].asString(); + else format.clear(); } + if (!format.empty()) { pid_.push_back(util::command::forkExec(format)); }