Bug 449873 - Losing focus of active application window when switching
Summary: Losing focus of active application window when switching
Status: CONFIRMED
Alias: None
Product: kwin
Classification: Plasma
Component: activities (other bugs)
Version First Reported In: 5.24.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords: regression
: 457616 (view as bug list)
Depends on:
Blocks:
 
Reported: 2022-02-09 16:55 UTC by bastimeyer123
Modified: 2023-08-08 02:45 UTC (History)
6 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bastimeyer123 2022-02-09 16:55:12 UTC
SUMMARY
This started happening after the 5.24.0 upgrade today:
When switching activities with active application windows on the desktop, focus gets lost.

STEPS TO REPRODUCE
1. Have more than one activity and a keybinding for cycling through activities
2. Have applications running on the current desktop of each activity, eg. a text editor and a video player
3. Use keyboard on the currently focused application (working as expected)
4. Cycle through activities
5. Focus is lost and keyboard triggers KRunner input instead

SOFTWARE/OS VERSIONS
Linux: Arch Linux
KDE Plasma Version: 5.24.0
KDE Frameworks Version: 5.90.0
Qt Version: 5.15.2
Comment 1 Vlad Zahorodnii 2022-02-11 17:28:52 UTC
Perhaps it's related to wayland-related fixes.
Comment 2 bastimeyer123 2022-02-24 22:42:00 UTC
Has the source of this bug been found? The issue is still present after upgrading to 5.24.2.
Comment 3 bastimeyer123 2022-04-20 14:55:37 UTC
What can I do to help getting this resolved?

I use two activies on my desktop, one for regular usage and one for work related stuff, and switching activities is PAINFUL because of this bug, because focus of the active window is lost every single time, and a mouse click, alt+tab or toggling the desktop-grid is required in order to regain focus, which is incredibly annoying.

Please don't ignore this bug.

> Perhaps it's related to wayland-related fixes.

I'm on X11, btw.
Comment 4 bastimeyer123 2022-07-08 11:44:14 UTC
Could this commit be related?
https://invent.kde.org/plasma/kwin/-/commit/b1686fd4729798f04ddf5980b49ae7f6e40734ce

It's the only relevant change in the `Workspace::updateCurrentActivity` (not sure if I'm looking at something unrelated):
https://invent.kde.org/plasma/kwin/-/blame/6af5a5e65112d9b24392efd6be461d3b85c1ef86/src/workspace.cpp#L1109-1189
Comment 5 bastimeyer123 2022-07-08 12:18:26 UTC
Reverting the linked commit fixes losing focus when switching activities.
Comment 6 Nate Graham 2022-07-12 16:22:05 UTC
Nice find, can confirm.
Comment 7 Chad Feller 2022-09-28 23:18:31 UTC
Can also confirm:
Fedora 36
Plasma Version: 5.25.5
X11
Comment 8 bastimeyer123 2022-09-29 01:38:04 UTC
I'm not exactly sure what the commit mentioned in #4 was trying to solve. I'm using KWin's default focus settings and reverting the commit fixes the focus issues when switching activities. I've been including the revert in my KWin builds ever since, without any issues. The revert had to be updated at least twice though due to merge conflicts.

I don't want to submit a pull request myself, because as I said, I don't know the intentions of the commit that broke the focus, and I also pretty much don't know anything about KWin's internals, so this should be done by one of the devs/maintainers. Here's the current diff for the master branch which reverts the order of the if-blocks and restores the previous comments:

```diff
diff --git a/src/workspace.cpp b/src/workspace.cpp
index 8169fcb10..735b4a1a2 100644
--- a/src/workspace.cpp
+++ b/src/workspace.cpp
@@ -1360,13 +1360,16 @@ void Workspace::updateCurrentActivity(const QString &new_activity)
     Window *window = nullptr;
 
     // FIXME below here is a lot of focuschain stuff, probably all wrong now
-    //  Keep active window focused if it's on the new activity
-    if (m_activeWindow && m_activeWindow->isShown() && m_activeWindow->isOnCurrentDesktop() && m_activeWindow->isOnCurrentActivity()) {
-        window = m_activeWindow;
-    } else if (options->focusPolicyIsReasonable()) {
+    if (options->focusPolicyIsReasonable()) {
         // Search in focus chain
         window = m_focusChain->getForActivation(VirtualDesktopManager::self()->currentDesktop());
     }
+    // If "unreasonable focus policy" and active_client is on_all_desktops and
+    // under mouse (Hence == old_active_client), conserve focus.
+    // (Thanks to Volker Schatz <V.Schatz at thphys.uni-heidelberg.de>)
+    else if (m_activeWindow && m_activeWindow->isShown() && m_activeWindow->isOnCurrentDesktop() && m_activeWindow->isOnCurrentActivity()) {
+        window = m_activeWindow;
+    }
 
     if (!window) {
         window = findDesktop(true, VirtualDesktopManager::self()->currentDesktop());
```
Comment 9 joelsonejr 2023-08-08 02:45:53 UTC
*** Bug 457616 has been marked as a duplicate of this bug. ***