From 9a3044a54f1d38a3618cb6369ebbcf6a91e9d996 Mon Sep 17 00:00:00 2001 From: Milo Mordaunt Date: Wed, 24 Apr 2024 18:15:40 -0400 Subject: [PATCH] Cursor change to indicate module clickability (#3108) * Indicate clickability on mouse hover * Avoid messy overrides situation * Update AModule.cpp * Update AModule.cpp * Update AModule.cpp * Update AModule.cpp --------- Co-authored-by: Alexis Rouillard --- include/AModule.hpp | 3 +++ src/AModule.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/include/AModule.hpp b/include/AModule.hpp index ea692ff8..58076655 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -37,6 +37,8 @@ class AModule : public IModule { const Json::Value &config_; Gtk::EventBox event_box_; + virtual void setCursor(Gdk::CursorType const c); + virtual bool handleToggle(GdkEventButton *const &ev); virtual bool handleMouseEnter(GdkEventCrossing *const &ev); virtual bool handleMouseLeave(GdkEventCrossing *const &ev); @@ -46,6 +48,7 @@ class AModule : public IModule { private: bool handleUserEvent(GdkEventButton *const &ev); const bool isTooltip; + bool hasUserEvents_; std::vector pid_; gdouble distance_scrolled_y_; gdouble distance_scrolled_x_; diff --git a/src/AModule.cpp b/src/AModule.cpp index 399a23e4..082e6233 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -15,6 +15,7 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: distance_scrolled_x_(0.0) { // Configure module action Map const Json::Value actions{config_["actions"]}; + for (Json::Value::const_iterator it = actions.begin(); it != actions.end(); ++it) { if (it.key().isString() && it->isString()) if (eventActionMap_.count(it.key().asString()) == 0) { @@ -31,17 +32,20 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: event_box_.signal_leave_notify_event().connect(sigc::mem_fun(*this, &AModule::handleMouseLeave)); // configure events' user commands - // hasUserEvent is true if any element from eventMap_ is satisfying the condition in the lambda - bool hasUserEvent = + // hasUserEvents is true if any element from eventMap_ is satisfying the condition in the lambda + bool hasUserEvents = std::find_if(eventMap_.cbegin(), eventMap_.cend(), [&config](const auto& eventEntry) { // True if there is any non-release type event return eventEntry.first.second != GdkEventType::GDK_BUTTON_RELEASE && config[eventEntry.second].isString(); }) != eventMap_.cend(); - if (enable_click || hasUserEvent) { + if (enable_click || hasUserEvents) { + hasUserEvents_ = true; event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &AModule::handleToggle)); + } else { + hasUserEvents_ = false; } bool hasReleaseEvent = @@ -86,10 +90,21 @@ auto AModule::doAction(const std::string& name) -> void { } } + +void AModule::setCursor(Gdk::CursorType c) { + auto cursor = Gdk::Cursor::create(Gdk::HAND2); + auto gdk_window = event_box_.get_window(); + gdk_window->set_cursor(cursor); +} + bool AModule::handleMouseEnter(GdkEventCrossing* const& e) { if (auto* module = event_box_.get_child(); module != nullptr) { module->set_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); } + + if (hasUserEvents_) { + setCursor(Gdk::HAND2); + } return false; } @@ -97,6 +112,10 @@ bool AModule::handleMouseLeave(GdkEventCrossing* const& e) { if (auto* module = event_box_.get_child(); module != nullptr) { module->unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT); } + + if (hasUserEvents_) { + setCursor(Gdk::ARROW); + } return false; } @@ -108,6 +127,7 @@ bool AModule::handleUserEvent(GdkEventButton* const& e) { std::string format{}; const std::map, std::string>::const_iterator& rec{ eventMap_.find(std::pair(e->button, e->type))}; + if (rec != eventMap_.cend()) { // First call module actions this->AModule::doAction(rec->second);