From 884b909e7d4c8f670785ba3fac80ec931922ab50 Mon Sep 17 00:00:00 2001 From: Benjamin Voisin Date: Thu, 9 May 2024 17:28:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20GtkMenu=20to=20the=20AModule?= =?UTF-8?q?=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit You can configure what key launch the menu with the "menu" element in the config, the xml file that describes the menu with the "menu-file" element in the config, and the actions of each buttons with the "menu-actions" field. --- include/AModule.hpp | 5 +++++ src/AModule.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) 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())