hover pauses notification
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
#include "widgets/notification/baseNotification.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "helpers/system.hpp"
|
||||
|
||||
#include "gdkmm/monitor.h"
|
||||
#include "glibmm/main.h"
|
||||
#include "glibmm/object.h"
|
||||
#include "gtk4-layer-shell.h"
|
||||
#include "gtkmm/button.h"
|
||||
#include "gtkmm/cssprovider.h"
|
||||
#include "gtkmm/eventcontrollermotion.h"
|
||||
#include "gtkmm/gestureclick.h"
|
||||
|
||||
BaseNotification::BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor) {
|
||||
ensure_notification_css_loaded();
|
||||
@@ -25,8 +31,94 @@ BaseNotification::BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk:
|
||||
this->set_vexpand(false);
|
||||
|
||||
this->notificationId = notificationId;
|
||||
|
||||
auto window_click = Gtk::GestureClick::create();
|
||||
window_click->set_button(GDK_BUTTON_PRIMARY);
|
||||
window_click->set_exclusive(false);
|
||||
window_click->signal_released().connect([this](int, double x, double y) {
|
||||
Gtk::Widget *picked = this->pick(x, y, Gtk::PickFlags::DEFAULT);
|
||||
for (auto *w = picked; w != nullptr; w = w->get_parent()) {
|
||||
if (dynamic_cast<Gtk::Button *>(w) != nullptr) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->signal_close.emit(this->notificationId);
|
||||
});
|
||||
add_controller(window_click);
|
||||
|
||||
auto window_motion = Gtk::EventControllerMotion::create();
|
||||
window_motion->signal_enter().connect([this](double, double) {
|
||||
signal_hover_changed.emit(true);
|
||||
pause_auto_close();
|
||||
});
|
||||
window_motion->signal_leave().connect([this]() {
|
||||
signal_hover_changed.emit(false);
|
||||
resume_auto_close();
|
||||
});
|
||||
add_controller(window_motion);
|
||||
}
|
||||
|
||||
void BaseNotification::pauseAutoClose() {
|
||||
pause_auto_close();
|
||||
}
|
||||
|
||||
void BaseNotification::resumeAutoClose() {
|
||||
resume_auto_close();
|
||||
}
|
||||
|
||||
void BaseNotification::startAutoClose(int timeoutMs) {
|
||||
if (timeoutMs <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
autoCloseRemainingMs = timeoutMs;
|
||||
autoClosePaused = false;
|
||||
start_auto_close_timeout(timeoutMs);
|
||||
}
|
||||
|
||||
void BaseNotification::start_auto_close_timeout(int timeoutMs) {
|
||||
if (autoCloseConnection.connected()) {
|
||||
autoCloseConnection.disconnect();
|
||||
}
|
||||
|
||||
autoCloseDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||
autoCloseConnection = Glib::signal_timeout().connect([this]() {
|
||||
this->signal_close.emit(this->notificationId);
|
||||
return false; // Don't repeat
|
||||
},
|
||||
timeoutMs);
|
||||
}
|
||||
|
||||
void BaseNotification::pause_auto_close() {
|
||||
if (autoClosePaused || autoCloseRemainingMs <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (autoCloseConnection.connected()) {
|
||||
autoCloseConnection.disconnect();
|
||||
}
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(autoCloseDeadline - now).count();
|
||||
autoCloseRemainingMs = static_cast<int>(std::max<int64_t>(0, remaining));
|
||||
autoClosePaused = true;
|
||||
}
|
||||
|
||||
void BaseNotification::resume_auto_close() {
|
||||
if (!autoClosePaused) {
|
||||
return;
|
||||
}
|
||||
|
||||
autoClosePaused = false;
|
||||
if (autoCloseRemainingMs <= 0) {
|
||||
this->signal_close.emit(this->notificationId);
|
||||
return;
|
||||
}
|
||||
|
||||
start_auto_close_timeout(autoCloseRemainingMs);
|
||||
}
|
||||
|
||||
|
||||
void BaseNotification::ensure_notification_css_loaded() {
|
||||
static bool loaded = false;
|
||||
if (loaded) {
|
||||
|
||||
@@ -12,19 +12,6 @@
|
||||
NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor, NotifyMessage notify) : BaseNotification(notificationId, monitor) {
|
||||
set_title(notify.summary);
|
||||
|
||||
auto window_click = Gtk::GestureClick::create();
|
||||
window_click->set_button(GDK_BUTTON_PRIMARY);
|
||||
window_click->set_exclusive(false);
|
||||
window_click->signal_released().connect([this](int, double x, double y) {
|
||||
Gtk::Widget *picked = this->pick(x, y, Gtk::PickFlags::DEFAULT);
|
||||
for (auto *w = picked; w != nullptr; w = w->get_parent()) {
|
||||
if (dynamic_cast<Gtk::Button *>(w) != nullptr) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->signal_close.emit(this->notificationId);
|
||||
});
|
||||
add_controller(window_click);
|
||||
|
||||
// Main vertical box
|
||||
auto vbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::VERTICAL, 8);
|
||||
@@ -88,6 +75,19 @@ NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<
|
||||
btn->set_label(action_label);
|
||||
|
||||
btn->add_css_class("notification-button");
|
||||
|
||||
switch (notify.urgency) {
|
||||
case NotificationUrgency::CRITICAL:
|
||||
btn->add_css_class("notification-critical");
|
||||
break;
|
||||
case NotificationUrgency::NORMAL:
|
||||
btn->add_css_class("notification-normal");
|
||||
break;
|
||||
case NotificationUrgency::LOW:
|
||||
btn->add_css_class("notification-low");
|
||||
break;
|
||||
}
|
||||
|
||||
btn->signal_clicked().connect([this, action_id, cb = notify.on_action]() {
|
||||
if (cb) {
|
||||
cb(action_id);
|
||||
|
||||
Reference in New Issue
Block a user