#include "services/notificationController.hpp" #include #include "services/dbus/messages.hpp" #include "widgets/notification/notification.hpp" #include "widgets/notification/spotifyNotification.hpp" #include "gdkmm/display.h" #include "glibmm/main.h" #define DEFAULT_NOTIFICATION_TIMEOUT 4000 std::shared_ptr NotificationController::instance = nullptr; NotificationController::NotificationController() { if (NotificationController::instance) { throw std::runtime_error("use getInstance()!"); } auto display = Gdk::Display::get_default(); if (!display) { return; } auto monitors = display->get_monitors(); if (!monitors) { return; } for (guint i = 0; i < monitors->get_n_items(); ++i) { auto monitor = std::dynamic_pointer_cast( monitors->get_object(i)); this->activeMonitors.push_back(monitor); } } void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) { for (const auto &monitor : this->activeMonitors) { auto notification = std::make_shared(monitor, mpris); notification->show(); Glib::signal_timeout().connect([notification]() { notification->close(); return false; // Don't repeat }, DEFAULT_NOTIFICATION_TIMEOUT); } } void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) { for (const auto &monitor : this->activeMonitors) { auto notification = std::make_shared(monitor, notify); 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 } Glib::signal_timeout().connect([notification]() { notification->close(); return false; // Don't repeat }, timeout); } }