SNI: Add context menu

Since it uses a 13 year old library, it is broken. The popup size is
hard-fixed at 200x200, with looks really ugly with non-transparent
background.
This commit is contained in:
scorpion-26 2023-04-30 23:13:41 +02:00
parent 40115befdd
commit b7a92e50d9
7 changed files with 65 additions and 3 deletions

View file

@ -3,6 +3,10 @@
font-family: "CaskaydiaCove Nerd Font";
}
.popup {
color: #50fa7b;
}
.bar, tooltip {
background-color: #282a36;
border-radius: 16px;

View file

@ -27,6 +27,10 @@ $textsize: 16px;
//background-color: #00cc00
}
.popup {
color: #50fa7b;
}
.bar,tooltip{
background-color: $bg;

View file

@ -44,6 +44,7 @@ sni_watcher_header = custom_target('generate-sni-watcher-header',
input: ['protocols/sni-watcher.xml'],
output: ['sni-watcher.h'],
command: [dbus_gen, '--c-namespace', 'sni', '--header', '--output', '@OUTPUT@', '@INPUT@'])
libdbusmenu = dependency('dbusmenu-gtk3-0.4')
gtk = dependency('gtk+-3.0')
gtk_layer_shell = dependency('gtk-layer-shell-0')
@ -109,7 +110,7 @@ libgBar = library('gBar',
'src/Log.cpp',
'src/SNI.cpp',
],
dependencies: [gtk, gtk_layer_shell, pulse, wayland_client],
dependencies: [gtk, gtk_layer_shell, pulse, wayland_client, libdbusmenu],
include_directories: stb,
install: true)

View file

@ -5,6 +5,7 @@
#include <sni-watcher.h>
#include <sni-item.h>
#include <gio/gio.h>
#include <libdbusmenu-gtk/menu.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
@ -28,6 +29,10 @@ namespace SNI
uint8_t* iconData = nullptr;
std::string tooltip;
std::string menuObjectPath;
EventBox* gtkEvent;
};
std::vector<Item> items;
@ -182,6 +187,22 @@ namespace SNI
g_variant_unref(tooltipVar);
}
// Query menu
GVariant* menuPath = getProperty("Menu");
if (menuPath)
{
GVariant* menuVariant;
g_variant_get_child(menuPath, 0, "v", &menuVariant);
const char* objectPath;
g_variant_get(menuVariant, "o", &objectPath);
LOG("SNI: Menu object path: " << objectPath);
item.menuObjectPath = objectPath;
g_variant_unref(menuVariant);
g_variant_unref(menuPath);
}
return item;
}
@ -266,11 +287,36 @@ namespace SNI
{
if (item.iconData)
{
auto eventBox = Widget::Create<EventBox>();
item.gtkEvent = eventBox.get();
eventBox->SetOnCreate(
[&](Widget& w)
{
auto clickFn = [](GtkWidget*, GdkEventButton* event, void* data) -> gboolean
{
if (event->button == 1)
{
Item* item = (Item*)data;
GtkMenu* menu = (GtkMenu*)dbusmenu_gtkmenu_new(item->name.data(), item->menuObjectPath.data());
LOG(menu);
gtk_menu_attach_to_widget(menu, item->gtkEvent->Get(), nullptr);
gtk_menu_popup_at_pointer(menu, (GdkEvent*)event);
LOG(item->menuObjectPath << " click");
}
return GDK_EVENT_STOP;
};
g_signal_connect(w.Get(), "button-release-event", G_CALLBACK(+clickFn), &item);
});
auto texture = Widget::Create<Texture>();
texture->SetHorizontalTransform({0, true, Alignment::Fill});
texture->SetBuf(item.w, item.h, item.iconData);
texture->SetTooltip(item.tooltip);
iconBox->AddChild(std::move(texture));
eventBox->AddChild(std::move(texture));
iconBox->AddChild(std::move(eventBox));
}
}
parentBox->AddChild(std::move(container));

View file

@ -173,6 +173,9 @@ void Widget::ApplyPropertiesToWidget()
gtk_widget_set_valign(m_Widget, Utils::ToGtkAlign(m_VerticalTransform.alignment));
gtk_widget_set_hexpand(m_Widget, m_HorizontalTransform.expand);
gtk_widget_set_vexpand(m_Widget, m_VerticalTransform.expand);
if (m_OnCreate)
m_OnCreate(*this);
}
void Box::SetOrientation(Orientation orientation)

View file

@ -157,6 +157,8 @@ public:
void SetVisible(bool visible);
void SetOnCreate(Callback<Widget>&& onCreate) { m_OnCreate = onCreate; }
protected:
void PropagateToParent(GdkEvent* event);
void ApplyPropertiesToWidget();
@ -169,6 +171,8 @@ protected:
std::string m_Tooltip;
Transform m_HorizontalTransform; // X
Transform m_VerticalTransform; // Y
Callback<Widget> m_OnCreate;
};
class Box : public Widget