diff --git a/include/AModule.hpp b/include/AModule.hpp index 58076655..903e00b8 100644 --- a/include/AModule.hpp +++ b/include/AModule.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "IModule.hpp" @@ -52,6 +53,10 @@ class AModule : public IModule { std::vector pid_; gdouble distance_scrolled_y_; gdouble distance_scrolled_x_; + GObject* menu_; + std::map submenus_; + std::map menuActionsMap_; + static void handleGtkMenuEvent(GtkMenuItem* menuitem, gpointer data); std::map eventActionMap_; static const inline std::map, std::string> eventMap_{ {std::make_pair(1, GdkEventType::GDK_BUTTON_PRESS), "on-click"}, diff --git a/src/AModule.cpp b/src/AModule.cpp index 915c8603..709a1e38 100644 --- a/src/AModule.cpp +++ b/src/AModule.cpp @@ -27,6 +27,24 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: else spdlog::warn("Wrong actions section configuration. See config by index: {}", it.index()); } + // If a GTKMenu is requested in the config + if (config_["menu"].isString()) { + // Create the GTKMenu widget + GtkBuilder* builder = gtk_builder_new_from_file(config_["menu-file"].asString().c_str()); + menu_ = gtk_builder_get_object(builder, "menu"); + submenus_ = std::map(); + menuActionsMap_ = std::map(); + // Linking actions to the GTKMenu based on + for (Json::Value::const_iterator it = config_["menu-actions"].begin(); it != config_["menu-actions"].end(); ++it) { + std::string key = it.key().asString(); + submenus_[key] = GTK_MENU_ITEM(gtk_builder_get_object(builder, key.c_str())); + menuActionsMap_[key] = it->asString(); + g_signal_connect(submenus_[key], "activate", G_CALLBACK(handleGtkMenuEvent), (gpointer) menuActionsMap_[key].c_str()); + + } + // Enable click + enable_click = true; + } event_box_.signal_enter_notify_event().connect(sigc::mem_fun(*this, &AModule::handleMouseEnter)); event_box_.signal_leave_notify_event().connect(sigc::mem_fun(*this, &AModule::handleMouseLeave)); @@ -66,6 +84,10 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std:: } } +void AModule::handleGtkMenuEvent(GtkMenuItem* menuitem, gpointer data) { + waybar::util::command::res res = waybar::util::command::exec((char*) data, "TLP"); +} + AModule::~AModule() { for (const auto& pid : pid_) { if (pid != -1) { @@ -133,6 +155,14 @@ bool AModule::handleUserEvent(GdkEventButton* const& e) { format = rec->second; } + + // Check if the event is the one specified for the "menu" option + if (rec->second == config_["menu"].asString()) { + // Popup the menu + gtk_widget_show_all(GTK_WIDGET(menu_)); + gtk_menu_popup_at_pointer (GTK_MENU(menu_), reinterpret_cast(e)); + + } // Second call user scripts if (!format.empty()) { if (config_[format].isString())