diff --git a/src/knotification.h b/src/knotification.h --- a/src/knotification.h +++ b/src/knotification.h @@ -28,6 +28,7 @@ #include #include #include +#include class QWidget; @@ -263,6 +264,16 @@ */ SkipGrouping = 0x10, + /** + * The notification will be automatically closed if the window() becomes + * activated. + * + * You need to set a window using setWindow(). + * + * @since 5.71 + */ + CloseWhenWindowActivated = 0x20, + /** * @internal * The event is a standard kde event, and not an event of the application @@ -556,6 +567,22 @@ */ void setUrgency(Urgency urgency); + /** + * Sets the window associated with this notification. + * This is relevant when using the CloseWhenWindowActivated flag. + * + * @since 5.71 + */ + void setWindow(QWindow *window); + + /** + * The window associated with this notification. nullptr by default. + * @return the window set by setWindow() + * + * @since 5.71 + */ + QWindow *window() const; + /** * @internal * the id given by the notification manager diff --git a/src/knotification.cpp b/src/knotification.cpp --- a/src/knotification.cpp +++ b/src/knotification.cpp @@ -58,6 +58,7 @@ QTimer updateTimer; bool needUpdate; + QWindow *window = nullptr; Private() : id(-1), ref(0), widget(nullptr), urgency(KNotification::DefaultUrgency), needUpdate(false) {} #if KNOTIFICATIONS_BUILD_DEPRECATED_SINCE(5, 67) @@ -575,3 +576,21 @@ return d->hints; } +void KNotification::setWindow(QWindow *window) +{ + if (window == d->window) { + return; + } + + d->window = window; + connect(window, &QWindow::activeChanged, this, [this, window]{ + if (window->isActive() && (d->flags & CloseWhenWidgetActivated)) { + close(); + } + }); +} + +QWindow *KNotification::window() const +{ + return d->window; +}