Bug 495741

Summary: Focus not properly given to the destination window after Alt+Tab under Wayland
Product: [Plasma] kwin Reporter: Huanyu Liu <1293660441>
Component: generalAssignee: KWin default assignee <kwin-bugs-null>
Status: CONFIRMED ---    
Severity: normal CC: ben.agaric+kde, filip.kendes1, kde, maxim.yegorushkin, miranda, phil.hord, shimeike
Priority: NOR Keywords: usability
Version: 6.2.4   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Screencast of the bug when using Kate with an IME
dolphin issue
kwinrc-01

Description Huanyu Liu 2024-11-03 09:14:51 UTC
Created attachment 175481 [details]
Screencast of the bug when using Kate with an IME

SUMMARY
When using Alt+Tab to switch windows under Wayland, the focus is not properly given to the destination window. An additional keystroke is required to gain the focus properly. The additional keystroke may or may not be sent to the destination window, depending on the situation. This bug is not reproducible under X11. The bug is not reproducible when switching windows using taskbar.

STEPS TO REPRODUCE & OBSERVED RESULT
Case 1 (System Settings)
1. Launch System Settings
2. Click on the search bar
3. Switch windows using Alt+Tab to System Settings again
-- Now you can observe that everything in the System Settings window is displayed in unfocused style
4a. Press any key on the keyboard, be it A, Caps Lock, Alt or Esc
-- Now the window regains the focus, while the pressed key is not sent to it
4b. Alternatively, click on the search bar with a mouse
-- Now only the search bar regains the focus, while everything else in the window remains unfocused, *even if you click anything with a mouse*, before you press a key

Case 2 (Kate)
1. Launch Kate and create a new file
2. Switch windows using Alt+Tab to Kate again
-- Now you can observe the same unfocused display, especially, the cursor not blinking, *even though you can move the cursor position with a mouse*
3. Press any key on the keyboard
-- Now the window regains the focus, while the pressed key is sent to it (e.g. if you press A, then an "a" will be typed)

EXPECTED RESULT
The destination window should be given the focus immediately after switching to it.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux 
KDE Plasma Version: 6.2.2
KDE Frameworks Version: 6.7.0
Qt Version: 6.8.0
Kernel Version: 6.11.5-arch1-1 (64-bit)
Graphics Platform: Wayland

ADDITIONAL INFORMATION
This bug is especially annoying if you are using an IME, since the first keystroke after Alt+Tab will bypass the IME. See the attached screencast for a demonstration of the bug when using Kate with an IME.
Comment 1 David Edmundson 2024-11-04 17:32:14 UTC
I cannot reproduce even with fcitx enabled. Can you share your ~/.config/kwinrc?
Comment 2 Huanyu Liu 2024-11-09 03:31:57 UTC
I updated to KDE Plasma 6.2.3 and Linux Kernel 6.11.6-arch1-1 and the bug persists.

(In reply to David Edmundson from comment #1)
> I cannot reproduce even with fcitx enabled. Can you share your
> ~/.config/kwinrc?

I created a new account with minimal KWin settings, fcitx disabled and system language set to English (as I encountered a language-dependent crash in LabPlot previously). The bug can still be reproduced. Here is the contents of the auto-generated minimal kwinrc file:

[Desktops]
Id_1=610b991e-7f67-42d3-9531-d707b5bc4e9a
Number=1
Rows=1

[Tiling]
padding=4

[Tiling][052f5458-d764-5f16-9bfc-557564c1e991]
tiles={"layoutDirection":"horizontal","tiles":[{"width":0.25},{"width":0.5},{"width":0.25}]}

[Tiling][292aea65-7de9-5e3b-b0a0-35d5d648c8c2]
tiles={"layoutDirection":"horizontal","tiles":[{"width":0.25},{"width":0.5},{"width":0.25}]}

[Tiling][afb210b1-e2f2-52db-a504-666041e512b9]
tiles={"layoutDirection":"horizontal","tiles":[{"width":0.25},{"width":0.5},{"width":0.25}]}

[Xwayland]
Scale=1
Comment 3 Filip 2024-11-26 19:22:42 UTC
*** Bug 496535 has been marked as a duplicate of this bug. ***
Comment 4 Filip 2024-11-26 19:22:47 UTC
*** Bug 496716 has been marked as a duplicate of this bug. ***
Comment 5 Filip 2024-11-26 19:25:04 UTC
Created attachment 176147 [details]
dolphin issue
Comment 6 Filip 2024-11-26 19:27:52 UTC
Created attachment 176148 [details]
kwinrc-01
Comment 7 Filip 2024-11-26 19:31:55 UTC
I can also reproduce this issue on Dolphin (uploaded a video) and in terminals like in https://bugs.kde.org/show_bug.cgi?id=496716 on multiple devices (PC and laptop)

I also uploaded my kwinrc from PC.

Both devices run Arch Linux, PC has Nvidia proprietary driver, laptop has nvidia gpu, but running nouveau. Both on Wayland.
Operating System: Arch Linux 
KDE Plasma Version: 6.2.3
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.0
Kernel Version: 6.11.9-arch1-1 (64-bit)
Graphics Platform: Wayland
Comment 8 ozziem 2024-12-02 22:20:15 UTC
Possible further confirmation of an issue related to window focus using Alt-Tab under Wayland:
https://forum.manjaro.org/t/plasma-window-focus-when-using-task-switcher-across-desktops-behaviour-changed-after-latest-stable-update/171317

In my case the focus issues occur specifically when "Alt-Tabbing" to a window which is on a different virtual desktop.

Operating System: Manjaro Linux rolling
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.0
Kernel Version: 6.12.1-4-MANJARO (64-bit)
Graphics Platform: Wayland
Comment 9 nttkde 2024-12-09 17:23:14 UTC
I too noticed that when I Alt-Tab to eg. Kwrite(Kate) its text area stays in this weird unfocused state (but titlebar gets focused color).
In Kwin debug console, the window's parameter 'active' seems to change normally to true.
A key press brings the app to proper focus again.
Also, in Kwrite the first keypress of regular letter keys or Enter get passed to its text area, but first press of at least Backspace, Insert, Delete, PgUp, PGDown and arrow keys is discarded.
System setting's search field seems to discard any first keypress.

Operating System: KDE neon 6.2
KDE Plasma Version: 6.2.3
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.0
Kernel Version: 6.8.0-49-generic (64-bit)
Graphics Platform: Wayland
single monitor, no virtual desktops in use
Comment 10 benjamin melançon 2024-12-17 03:40:37 UTC
Confirming this as still a problem with the Task Switcher in Plasma 6.2.4 also in NixOS, not only Arch.  (Posted here before being pointed to this issue: https://discuss.kde.org/t/task-switcher-gives-a-konsole-window-sufficient-focus-to-type-but-not-to-middle-click-paste/27029 )

Perhaps this will make it easier to fix: This is NOT a problem when the Icons-Only Task Manager set so that "Clicking grouped task" "Shows large window previews" and windows are selected and filtered that way.  (Indeed, if the Task Switcher could borrow more from the Icons-Only Task Manager here— correctly getting icons, above all allowing filtering, and "fully" selecting so that middle-click etc works as well as typing, KDE would be in fantastic shape as the most usable desktop for heavy task switchers.)
Comment 11 Maxim Egorushkin 2024-12-25 06:33:21 UTC
Came here to report exactly this same problem. 

Konsole doesn't get focus/blinking-cursor when alt-tab'ing into it. 

Emacs windows (frames in emacs parlance), on the other hand, do, and start blinking the cursor, as expected.

But when switching to Konsole windows using kwin «Switch to Window Left/Right/Up/Down» keyboard shortcuts Konsole does get focus and start blinking the cursor, as expected.

Info Center:
Operating System: KDE neon 6.2
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.1
Kernel Version: 6.8.0-50-generic (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 5825U with Radeon Graphics
Memory: 62.2 GiB of RAM
Graphics Processor: AMD Radeon Graphics
Manufacturer: Dell Inc.
Product Name: Inspiron 14 7425 2-in-1
System Version: 1.16.0