From b086e2f9959a055b2a1d5655d17aed927b16b34a Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Tue, 29 Mar 2022 12:26:05 +0300 Subject: [PATCH 1/2] Waybar. Issue#1068. Double/Triple events --- include/AModule.hpp | 19 +++++++++++++++++++ src/AModule.cpp | 35 +++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/include/AModule.hpp b/include/AModule.hpp index c9f1ae2..ea73028 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -7,6 +7,8 @@ #include "IModule.hpp" +#include + namespace waybar { class AModule : public IModule { @@ -36,6 +38,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 0e0f00e..e65067e 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)); } From f4cc088d2fa5574632baf47687fa3cad7c9a670f Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Tue, 29 Mar 2022 12:35:50 +0300 Subject: [PATCH 2/2] Waybar. Issue#1068. Double/Triple events. Removed unnecessary including --- include/AModule.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/AModule.hpp b/include/AModule.hpp index ea73028..91606f5 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -7,8 +7,6 @@ #include "IModule.hpp" -#include - namespace waybar { class AModule : public IModule {