139 lines
4.0 KiB
C++
139 lines
4.0 KiB
C++
#include "services/notificationController.hpp"
|
|
|
|
#include <memory>
|
|
|
|
#include "services/dbus/messages.hpp"
|
|
#include "widgets/notification/baseNotification.hpp"
|
|
#include "widgets/notification/notificationWindow.hpp"
|
|
#include "widgets/notification/spotifyNotification.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "gdkmm/display.h"
|
|
#include "sigc++/adaptors/bind.h"
|
|
|
|
std::shared_ptr<NotificationController> 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<Gdk::Monitor>(
|
|
monitors->get_object(i));
|
|
|
|
this->activeMonitors.push_back(monitor);
|
|
}
|
|
}
|
|
|
|
|
|
void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify) {
|
|
uint64_t id = this->globalNotificationId++;
|
|
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
|
|
|
for (const auto &monitor : this->activeMonitors) {
|
|
auto notification = std::make_shared<NotificationWindow>(id, monitor, notify);
|
|
notifications.push_back(notification);
|
|
|
|
auto timeout = notify.expire_timeout;
|
|
notification->show();
|
|
// -1 means use default timeout, 0 means never expire
|
|
if (timeout < 0) {
|
|
timeout = DEFAULT_NOTIFICATION_TIMEOUT;
|
|
}
|
|
|
|
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
|
|
closeNotification(id);
|
|
});
|
|
|
|
notification->signal_hover_changed.connect([this, id = notification->getNotificationId()](bool hovered) {
|
|
updateHoverState(id, hovered);
|
|
});
|
|
|
|
if (timeout == 0) {
|
|
continue;
|
|
}
|
|
|
|
notification->startAutoClose(timeout);
|
|
}
|
|
|
|
this->activeNotifications[id] = notifications;
|
|
|
|
}
|
|
|
|
|
|
void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris) {
|
|
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
|
uint64_t id = this->globalNotificationId++;
|
|
|
|
|
|
for (const auto &monitor : this->activeMonitors) {
|
|
auto notification = std::make_shared<SpotifyNotification>(id, monitor, mpris);
|
|
|
|
notification->show();
|
|
notifications.push_back(notification);
|
|
|
|
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
|
|
closeNotification(id);
|
|
});
|
|
|
|
notification->signal_hover_changed.connect([this, id = notification->getNotificationId()](bool hovered) {
|
|
updateHoverState(id, hovered);
|
|
});
|
|
|
|
|
|
notification->startAutoClose(10000);
|
|
}
|
|
|
|
this->activeNotifications[id] = notifications;
|
|
|
|
}
|
|
|
|
void NotificationController::updateHoverState(uint64_t notificationId, bool isHovered) {
|
|
if (this->activeNotifications.find(notificationId) == this->activeNotifications.end()) {
|
|
return;
|
|
}
|
|
|
|
int &count = this->hoverCounts[notificationId];
|
|
if (isHovered) {
|
|
count += 1;
|
|
} else {
|
|
count = std::max(0, count - 1);
|
|
}
|
|
|
|
auto ¬ifications = this->activeNotifications[notificationId];
|
|
if (count > 0) {
|
|
for (const auto ¬ification : notifications) {
|
|
notification->pauseAutoClose();
|
|
}
|
|
} else {
|
|
for (const auto ¬ification : notifications) {
|
|
notification->resumeAutoClose();
|
|
}
|
|
}
|
|
}
|
|
|
|
void NotificationController::closeNotification(uint64_t notificationId) {
|
|
if (this->activeNotifications.find(notificationId) == this->activeNotifications.end()) {
|
|
return;
|
|
}
|
|
|
|
auto notifications = this->activeNotifications[notificationId];
|
|
for (const auto ¬ification : notifications) {
|
|
notification->close();
|
|
}
|
|
|
|
this->activeNotifications.erase(notificationId);
|
|
this->hoverCounts.erase(notificationId);
|
|
} |