add popover component

This commit is contained in:
2025-12-20 20:52:04 +01:00
parent 3558fd3ebc
commit 47f052f913
8 changed files with 119 additions and 89 deletions

View File

@@ -30,6 +30,7 @@ target_sources(bar_lib
src/services/tray.cpp src/services/tray.cpp
src/services/notifications.cpp src/services/notifications.cpp
src/widgets/tray.cpp src/widgets/tray.cpp
src/components/popover.cpp
) )
include_directories(bar_lib PRIVATE include_directories(bar_lib PRIVATE
include include

View File

@@ -0,0 +1,19 @@
#pragma once
#include <gtkmm/button.h>
#include <gtkmm/popover.h>
#include <string>
class Popover: public Gtk::Button {
public:
Popover(std::string icon, std::string name);
~Popover() override;
protected:
void on_toggle_window();
Gtk::Popover* popover = nullptr;
void set_popover_child(Gtk::Widget& child) {
gtk_popover_set_child(popover->gobj(), child.gobj());
}
};

View File

@@ -1,14 +1,11 @@
#pragma once #pragma once
#include "components/popover.hpp"
#include <gtkmm/button.h> #include <gtkmm/button.h>
#include <gtkmm/popover.h> #include <gtkmm/popover.h>
class WebWidget : public Gtk::Button { class WebWidget : public Popover {
public: public:
WebWidget(std::string icon, std::string title, std::string url); WebWidget(std::string icon, std::string title, std::string url);
~WebWidget() override;
private: private:
void on_toggle_window();
Gtk::Popover* popover = nullptr;
}; };

View File

@@ -0,0 +1,35 @@
#include "components/popover.hpp"
#include "gtkmm/label.h"
#include "gtkmm/object.h"
Popover::Popover(std::string icon, std::string name) {
auto label = Gtk::make_managed<Gtk::Label>(icon);
label->add_css_class("icon-label");
set_child(*label);
signal_clicked().connect(
sigc::mem_fun(*this, &Popover::on_toggle_window));
popover = new Gtk::Popover();
popover->set_parent(*this);
popover->set_autohide(true);
popover->signal_closed().connect([this]() {
this->add_css_class("minimized");
this->remove_css_class("restored");
});
}
Popover::~Popover() {
delete popover;
}
void Popover::on_toggle_window() {
if (popover->get_visible()) {
popover->popdown();
} else {
this->remove_css_class("minimized");
this->add_css_class("restored");
popover->popup();
}
}

View File

@@ -270,7 +270,6 @@ void HyprlandService::onUrgentEvent(std::string windowAddress) {
it->second->urgentWindows.push_back(windowAddress); it->second->urgentWindows.push_back(windowAddress);
workspaceStateChanged.emit(); workspaceStateChanged.emit();
} }
} }
break; break;

View File

@@ -90,8 +90,10 @@ static void on_method_call(GDBusConnection* /*connection*/,
std::cout << "Title: " << (summary ? summary : "") << std::endl; std::cout << "Title: " << (summary ? summary : "") << std::endl;
std::cout << "Body: " << (body ? body : "") << std::endl; std::cout << "Body: " << (body ? body : "") << std::endl;
if (actions) g_variant_unref(actions); if (actions)
if (hints) g_variant_unref(hints); g_variant_unref(actions);
if (hints)
g_variant_unref(hints);
guint32 id = self->allocateNotificationId(replaces_id); guint32 id = self->allocateNotificationId(replaces_id);
g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", id)); g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", id));
@@ -107,8 +109,7 @@ static void on_method_call(GDBusConnection* /*connection*/,
"body-markup", "body-markup",
"icon-static", "icon-static",
"persistence", "persistence",
nullptr nullptr};
};
GVariant *capsV = g_variant_new_strv(caps, -1); GVariant *capsV = g_variant_new_strv(caps, -1);
g_dbus_method_invocation_return_value(invocation, g_variant_new("(@as)", capsV)); g_dbus_method_invocation_return_value(invocation, g_variant_new("(@as)", capsV));
return; return;
@@ -148,7 +149,8 @@ static void on_method_call(GDBusConnection* /*connection*/,
} }
guint32 NotificationService::allocateNotificationId(guint32 replacesId) { guint32 NotificationService::allocateNotificationId(guint32 replacesId) {
if (replacesId != 0) return replacesId; if (replacesId != 0)
return replacesId;
return this->nextNotificationId++; return this->nextNotificationId++;
} }
@@ -175,8 +177,10 @@ NotificationService::~NotificationService() {
-1, -1,
nullptr, nullptr,
&error); &error);
if (releaseResult) g_variant_unref(releaseResult); if (releaseResult)
if (error) g_error_free(error); g_variant_unref(releaseResult);
if (error)
g_error_free(error);
} }
if (this->registrationId != 0) { if (this->registrationId != 0) {
@@ -204,7 +208,8 @@ void NotificationService::intialize() {
gchar *address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, nullptr, &error); gchar *address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, nullptr, &error);
if (!address) { if (!address) {
std::cerr << "Failed to get session bus address: " << (error ? error->message : "unknown error") << std::endl; std::cerr << "Failed to get session bus address: " << (error ? error->message : "unknown error") << std::endl;
if (error) g_error_free(error); if (error)
g_error_free(error);
return; return;
} }
@@ -225,7 +230,8 @@ void NotificationService::intialize() {
if (!this->connection) { if (!this->connection) {
std::cerr << "Failed to connect to session bus: " << (error ? error->message : "unknown error") << std::endl; std::cerr << "Failed to connect to session bus: " << (error ? error->message : "unknown error") << std::endl;
if (error) g_error_free(error); if (error)
g_error_free(error);
return; return;
} }
@@ -237,7 +243,8 @@ void NotificationService::intialize() {
this->nodeInfo = g_dbus_node_info_new_for_xml(kNotificationsIntrospectionXml, &error); this->nodeInfo = g_dbus_node_info_new_for_xml(kNotificationsIntrospectionXml, &error);
if (!this->nodeInfo) { if (!this->nodeInfo) {
std::cerr << "Failed to create introspection data: " << (error ? error->message : "unknown error") << std::endl; std::cerr << "Failed to create introspection data: " << (error ? error->message : "unknown error") << std::endl;
if (error) g_error_free(error); if (error)
g_error_free(error);
return; return;
} }
@@ -258,7 +265,8 @@ void NotificationService::intialize() {
if (this->registrationId == 0) { if (this->registrationId == 0) {
std::cerr << "Failed to register notifications object: " << (error ? error->message : "unknown error") << std::endl; std::cerr << "Failed to register notifications object: " << (error ? error->message : "unknown error") << std::endl;
if (error) g_error_free(error); if (error)
g_error_free(error);
return; return;
} }
@@ -280,7 +288,8 @@ void NotificationService::intialize() {
if (!requestResult) { if (!requestResult) {
std::cerr << "Failed to RequestName(org.freedesktop.Notifications): " std::cerr << "Failed to RequestName(org.freedesktop.Notifications): "
<< (error ? error->message : "unknown error") << std::endl; << (error ? error->message : "unknown error") << std::endl;
if (error) g_error_free(error); if (error)
g_error_free(error);
return; return;
} }

View File

@@ -3,23 +3,7 @@
#include <gtkmm/label.h> #include <gtkmm/label.h>
#include <webkit/webkit.h> #include <webkit/webkit.h>
WebWidget::WebWidget(std::string icon, std::string title, std::string url) { WebWidget::WebWidget(std::string icon, std::string name, std::string url) : Popover(icon, name) {
auto label = Gtk::make_managed<Gtk::Label>(icon);
label->add_css_class("icon-label");
set_child(*label);
signal_clicked().connect(
sigc::mem_fun(*this, &WebWidget::on_toggle_window));
popover = new Gtk::Popover();
popover->set_parent(*this);
popover->set_autohide(true);
popover->signal_closed().connect([this]() {
this->add_css_class("minimized");
this->remove_css_class("restored");
});
auto webview = webkit_web_view_new(); auto webview = webkit_web_view_new();
gtk_widget_set_hexpand(webview, true); gtk_widget_set_hexpand(webview, true);
gtk_widget_set_vexpand(webview, true); gtk_widget_set_vexpand(webview, true);
@@ -31,19 +15,5 @@ WebWidget::WebWidget(std::string icon, std::string title, std::string url) {
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), url.c_str()); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), url.c_str());
gtk_popover_set_child(popover->gobj(), webview); this->set_popover_child(*Glib::wrap(webview));
}
WebWidget::~WebWidget() {
delete popover;
}
void WebWidget::on_toggle_window() {
if (popover->get_visible()) {
popover->popdown();
} else {
popover->popup();
this->remove_css_class("minimized");
this->add_css_class("restored");
}
} }