add notifications
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include "app.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <vector>
|
||||
#include "services/notification.hpp"
|
||||
|
||||
App::App() {
|
||||
this->app = Gtk::Application::create("org.example.mybar");
|
||||
this->setupServices();
|
||||
this->hyprlandService = HyprlandService::getInstance();
|
||||
this->notificationService = std::make_shared<NotificationService>();
|
||||
|
||||
app->signal_activate().connect([&]() {
|
||||
auto display = Gdk::Display::get_default();
|
||||
@@ -17,7 +18,7 @@ App::App() {
|
||||
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
|
||||
monitors->get_object(i)
|
||||
);
|
||||
|
||||
|
||||
if (monitor) {
|
||||
auto bar = std::make_shared<Bar>(monitor->gobj());
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
#include "components/workspaceIndicator.hpp"
|
||||
#include <iostream>
|
||||
#include "services/hyprland.hpp"
|
||||
|
||||
#include "gtkmm/gestureclick.h"
|
||||
#include "gtkmm/label.h"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "services/hyprland.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <glib-unix.h>
|
||||
#include <glib.h>
|
||||
|
||||
89
src/services/notification.cpp
Normal file
89
src/services/notification.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "services/notification.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include "widgets/notification.hpp"
|
||||
#include "gtkmm/object.h"
|
||||
|
||||
void NotificationService::onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name) {
|
||||
std::cout << "Acquired bus name: " << name << std::endl;
|
||||
|
||||
auto introspection_data = Gio::DBus::NodeInfo::create_for_xml(introspection_xml);
|
||||
|
||||
// Register the object at the standard path
|
||||
connection->register_object(
|
||||
"/org/freedesktop/Notifications",
|
||||
introspection_data->lookup_interface("org.freedesktop.Notifications"),
|
||||
getMessageInterfaceVTable());
|
||||
}
|
||||
|
||||
const Gio::DBus::InterfaceVTable &NotificationService::getMessageInterfaceVTable() {
|
||||
static Gio::DBus::InterfaceVTable vtable(
|
||||
sigc::mem_fun(*this, &NotificationService::on_method_call));
|
||||
return vtable;
|
||||
}
|
||||
|
||||
void NotificationService::on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &,
|
||||
const Glib::ustring &,
|
||||
const Glib::ustring &,
|
||||
const Glib::ustring &,
|
||||
const Glib::ustring &method_name,
|
||||
const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation) {
|
||||
|
||||
if (method_name == "Notify") {
|
||||
handle_notify(parameters, invocation);
|
||||
} else if (method_name == "GetCapabilities") {
|
||||
auto caps = std::vector<Glib::ustring>{"body"};
|
||||
invocation->return_value(Glib::VariantContainerBase::create_tuple(
|
||||
Glib::Variant<std::vector<Glib::ustring>>::create(caps)));
|
||||
} else if (method_name == "GetServerInformation") {
|
||||
invocation->return_value(Glib::VariantContainerBase::create_tuple({Glib::Variant<Glib::ustring>::create("MyGtkmmNotifier"),
|
||||
Glib::Variant<Glib::ustring>::create("Custom"),
|
||||
Glib::Variant<Glib::ustring>::create("1.0"),
|
||||
Glib::Variant<Glib::ustring>::create("1.2")}));
|
||||
} else {
|
||||
// Handle other methods or return error
|
||||
invocation->return_value(Glib::VariantContainerBase());
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationService::handle_notify(const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation) {
|
||||
|
||||
Glib::VariantBase app_name_var, replaces_id_var, app_icon_var, summary_var, body_var, actions_var, hints_var, timeout_var;
|
||||
parameters.get_child(app_name_var, 0);
|
||||
parameters.get_child(summary_var, 3);
|
||||
parameters.get_child(body_var, 4);
|
||||
|
||||
Glib::ustring summary = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(summary_var).get();
|
||||
Glib::ustring body = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(body_var).get();
|
||||
|
||||
std::cout << "Notification Received: " << summary << " - " << body << std::endl;
|
||||
|
||||
createNotificationPopup(summary, body);
|
||||
|
||||
guint id = notificationIdCounter++;
|
||||
invocation->return_value(Glib::VariantContainerBase::create_tuple(
|
||||
Glib::Variant<guint>::create(id)));
|
||||
}
|
||||
|
||||
void NotificationService::createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message) {
|
||||
auto display = Gdk::Display::get_default();
|
||||
if (!display) {
|
||||
std::cerr << "Error: No default display found" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
auto monitors = display->get_monitors();
|
||||
if (!monitors) {
|
||||
std::cerr << "Error: No monitors found" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (guint i = 0; i < monitors->get_n_items(); ++i) {
|
||||
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(monitors->get_object(i));
|
||||
if (!monitor) continue;
|
||||
|
||||
auto widget = std::make_shared<NotificationWidget>(monitor, title, message);
|
||||
}
|
||||
}
|
||||
34
src/widgets/notification.cpp
Normal file
34
src/widgets/notification.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "widgets/notification.hpp"
|
||||
|
||||
#include "gtk4-layer-shell.h"
|
||||
|
||||
NotificationWidget::NotificationWidget(std::shared_ptr<Gdk::Monitor> monitor, const Glib::ustring &title, const Glib::ustring &message) {
|
||||
if (!monitor) return;
|
||||
|
||||
auto win = new Gtk::Window();
|
||||
win->set_title(title);
|
||||
win->set_default_size(300, 100);
|
||||
|
||||
auto label = Gtk::make_managed<Gtk::Label>(message);
|
||||
label->set_use_markup(true);
|
||||
win->set_child(*label);
|
||||
|
||||
gtk_layer_init_for_window(win->gobj());
|
||||
gtk_layer_set_monitor(win->gobj(), monitor->gobj());
|
||||
gtk_layer_set_layer(win->gobj(), GTK_LAYER_SHELL_LAYER_OVERLAY);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, TRUE);
|
||||
gtk_layer_set_anchor(win->gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE);
|
||||
gtk_layer_set_margin(win->gobj(), GTK_LAYER_SHELL_EDGE_TOP, 2);
|
||||
|
||||
win->add_css_class("notification-popup");
|
||||
|
||||
win->show();
|
||||
|
||||
// Auto close after 3 seconds for demo purposes
|
||||
Glib::signal_timeout().connect([win]() {
|
||||
win->close();
|
||||
delete win;
|
||||
return false; // Don't repeat
|
||||
},
|
||||
3000);
|
||||
}
|
||||
Reference in New Issue
Block a user