Waybar. Issue#1068. Double/Triple events

pull/1499/head
Viktar Lukashonak 2022-03-29 12:26:05 +03:00
parent d4a07483b2
commit b086e2f995
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
2 changed files with 38 additions and 16 deletions

View File

@ -7,6 +7,8 @@
#include "IModule.hpp" #include "IModule.hpp"
#include <map>
namespace waybar { namespace waybar {
class AModule : public IModule { class AModule : public IModule {
@ -36,6 +38,23 @@ class AModule : public IModule {
std::vector<int> pid_; std::vector<int> pid_;
gdouble distance_scrolled_y_; gdouble distance_scrolled_y_;
gdouble distance_scrolled_x_; gdouble distance_scrolled_x_;
static const inline std::map<std::pair<uint, GdkEventType>, 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 } // namespace waybar

View File

@ -10,16 +10,24 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
, distance_scrolled_y_(0.0) , distance_scrolled_y_(0.0)
, distance_scrolled_x_(0.0) { , distance_scrolled_x_(0.0) {
// configure events' user commands // configure events' user commands
if (config_["on-click"].isString() || config_["on-click-middle"].isString() || if (enable_click) {
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_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle)); event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle));
} else {
std::map<std::pair<uint, GdkEventType>, 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) { 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_.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll)); event_box_.signal_scroll_event().connect(sigc::mem_fun(*this, &AModule::handleScroll));
} }
} }
AModule::~AModule() { AModule::~AModule() {
@ -30,7 +38,6 @@ AModule::~AModule() {
} }
} }
auto AModule::update() -> void { auto AModule::update() -> void {
// Run user-provided update handler if configured // Run user-provided update handler if configured
if (config_["on-update"].isString()) { if (config_["on-update"].isString()) {
@ -39,18 +46,14 @@ auto AModule::update() -> void {
} }
bool AModule::handleToggle(GdkEventButton* const& e) { bool AModule::handleToggle(GdkEventButton* const& e) {
std::string format; const std::map<std::pair<uint, GdkEventType>, std::string>::const_iterator& rec{eventMap_.find(std::pair(e->button, e->type))};
if (config_["on-click"].isString() && e->button == 1) { std::string format{ (rec != eventMap_.cend()) ? rec->second : std::string{""}};
format = config_["on-click"].asString();
} else if (config_["on-click-middle"].isString() && e->button == 2) { if (!format.empty()) {
format = config_["on-click-middle"].asString(); if (config_[format].isString()) format = config_[format].asString();
} else if (config_["on-click-right"].isString() && e->button == 3) { else format.clear();
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();
} }
if (!format.empty()) { if (!format.empty()) {
pid_.push_back(util::command::forkExec(format)); pid_.push_back(util::command::forkExec(format));
} }