close one notification to close all

This commit is contained in:
2026-02-02 21:47:47 +01:00
parent ed1a9a8605
commit 6d9f016350
9 changed files with 137 additions and 33 deletions

View File

@@ -1,8 +1,13 @@
#pragma once #pragma once
#include <cstdint>
#include <map>
#include <memory> #include <memory>
#include <sys/types.h>
#include <vector> #include <vector>
#include "services/dbus/messages.hpp" #include "services/dbus/messages.hpp"
#include "widgets/notification/baseNotification.hpp"
#include "gdkmm/monitor.h" #include "gdkmm/monitor.h"
@@ -21,6 +26,10 @@ class NotificationController {
void showNotificationOnAllMonitors(NotifyMessage notify); void showNotificationOnAllMonitors(NotifyMessage notify);
private: private:
uint64_t globalNotificationId = 1;
std::map<uint64_t, std::vector<std::shared_ptr<BaseNotification>>> activeNotifications;
NotificationController(); NotificationController();
std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors; std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors;
};
void closeNotification(uint64_t notificationId);
};

View File

@@ -1,24 +1,30 @@
#pragma once #pragma once
#include <filesystem> #include <csignal>
#include <string> #include <cstdint>
#include "helpers/system.hpp"
#include "gdkmm/monitor.h" #include "gdkmm/monitor.h"
#include "gtk4-layer-shell.h"
#include "gtkmm/cssprovider.h"
#include "gtkmm/window.h" #include "gtkmm/window.h"
#define DEFAULT_NOTIFICATION_TIMEOUT 7000 #define DEFAULT_NOTIFICATION_TIMEOUT 7000
class BaseNotification : public Gtk::Window { class BaseNotification : public Gtk::Window {
public: public:
BaseNotification(std::shared_ptr<Gdk::Monitor> monitor); BaseNotification( uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor);
sigc::signal<void(int)> signal_close;
virtual ~BaseNotification() = default; virtual ~BaseNotification() = default;
uint64_t getNotificationId() const {
return this->notificationId;
}
private: private:
void ensure_notification_css_loaded(); void ensure_notification_css_loaded();
// onClose signal can be added here if needed
protected:
uint64_t notificationId;
}; };

View File

@@ -1,11 +1,13 @@
#pragma once #pragma once
#include <cstdint>
#include "services/dbus/messages.hpp" #include "services/dbus/messages.hpp"
#include "widgets/notification/baseNotification.hpp" #include "widgets/notification/baseNotification.hpp"
class NotificationWindow : public BaseNotification { class NotificationWindow : public BaseNotification {
public: public:
NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage message); NotificationWindow(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage message);
virtual ~NotificationWindow() = default; virtual ~NotificationWindow() = default;
}; };

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstdint>
#include <memory> #include <memory>
#include "services/dbus/messages.hpp" #include "services/dbus/messages.hpp"
@@ -9,7 +10,7 @@
class SpotifyNotification : public BaseNotification { class SpotifyNotification : public BaseNotification {
public: public:
SpotifyNotification(std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message message); SpotifyNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message message);
virtual ~SpotifyNotification() = default; virtual ~SpotifyNotification() = default;
private: private:

View File

@@ -70,3 +70,20 @@
.notification-button:hover { .notification-button:hover {
background-color: #555555; background-color: #555555;
} }
.notification-app-icon {
padding-right: 8px;
border-radius: 4px;
border: 1px solid var(--color-border);
}
.notification-app-name {
font-weight: 600;
font-size: 12px;
color: #ffffff;
}
.notification-header {
margin-bottom: 6px;
border-bottom: 1px solid var(--color-border);
}

View File

@@ -3,12 +3,13 @@
#include <memory> #include <memory>
#include "services/dbus/messages.hpp" #include "services/dbus/messages.hpp"
#include "widgets/notification/baseNotification.hpp"
#include "widgets/notification/notificationWindow.hpp" #include "widgets/notification/notificationWindow.hpp"
#include "widgets/notification/spotifyNotification.hpp" #include "widgets/notification/spotifyNotification.hpp"
#include "gdkmm/display.h" #include "gdkmm/display.h"
#include "glibmm/main.h" #include "glibmm/main.h"
#include "sigc++/adaptors/bind.h"
std::shared_ptr<NotificationController> NotificationController::instance = nullptr; std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
@@ -38,9 +39,19 @@ NotificationController::NotificationController() {
void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) { void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) {
return; return;
std::vector<std::shared_ptr<BaseNotification>> notifications;
uint64_t id = this->globalNotificationId++;
for (const auto &monitor : this->activeMonitors) { for (const auto &monitor : this->activeMonitors) {
auto notification = std::make_shared<SpotifyNotification>(monitor, mpris); auto notification = std::make_shared<SpotifyNotification>(id, monitor, mpris);
notification->show(); notification->show();
notifications.push_back(notification);
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
closeNotification(id);
});
this->activeNotifications[id] = notifications;
Glib::signal_timeout().connect([notification]() { Glib::signal_timeout().connect([notification]() {
notification->close(); notification->close();
@@ -48,11 +59,17 @@ void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris)
}, },
DEFAULT_NOTIFICATION_TIMEOUT); DEFAULT_NOTIFICATION_TIMEOUT);
} }
this->activeNotifications[id] = notifications;
} }
void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) { void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) {
uint64_t id = this->globalNotificationId++;
std::vector<std::shared_ptr<BaseNotification>> notifications;
for (const auto &monitor : this->activeMonitors) { for (const auto &monitor : this->activeMonitors) {
auto notification = std::make_shared<NotificationWindow>(monitor, notify); auto notification = std::make_shared<NotificationWindow>(id, monitor, notify);
notifications.push_back(notification);
auto timeout = notify.expire_timeout; auto timeout = notify.expire_timeout;
notification->show(); notification->show();
@@ -61,6 +78,11 @@ void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify)
timeout = DEFAULT_NOTIFICATION_TIMEOUT; timeout = DEFAULT_NOTIFICATION_TIMEOUT;
} }
notification->signal_close.connect(
[this, id = notification->getNotificationId()](int) {
closeNotification(id);
});
if (timeout == 0) { if (timeout == 0) {
continue; continue;
} }
@@ -71,4 +93,19 @@ void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify)
}, },
timeout); timeout);
} }
this->activeNotifications[id] = notifications;
}
void NotificationController::closeNotification(uint64_t notificationId) {
if (this->activeNotifications.find(notificationId) == this->activeNotifications.end()) {
return;
}
auto notifications = this->activeNotifications[notificationId];
for (const auto &notification : notifications) {
notification->close();
}
this->activeNotifications.erase(notificationId);
} }

View File

@@ -9,7 +9,7 @@
#include "gtk4-layer-shell.h" #include "gtk4-layer-shell.h"
#include "gtkmm/cssprovider.h" #include "gtkmm/cssprovider.h"
BaseNotification::BaseNotification(std::shared_ptr<Gdk::Monitor> monitor) { BaseNotification::BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor) {
ensure_notification_css_loaded(); ensure_notification_css_loaded();
set_default_size(350, -1); set_default_size(350, -1);
gtk_layer_init_for_window(gobj()); gtk_layer_init_for_window(gobj());
@@ -23,6 +23,8 @@ BaseNotification::BaseNotification(std::shared_ptr<Gdk::Monitor> monitor) {
this->set_hexpand(false); this->set_hexpand(false);
this->set_vexpand(false); this->set_vexpand(false);
this->notificationId = notificationId;
} }
void BaseNotification::ensure_notification_css_loaded() { void BaseNotification::ensure_notification_css_loaded() {

View File

@@ -1,25 +1,34 @@
#include "widgets/notification/notificationWindow.hpp" #include "widgets/notification/notificationWindow.hpp"
#include <cstdint>
#include <sys/types.h>
#include "helpers/string.hpp" #include "helpers/string.hpp"
#include "gtkmm/box.h" #include "gtkmm/box.h"
#include "gtkmm/button.h" #include "gtkmm/button.h"
#include "gtkmm/gestureclick.h"
#include "gtkmm/image.h" #include "gtkmm/image.h"
#include "gtkmm/label.h" #include "gtkmm/label.h"
NotificationWindow::NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage notify) : BaseNotification(monitor) { NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage notify) : BaseNotification(notificationId, monitor) {
set_title(notify.summary); set_title(notify.summary);
if (notify.imageData) { auto window_click = Gtk::GestureClick::create();
auto img = Gtk::make_managed<Gtk::Image>(*(notify.imageData)); window_click->set_button(GDK_BUTTON_PRIMARY);
img->set_pixel_size(64); window_click->set_exclusive(false);
img->set_halign(Gtk::Align::CENTER); window_click->signal_released().connect([this](int, double x, double y) {
img->set_valign(Gtk::Align::CENTER); Gtk::Widget *picked = this->pick(x, y, Gtk::PickFlags::DEFAULT);
img->add_css_class("notification-image"); for (auto *w = picked; w != nullptr; w = w->get_parent()) {
set_child(*img); if (dynamic_cast<Gtk::Button *>(w) != nullptr) {
return;
} }
}
this->signal_close.emit(this->notificationId);
});
add_controller(window_click);
// Main vertical box // Main vertical box
auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 8); auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 8);
vbox->set_halign(Gtk::Align::FILL);
switch (notify.urgency) { switch (notify.urgency) {
case NotificationUrgency::CRITICAL: case NotificationUrgency::CRITICAL:
@@ -31,10 +40,30 @@ NotificationWindow::NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, No
case NotificationUrgency::LOW: case NotificationUrgency::LOW:
add_css_class("notification-low"); add_css_class("notification-low");
break; break;
default:
break;
} }
auto header_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
header_box->set_halign(Gtk::Align::START);
header_box->add_css_class("notification-header");
header_box->set_hexpand(true);
header_box->set_halign(Gtk::Align::FILL);
if (notify.imageData) {
auto img = Gtk::make_managed<Gtk::Image>(*(notify.imageData));
img->set_pixel_size(16);
img->set_halign(Gtk::Align::START);
img->set_valign(Gtk::Align::CENTER);
img->add_css_class("notification-app-icon");
header_box->append(*img);
}
auto app_label = Gtk::make_managed<Gtk::Label>(StringHelper::trimToSize(notify.app_name, 24));
app_label->set_halign(Gtk::Align::START);
app_label->set_wrap(false);
app_label->add_css_class("notification-app-name");
header_box->append(*app_label);
vbox->append(*header_box);
// Summary label // Summary label
auto summary_label = Gtk::make_managed<Gtk::Label>("<b>" + StringHelper::trimToSize(notify.summary, 20) + "</b>"); auto summary_label = Gtk::make_managed<Gtk::Label>("<b>" + StringHelper::trimToSize(notify.summary, 20) + "</b>");
summary_label->set_use_markup(true); summary_label->set_use_markup(true);
@@ -62,7 +91,7 @@ NotificationWindow::NotificationWindow(std::shared_ptr<Gdk::Monitor> monitor, No
btn->signal_clicked().connect([this, action_id, cb = notify.on_action]() { btn->signal_clicked().connect([this, action_id, cb = notify.on_action]() {
if (cb) { if (cb) {
cb(action_id); cb(action_id);
this->close(); this->signal_close.emit(this->notificationId);
} }
}); });
actions_box->append(*btn); actions_box->append(*btn);

View File

@@ -1,4 +1,5 @@
#include "widgets/notification/spotifyNotification.hpp" #include "widgets/notification/spotifyNotification.hpp"
#include <sys/types.h>
#include "helpers/string.hpp" #include "helpers/string.hpp"
#include "services/textureCache.hpp" #include "services/textureCache.hpp"
@@ -9,7 +10,7 @@
#include "gtkmm/image.h" #include "gtkmm/image.h"
#include "gtkmm/label.h" #include "gtkmm/label.h"
SpotifyNotification::SpotifyNotification(std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message mpris) : BaseNotification(monitor) { SpotifyNotification::SpotifyNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, MprisPlayer2Message mpris) : BaseNotification(notificationId, monitor) {
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10); auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
container->set_hexpand(true); container->set_hexpand(true);
@@ -59,7 +60,7 @@ std::unique_ptr<Gtk::CenterBox> SpotifyNotification::createButtonBox(MprisPlayer
backButton->signal_clicked().connect([this, mpris]() { backButton->signal_clicked().connect([this, mpris]() {
if (mpris.previous) { if (mpris.previous) {
mpris.previous(); mpris.previous();
this->close(); this->signal_close.emit(this->notificationId);
} }
}); });
@@ -86,7 +87,7 @@ std::unique_ptr<Gtk::CenterBox> SpotifyNotification::createButtonBox(MprisPlayer
nextButton->signal_clicked().connect([this, mpris]() { nextButton->signal_clicked().connect([this, mpris]() {
if (mpris.next) { if (mpris.next) {
mpris.next(); mpris.next();
this->close(); this->signal_close.emit(this->notificationId);
} }
}); });
buttonBox->set_start_widget(*backButton); buttonBox->set_start_widget(*backButton);