diff --git a/shell/panelview.cpp b/shell/panelview.cpp index d4b2304b5..44601974c 100644 --- a/shell/panelview.cpp +++ b/shell/panelview.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #if HAVE_X11 @@ -1400,11 +1401,31 @@ void PanelView::refreshStatus(Plasma::Types::ItemStatus status) } } else if (status == Plasma::Types::AcceptingInputStatus) { setFlags(flags() & ~Qt::WindowDoesNotAcceptFocus); +#ifdef HAVE_X11 + if (KWindowSystem::isPlatformX11()) { + m_previousWId = KWindowSystem::activeWindow(); + } KWindowSystem::forceActiveWindow(winId()); +#endif if (m_shellSurface) { + m_previousPlasmaWindow = m_corona->waylandPlasmaWindowManagementInterface()->activeWindow(); m_shellSurface->setPanelTakesFocus(true); } } else { + if (status == Plasma::Types::PassiveStatus) { + // Restores the previous focus +#ifdef HAVE_X11 + if (KWindowSystem::isPlatformX11() && m_previousWId) { + KWindowSystem::forceActiveWindow(m_previousWId); + m_previousWId = 0; + } +#endif + if (m_previousPlasmaWindow) { + m_previousPlasmaWindow->requestActivate(); + m_previousPlasmaWindow = nullptr; + } + } + restoreAutoHide(); setFlags(flags() | Qt::WindowDoesNotAcceptFocus); if (m_shellSurface) { diff --git a/shell/panelview.h b/shell/panelview.h index d5072a06b..68dc14cfa 100644 --- a/shell/panelview.h +++ b/shell/panelview.h @@ -9,6 +9,9 @@ #include #include #include +#ifdef HAVE_X11 +#include // For WId +#endif #include #include @@ -20,6 +23,7 @@ namespace KWayland namespace Client { class PlasmaShellSurface; +class PlasmaWindow; } } @@ -279,5 +283,11 @@ private: QPointer m_screenToFollow; QMetaObject::Connection m_transientWindowVisibleWatcher; + // Used to restore the previous activated window after the panel loses focus +#ifdef HAVE_X11 + WId m_previousWId = 0; +#endif + KWayland::Client::PlasmaWindow *m_previousPlasmaWindow = nullptr; + static const int STRUTSTIMERDELAY = 200; }; diff --git a/shell/shellcorona.cpp b/shell/shellcorona.cpp index faa4caff4..10eb0e94e 100644 --- a/shell/shellcorona.cpp +++ b/shell/shellcorona.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include "config-ktexteditor.h" // HAVE_KTEXTEDITOR @@ -2174,6 +2175,9 @@ void ShellCorona::setupWaylandIntegration() connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) { m_waylandPlasmaShell = registry->createPlasmaShell(name, version, this); }); + connect(registry, &KWayland::Client::Registry::plasmaWindowManagementAnnounced, this, [this, registry](quint32 name, quint32 version) { + m_waylandWindowManagement = registry->createPlasmaWindowManagement(name, version, this); + }); registry->setup(); connection->roundtrip(); qApp->installEventFilter(new DismissPopupEventFilter(this)); @@ -2184,6 +2188,11 @@ KWayland::Client::PlasmaShell *ShellCorona::waylandPlasmaShellInterface() const return m_waylandPlasmaShell; } +KWayland::Client::PlasmaWindowManagement *ShellCorona::waylandPlasmaWindowManagementInterface() const +{ + return m_waylandWindowManagement; +} + ScreenPool *ShellCorona::screenPool() const { return m_screenPool; diff --git a/shell/shellcorona.h b/shell/shellcorona.h index 07da8f267..3f8a200ed 100644 --- a/shell/shellcorona.h +++ b/shell/shellcorona.h @@ -52,6 +52,7 @@ namespace KWayland namespace Client { class PlasmaShell; +class PlasmaWindowManagement; } } @@ -100,6 +101,7 @@ public: Plasma::Containment *createContainmentForActivity(const QString &activity, int screenNum); KWayland::Client::PlasmaShell *waylandPlasmaShellInterface() const; + KWayland::Client::PlasmaWindowManagement *waylandPlasmaWindowManagementInterface() const; ScreenPool *screenPool() const; @@ -268,6 +270,8 @@ private: QTimer m_invariantsTimer; #endif KWayland::Client::PlasmaShell *m_waylandPlasmaShell; + // For getting the active window on Wayland + KWayland::Client::PlasmaWindowManagement *m_waylandWindowManagement = nullptr; bool m_closingDown : 1; QString m_testModeLayout;