Merge pull request #3247 from benjamin-voisin/menu
Adding the ability to have dropdown menu for modules
This commit is contained in:
@ -2,6 +2,8 @@
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <util/command.hpp>
|
||||
|
||||
namespace waybar {
|
||||
@ -9,7 +11,9 @@ namespace waybar {
|
||||
ALabel::ALabel(const Json::Value& config, const std::string& name, const std::string& id,
|
||||
const std::string& format, uint16_t interval, bool ellipsize, bool enable_click,
|
||||
bool enable_scroll)
|
||||
: AModule(config, name, id, config["format-alt"].isString() || enable_click, enable_scroll),
|
||||
: AModule(config, name, id,
|
||||
config["format-alt"].isString() || config["menu"].isString() || enable_click,
|
||||
enable_scroll),
|
||||
format_(config_["format"].isString() ? config_["format"].asString() : format),
|
||||
interval_(config_["interval"] == "once"
|
||||
? std::chrono::seconds::max()
|
||||
@ -51,6 +55,47 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
|
||||
}
|
||||
}
|
||||
|
||||
// If a GTKMenu is requested in the config
|
||||
if (config_["menu"].isString()) {
|
||||
// Create the GTKMenu widget
|
||||
try {
|
||||
// Check that the file exists
|
||||
std::string menuFile = config_["menu-file"].asString();
|
||||
// Read the menu descriptor file
|
||||
std::ifstream file(menuFile);
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("Failed to open file: " + menuFile);
|
||||
}
|
||||
std::stringstream fileContent;
|
||||
fileContent << file.rdbuf();
|
||||
GtkBuilder* builder = gtk_builder_new();
|
||||
|
||||
// Make the GtkBuilder and check for errors in his parsing
|
||||
if (!gtk_builder_add_from_string(builder, fileContent.str().c_str(), -1, nullptr)) {
|
||||
throw std::runtime_error("Error found in the file " + menuFile);
|
||||
}
|
||||
|
||||
menu_ = gtk_builder_get_object(builder, "menu");
|
||||
if (!menu_) {
|
||||
throw std::runtime_error("Failed to get 'menu' object from GtkBuilder");
|
||||
}
|
||||
submenus_ = std::map<std::string, GtkMenuItem*>();
|
||||
menuActionsMap_ = std::map<std::string, std::string>();
|
||||
|
||||
// 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());
|
||||
}
|
||||
} catch (std::runtime_error& e) {
|
||||
spdlog::warn("Error while creating the menu : {}. Menu popup not activated.", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
if (config_["justify"].isString()) {
|
||||
auto justify_str = config_["justify"].asString();
|
||||
if (justify_str == "left") {
|
||||
@ -125,6 +170,10 @@ bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
|
||||
return AModule::handleToggle(e);
|
||||
}
|
||||
|
||||
void ALabel::handleGtkMenuEvent(GtkMenuItem* menuitem, gpointer data) {
|
||||
waybar::util::command::res res = waybar::util::command::exec((char*)data, "GtkMenu");
|
||||
}
|
||||
|
||||
std::string ALabel::getState(uint8_t value, bool lesser) {
|
||||
if (!config_["states"].isObject()) {
|
||||
return "";
|
||||
|
@ -133,6 +133,16 @@ bool AModule::handleUserEvent(GdkEventButton* const& e) {
|
||||
|
||||
format = rec->second;
|
||||
}
|
||||
|
||||
// Check that a menu has been configured
|
||||
if (config_["menu"].isString()) {
|
||||
// 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<GdkEvent*>(e));
|
||||
}
|
||||
}
|
||||
// Second call user scripts
|
||||
if (!format.empty()) {
|
||||
if (config_[format].isString())
|
||||
|
Reference in New Issue
Block a user