mirror of
				https://github.com/rad4day/Waybar.git
				synced 2025-10-31 16:02:43 +01:00 
			
		
		
		
	ALabel: Add support for configurable mouse events
This patch adds 3 new configuration options applicable for subclasses of ALabel. The options can be used to execute user defined code in response to the 3 mouse events: * on-click: The left mouse button click * on-scroll-up * on-scroll-down This patch also modifies the behaviour of the format-alt toggle such that when the on-click event is configured, format-alt is toggled on any mouse click other than left click. When on-click is not defined, any mouse button would toggle format-alt. Signed-off-by: Harish Krupo <harishkrupo@gmail.com>
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| #include "ALabel.hpp" | ||||
| #include <util/command.hpp> | ||||
|  | ||||
| #include <iostream> | ||||
|  | ||||
| @@ -14,30 +15,94 @@ waybar::ALabel::ALabel(const Json::Value& config, const std::string format) | ||||
|   } | ||||
|   if (config_["format-alt"].isString()) { | ||||
|     event_box_.add_events(Gdk::BUTTON_PRESS_MASK); | ||||
|     event_box_.signal_button_press_event() | ||||
|       .connect(sigc::mem_fun(*this, &ALabel::handleToggle)); | ||||
|     event_box_.signal_button_press_event().connect( | ||||
|         sigc::mem_fun(*this, &ALabel::handleToggle)); | ||||
|   } | ||||
|  | ||||
|   // configure events' user commands | ||||
|   if (config_["on-click"].isString()) { | ||||
|     std::string cmd = config_["on-click"].asString(); | ||||
|     event_box_.add_events(Gdk::BUTTON_PRESS_MASK); | ||||
|     event_box_.signal_button_press_event().connect( | ||||
|       sigc::mem_fun(*this, &ALabel::handleToggle)); | ||||
|  | ||||
|     button_press_cmd_ = cmd; | ||||
|   } | ||||
|   if (config_["on-scroll-up"].isString()) { | ||||
|     std::string cmd = config_["on-scroll-up"].asString(); | ||||
|     event_box_.add_events(Gdk::SCROLL_MASK); | ||||
|     event_box_.signal_scroll_event().connect( | ||||
|       sigc::mem_fun(*this, &ALabel::handleScroll)); | ||||
|  | ||||
|     scroll_up_cmd_ = cmd; | ||||
|   } | ||||
|   if (config_["on-scroll-down"].isString()) { | ||||
|     std::string cmd = config_["on-scroll-down"].asString(); | ||||
|     event_box_.add_events(Gdk::SCROLL_MASK); | ||||
|     event_box_.signal_scroll_event().connect( | ||||
|       sigc::mem_fun(*this, &ALabel::handleScroll)); | ||||
|  | ||||
|     scroll_down_cmd_ = cmd; | ||||
|   } | ||||
| } | ||||
|  | ||||
| auto waybar::ALabel::update() -> void | ||||
| { | ||||
| auto waybar::ALabel::update() -> void { | ||||
|   // Nothing here | ||||
| } | ||||
|  | ||||
| bool waybar::ALabel::handleToggle(GdkEventButton* const& /*ev*/) | ||||
| { | ||||
|   alt = !alt; | ||||
|   if (alt) { | ||||
|     format_ = config_["format-alt"].asString(); | ||||
| bool waybar::ALabel::handleToggle(GdkEventButton* const& e) { | ||||
|   if (button_press_cmd_ != "" && e->button == 1) { | ||||
|     waybar::util::command::forkExec(button_press_cmd_); | ||||
|   } else { | ||||
|     format_ = default_format_; | ||||
|     alt = !alt; | ||||
|     if (alt) { | ||||
|       format_ = config_["format-alt"].asString(); | ||||
|     } else { | ||||
|       format_ = default_format_; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   dp.emit(); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) | ||||
| { | ||||
| bool waybar::ALabel::handleScroll(GdkEventScroll* e) { | ||||
|  | ||||
|   // Avoid concurrent scroll event | ||||
|   { | ||||
|     std::lock_guard<std::mutex> lock(mutex_); | ||||
|     bool direction_up = false; | ||||
|  | ||||
|     if (e->direction == GDK_SCROLL_UP) { | ||||
|       direction_up = true; | ||||
|     } | ||||
|     if (e->direction == GDK_SCROLL_DOWN) { | ||||
|       direction_up = false; | ||||
|     } | ||||
|     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); | ||||
|       if (delta_y < 0) { | ||||
|         direction_up = true; | ||||
|       } else if (delta_y > 0) { | ||||
|         direction_up = false; | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     if (direction_up) | ||||
|       waybar::util::command::forkExec(scroll_up_cmd_); | ||||
|     else | ||||
|       waybar::util::command::forkExec(scroll_down_cmd_); | ||||
|  | ||||
|     dp.emit(); | ||||
|   } | ||||
|  | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| std::string waybar::ALabel::getIcon(uint16_t percentage, | ||||
|                                     const std::string& alt) { | ||||
|   auto format_icons = config_["format-icons"]; | ||||
|   if (format_icons.isObject()) { | ||||
|     if (!alt.empty() && format_icons[alt].isString()) { | ||||
| @@ -57,6 +122,4 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) | ||||
|   return ""; | ||||
| } | ||||
|  | ||||
| waybar::ALabel::operator Gtk::Widget &() { | ||||
|   return event_box_; | ||||
| } | ||||
| waybar::ALabel::operator Gtk::Widget&() { return event_box_; } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Harish Krupo
					Harish Krupo