refactor notifications

This commit is contained in:
2026-02-01 16:10:42 +01:00
parent 17aef717b7
commit 178a4451d4
18 changed files with 449 additions and 236 deletions

View File

@@ -2,19 +2,15 @@
#include <memory>
#include "services/dbus/mpris.hpp"
#include "services/textureCache.hpp"
#include "services/dbus/messages.hpp"
#include "widgets/notification/notification.hpp"
#include "widgets/notification/spotifyNotification.hpp"
#include "gdkmm/display.h"
#include "giomm/listmodel.h"
#include "glibmm/main.h"
#include "gtk4-layer-shell.h"
#include "gtkmm/box.h"
#include "gtkmm/button.h"
#include "gtkmm/centerbox.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
#include "gtkmm/window.h"
#define DEFAULT_NOTIFICATION_TIMEOUT 4000
std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
@@ -41,112 +37,34 @@ NotificationController::NotificationController() {
}
}
void NotificationController::showSpotifyNotification(MprisController::MprisPlayer2Message mpris) {
void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) {
for (const auto &monitor : this->activeMonitors) {
auto notification = std::make_shared<SpotifyNotification>(monitor, mpris);
notification->show();
auto win = std::make_shared<Gtk::Window>();
win->set_title(mpris.title);
this->baseWindowSetup(win, monitor);
Glib::signal_timeout().connect([notification]() {
notification->close();
return false; // Don't repeat
},
DEFAULT_NOTIFICATION_TIMEOUT);
}
}
auto container = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 10);
void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) {
for (const auto &monitor : this->activeMonitors) {
auto notification = std::make_shared<NotificationWindow>(monitor, notify);
if (auto texture = TextureCacheService::getInstance()->getTexture(mpris.artwork_url)) {
auto img = Gtk::make_managed<Gtk::Image>(texture);
img->set_pixel_size(64);
container->append(*img);
auto timeout = notify.expire_timeout;
notification->show();
// -1 means use default timeout, 0 means never expire
if (timeout <= 0) {
timeout = DEFAULT_NOTIFICATION_TIMEOUT; // default to 3 seconds
}
auto rightArea = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 5);
rightArea->set_halign(Gtk::Align::CENTER);
rightArea->set_valign(Gtk::Align::CENTER);
// make sure rigfht area takes the remaining space
rightArea->set_hexpand(true);
// also set a max width
auto title_label = Gtk::make_managed<Gtk::Label>("<b>" + mpris.title + "</b>");
title_label->set_use_markup(true);
title_label->set_halign(Gtk::Align::START);
rightArea->append(*title_label);
auto artistLabel = Gtk::make_managed<Gtk::Label>(mpris.artist);
artistLabel->set_halign(Gtk::Align::CENTER);
rightArea->append(*artistLabel);
auto buttonBox = Gtk::make_managed<Gtk::CenterBox>();
buttonBox->add_css_class("notification-button-box");
buttonBox->set_hexpand(true);
auto backButton = Gtk::make_managed<Gtk::Button>("<");
backButton->add_css_class("flat-button");
auto playPauseButton = Gtk::make_managed<Gtk::Button>("=<");
playPauseButton->add_css_class("flat-button");
auto nextButton = Gtk::make_managed<Gtk::Button>(">");
nextButton->add_css_class("flat-button");
backButton->signal_clicked().connect([mpris]() {
if (mpris.previous) {
mpris.previous();
}
});
playPauseButton->signal_clicked().connect([mpris]() {
if (mpris.play_pause) {
mpris.play_pause();
}
});
nextButton->signal_clicked().connect([mpris]() {
if (mpris.next) {
mpris.next();
}
});
buttonBox->set_start_widget(*backButton);
buttonBox->set_center_widget(*playPauseButton);
buttonBox->set_end_widget(*nextButton);
rightArea->append(*buttonBox);
container->append(*rightArea);
win->set_child(*container);
win->show();
Glib::signal_timeout().connect([win]() {
win->close();
Glib::signal_timeout().connect([notification]() {
notification->close();
return false; // Don't repeat
},
3000);
}
}
void NotificationController::baseWindowSetup(std::shared_ptr<Gtk::Window> win, std::shared_ptr<Gdk::Monitor> monitor) {
win->set_default_size(300, 100);
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");
}
void NotificationController::showNotificationOnAllMonitors(const std::string &title, const std::string &message) {
for (const auto &monitor : this->activeMonitors) {
auto win = std::make_shared<Gtk::Window>();
win->set_title(title);
this->baseWindowSetup(win, monitor);
auto label = Gtk::make_managed<Gtk::Label>(message);
label->set_use_markup(true);
win->set_child(*label);
win->show();
Glib::signal_timeout().connect([win]() {
win->close();
return false; // Don't repeat
},
3000);
timeout);
}
}