add notifications
This commit is contained in:
@@ -28,10 +28,12 @@ target_sources(bar_lib
|
||||
|
||||
src/widgets/clock.cpp
|
||||
src/widgets/date.cpp
|
||||
src/widgets/notification.cpp
|
||||
src/widgets/volumeWidget.cpp
|
||||
src/widgets/webWidget.cpp
|
||||
|
||||
src/services/hyprland.cpp
|
||||
src/services/notification.cpp
|
||||
src/services/tray.cpp
|
||||
|
||||
src/widgets/tray.cpp
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "bar/bar.hpp"
|
||||
#include "services/hyprland.hpp"
|
||||
#include "services/notification.hpp"
|
||||
|
||||
#include "glibmm/refptr.h"
|
||||
#include "gtkmm/application.h"
|
||||
@@ -17,8 +18,9 @@ class App {
|
||||
private:
|
||||
Glib::RefPtr<Gtk::Application> app;
|
||||
std::vector<std::shared_ptr<Bar>> bars;
|
||||
std::shared_ptr<NotificationService> notificationService = nullptr;
|
||||
HyprlandService *hyprlandService = nullptr;
|
||||
TrayService *trayService = TrayService::getInstance();
|
||||
|
||||
TrayService *trayService = TrayService::getInstance();
|
||||
void setupServices();
|
||||
};
|
||||
75
include/services/notification.hpp
Normal file
75
include/services/notification.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <giomm.h>
|
||||
#include <gtkmm.h>
|
||||
#include <memory>
|
||||
#include <sigc++/sigc++.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
#include "giomm/dbusconnection.h"
|
||||
#include "giomm/dbusownname.h"
|
||||
#include "glib.h"
|
||||
|
||||
const Glib::ustring introspection_xml = R"(
|
||||
<node>
|
||||
<interface name="org.freedesktop.Notifications">
|
||||
<method name="GetCapabilities">
|
||||
<arg name="capabilities" type="as" direction="out"/>
|
||||
</method>
|
||||
<method name="Notify">
|
||||
<arg name="app_name" type="s" direction="in"/>
|
||||
<arg name="replaces_id" type="u" direction="in"/>
|
||||
<arg name="app_icon" type="s" direction="in"/>
|
||||
<arg name="summary" type="s" direction="in"/>
|
||||
<arg name="body" type="s" direction="in"/>
|
||||
<arg name="actions" type="as" direction="in"/>
|
||||
<arg name="hints" type="a{sv}" direction="in"/>
|
||||
<arg name="expire_timeout" type="i" direction="in"/>
|
||||
<arg name="id" type="u" direction="out"/>
|
||||
</method>
|
||||
<method name="CloseNotification">
|
||||
<arg name="id" type="u" direction="in"/>
|
||||
</method>
|
||||
<method name="GetServerInformation">
|
||||
<arg name="name" type="s" direction="out"/>
|
||||
<arg name="vendor" type="s" direction="out"/>
|
||||
<arg name="version" type="s" direction="out"/>
|
||||
<arg name="spec_version" type="s" direction="out"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
||||
)";
|
||||
|
||||
class NotificationService {
|
||||
|
||||
public:
|
||||
NotificationService() : notificationIdCounter(1) {
|
||||
|
||||
Gio::DBus::own_name(
|
||||
Gio::DBus::BusType::SESSION,
|
||||
"org.freedesktop.Notifications",
|
||||
sigc::mem_fun(*this, &NotificationService::onBusAcquired),
|
||||
{}, // Name acquired slot (optional)
|
||||
{}, // Name lost slot (optional)
|
||||
Gio::DBus::BusNameOwnerFlags::REPLACE);
|
||||
}
|
||||
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
|
||||
|
||||
private:
|
||||
guint notificationIdCounter;
|
||||
const Gio::DBus::InterfaceVTable &getMessageInterfaceVTable();
|
||||
void on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||
const Glib::ustring &sender,
|
||||
const Glib::ustring &object_path,
|
||||
const Glib::ustring &interface_name,
|
||||
const Glib::ustring &method_name,
|
||||
const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation);
|
||||
|
||||
void handle_notify(const Glib::VariantContainerBase ¶meters,
|
||||
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation);
|
||||
void createNotificationPopup(const Glib::ustring &title, const Glib::ustring &message);
|
||||
};
|
||||
10
include/widgets/notification.hpp
Normal file
10
include/widgets/notification.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <giomm.h>
|
||||
#include <gtkmm.h>
|
||||
#include <memory>
|
||||
#include "gdkmm/monitor.h"
|
||||
class NotificationWidget {
|
||||
public:
|
||||
NotificationWidget(std::shared_ptr<Gdk::Monitor> monitor, const Glib::ustring &title, const Glib::ustring &message);
|
||||
};
|
||||
@@ -142,3 +142,12 @@ button {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.notification-popup {
|
||||
border-radius: 8px;
|
||||
padding: 8px 12px;
|
||||
background: rgba(30, 30, 30, 0.948);
|
||||
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
|
||||
border: 1px solid rgba(80, 80, 80, 0.8);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
@@ -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