diff --git a/changelog/next.md b/changelog/next.md index 83c1ae070..880a7d281 100644 --- a/changelog/next.md +++ b/changelog/next.md @@ -1,6 +1,8 @@ ## Other Changes - `SystemTrayItem.activate()` now forwards Wayland activation tokens to applications. +- `NotificationAction.invoke()` now sends a Wayland activation token to the notifying application. +- Added `Quickshell.requestActivationToken()` to get a Wayland activation token for the latest input, e.g. to pass to a launched app. ## Bug Fixes diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4824965b5..83d6bc359 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -61,6 +61,11 @@ qs_module_pch(quickshell-core SET large) target_link_libraries(quickshell PRIVATE quickshell-coreplugin) +if (WAYLAND) + target_compile_definitions(quickshell-core PRIVATE QS_WAYLAND) + target_link_libraries(quickshell-core PRIVATE quickshell-wayland-activation) +endif() + if (BUILD_TESTING) add_subdirectory(test) endif() diff --git a/src/core/qmlglobal.cpp b/src/core/qmlglobal.cpp index 14991be59..7149af02f 100644 --- a/src/core/qmlglobal.cpp +++ b/src/core/qmlglobal.cpp @@ -8,9 +8,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -34,6 +36,10 @@ #include "rootwrapper.hpp" #include "scanenv.hpp" +#ifdef QS_WAYLAND +#include "../wayland/activation.hpp" +#endif + QuickshellSettings::QuickshellSettings() { QObject::connect( static_cast(QGuiApplication::instance()), // NOLINT @@ -330,6 +336,26 @@ void QuickshellGlobal::execDetached(const qs::io::process::ProcessContext& conte process.startDetached(); } +void QuickshellGlobal::requestActivationToken(const QJSValue& callback) { + if (!callback.isCallable()) { + qCritical() << "requestActivationToken called with a non-function callback:" + << callback.toString(); + return; + } + + // The engine may be gone by the time the token arrives, e.g. after a reload. + const auto call = [self = QPointer(this), callback](const QString& token) { + if (!self) return; + const auto result = callback.call({token}); + if (result.isError()) qCritical() << "Error in activation token callback:" << result.toString(); + }; + +#ifdef QS_WAYLAND + if (qs::wayland::activation::requestActivationToken(call)) return; +#endif + call({}); +} + QString QuickshellGlobal::iconPath(const QString& icon) { return IconImageProvider::requestString(icon); } diff --git a/src/core/qmlglobal.hpp b/src/core/qmlglobal.hpp index 5fe2aa907..3c2a41681 100644 --- a/src/core/qmlglobal.hpp +++ b/src/core/qmlglobal.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -208,6 +209,21 @@ class QuickshellGlobal: public QObject { /// This function is equivalent to @@Quickshell.Io.Process.startDetached(). Q_INVOKABLE static void execDetached(const qs::io::process::ProcessContext& context); + /// Requests an xdg-activation token for the most recent input and calls `callback` with it, + /// or with an empty string if one can't be requested. The callback is called exactly once, + /// and may be called before this function returns. + /// + /// Pass the token to a launched program as `XDG_ACTIVATION_TOKEN` so the program can raise its + /// window for the input that launched it, e.g. an app that is already running. + /// + /// ```qml + /// Quickshell.requestActivationToken(token => { + /// const env = token ? ["env", `XDG_ACTIVATION_TOKEN=${token}`] : []; + /// Quickshell.execDetached([...env, "myapp"]); + /// }); + /// ``` + Q_INVOKABLE void requestActivationToken(const QJSValue& callback); + /// Returns a string usable for a @@QtQuick.Image.source for a given system icon. /// /// > [!INFO] By default, icons are loaded from the theme selected by the qt platform theme, diff --git a/src/services/notifications/CMakeLists.txt b/src/services/notifications/CMakeLists.txt index 57f83764f..425b1990e 100644 --- a/src/services/notifications/CMakeLists.txt +++ b/src/services/notifications/CMakeLists.txt @@ -29,3 +29,10 @@ target_link_libraries(quickshell-service-notifications PRIVATE Qt::Quick Qt::DBu target_link_libraries(quickshell PRIVATE quickshell-service-notificationsplugin) qs_module_pch(quickshell-service-notifications SET dbus) + +if (WAYLAND) + target_compile_definitions(quickshell-service-notifications PRIVATE QS_WAYLAND) + target_link_libraries(quickshell-service-notifications PRIVATE + quickshell-wayland-activation + ) +endif() diff --git a/src/services/notifications/notification.cpp b/src/services/notifications/notification.cpp index b58087f31..94cc8466c 100644 --- a/src/services/notifications/notification.cpp +++ b/src/services/notifications/notification.cpp @@ -1,4 +1,5 @@ #include "notification.hpp" +#include #include #include @@ -6,7 +7,9 @@ #include #include #include +#include #include +#include #include #include @@ -16,6 +19,10 @@ #include "dbusimage.hpp" #include "server.hpp" +#ifdef QS_WAYLAND +#include "../../wayland/activation.hpp" +#endif + namespace qs::service::notifications { // NOLINTNEXTLINE(misc-use-internal-linkage) @@ -48,11 +55,31 @@ void NotificationAction::invoke() { return; } - NotificationServer::instance()->ActionInvoked(this->notification->id(), this->mIdentifier); + this->notification->beginInvoke(); + +#ifdef QS_WAYLAND + // The token has to go out before ActionInvoked, so hold the action until the compositor sends it. + const auto callback = [action = QPointer(this)](const QString& token) { + if (action) action->sendInvoked(token); + }; + + if (qs::wayland::activation::requestActivationToken(callback)) return; +#endif + this->sendInvoked({}); +} + +void NotificationAction::sendInvoked(const QString& token) { + if (this->notification->isRetained()) return; + + auto* server = NotificationServer::instance(); + if (!token.isEmpty()) server->ActivationToken(this->notification->id(), token); + server->ActionInvoked(this->notification->id(), this->mIdentifier); if (!this->notification->bindableResident().value()) { this->notification->close(NotificationCloseReason::Dismissed); } + + this->notification->finishInvoke(); } void NotificationAction::setText(const QString& text) { @@ -71,6 +98,11 @@ void Notification::close(NotificationCloseReason::Enum reason) { return; } + if (reason != 0 && this->mPendingInvokes > 0) { + this->mDeferredCloseReason = reason; + return; + } + this->mCloseReason = reason; if (reason != 0) { @@ -78,6 +110,15 @@ void Notification::close(NotificationCloseReason::Enum reason) { } } +void Notification::beginInvoke() { this->mPendingInvokes++; } + +void Notification::finishInvoke() { + if (--this->mPendingInvokes > 0 || this->mDeferredCloseReason == 0) return; + this->close( + std::exchange(this->mDeferredCloseReason, static_cast(0)) + ); +} + void Notification::sendInlineReply(const QString& replyText) { if (!NotificationServer::instance()->support.inlineReply) { qCritical() << "Inline reply support disabled on server"; diff --git a/src/services/notifications/notification.hpp b/src/services/notifications/notification.hpp index 7f5246c1c..9638ded89 100644 --- a/src/services/notifications/notification.hpp +++ b/src/services/notifications/notification.hpp @@ -148,6 +148,11 @@ class Notification void close(NotificationCloseReason::Enum reason); + // Closes requested between these calls wait for finishInvoke, so the client + // gets ActionInvoked before NotificationClosed. + void beginInvoke(); + void finishInvoke(); + [[nodiscard]] quint32 id() const; [[nodiscard]] bool isTracked() const; @@ -207,6 +212,9 @@ class Notification private: quint32 mId; NotificationCloseReason::Enum mCloseReason = NotificationCloseReason::Dismissed; + qsizetype mPendingInvokes = 0; + NotificationCloseReason::Enum mDeferredCloseReason = + static_cast(0); bool mLastGeneration = false; NotificationImage mImagePixmap; QList mActions; @@ -268,6 +276,8 @@ class NotificationAction: public QObject { void textChanged(); private: + void sendInvoked(const QString& token); + Notification* notification; QString mIdentifier; QString mText; diff --git a/src/services/notifications/server.hpp b/src/services/notifications/server.hpp index 8bd92a346..35dc9de6a 100644 --- a/src/services/notifications/server.hpp +++ b/src/services/notifications/server.hpp @@ -61,6 +61,7 @@ class NotificationServer: public QObject { // NOLINTBEGIN void NotificationClosed(quint32 id, quint32 reason); void ActionInvoked(quint32 id, QString action); + void ActivationToken(quint32 id, QString activationToken); void NotificationReplied(quint32 id, QString replyText); // NOLINTEND diff --git a/src/wayland/activation.cpp b/src/wayland/activation.cpp index 01369d282..1aef58fd4 100644 --- a/src/wayland/activation.cpp +++ b/src/wayland/activation.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -29,14 +28,15 @@ class ActivationManager ActivationManager(): QWaylandClientExtensionTemplate(1) { this->initialize(); } }; +// One token request with its own callback. It isn't owned by the window, so the token still +// arrives if the window is closed or destroyed before the compositor answers. class ActivationToken : public QObject , public QtWayland::xdg_activation_token_v1 { public: - ActivationToken(::xdg_activation_token_v1* token, QtWaylandClient::QWaylandWindow* window) - : QObject(window) - , QtWayland::xdg_activation_token_v1(token) - , window(window) {} + ActivationToken(::xdg_activation_token_v1* token, std::function callback) + : QtWayland::xdg_activation_token_v1(token) + , callback(std::move(callback)) {} ~ActivationToken() override { this->destroy(); } Q_DISABLE_COPY_MOVE(ActivationToken); @@ -44,12 +44,29 @@ class ActivationToken private: void xdg_activation_token_v1_done(const QString& token) override { this->deleteLater(); - if (this->window) emit this->window->xdgActivationTokenCreated(token); + this->callback(token); } - QPointer window; + std::function callback; }; +bool sendRequest( + QtWaylandClient::QWaylandWindow* window, + quint32 serial, + std::function callback +) { + auto* seat = window->display()->lastInputDevice(); + + static auto* manager = new ActivationManager(); // NOLINT + if (!manager->isActive() || !window->surface() || !seat) return false; + + auto* token = new ActivationToken(manager->get_activation_token(), std::move(callback)); + token->set_serial(serial, seat->object()); + token->set_surface(window->surface()); + token->commit(); + return true; +} + } // namespace bool requestActivationToken(std::function callback) { @@ -57,33 +74,17 @@ bool requestActivationToken(std::function callback) { auto* display = QtWaylandClient::QWaylandIntegration::instance()->display(); auto* window = display->lastInputWindow(); - if (!window || !window->surface() || !display->lastInputDevice()) return false; - - QObject::connect( - window, - &QtWaylandClient::QWaylandWindow::xdgActivationTokenCreated, - window, - std::move(callback), - Qt::SingleShotConnection - ); - window->requestXdgActivationToken(display->lastInputSerial()); - return true; + if (!window) return false; + + return sendRequest(window, display->lastInputSerial(), std::move(callback)); } void requestXdgActivationToken(QtWaylandClient::QWaylandWindow* window, quint32 serial) { - auto* display = window->display(); - auto* seat = display->lastInputDevice(); - - static auto* manager = new ActivationManager(); // NOLINT - if (!manager->isActive() || !window->surface() || !seat) { - emit window->xdgActivationTokenCreated({}); - return; - } + const auto callback = [window = QPointer(window)](const QString& token) { + if (window) emit window->xdgActivationTokenCreated(token); + }; - auto* token = new ActivationToken(manager->get_activation_token(), window); - token->set_serial(serial, seat->object()); - token->set_surface(window->surface()); - token->commit(); + if (!sendRequest(window, serial, callback)) emit window->xdgActivationTokenCreated({}); } } // namespace qs::wayland::activation