Originally reported in the Compose Multiplatform bug tracker, but turns out this is a JVM bug: https://youtrack.jetbrains.com/issue/CMP-7434/LocalWindowInfo.current.isWindowFocused-reports-true-for-not-focused-windows
On Windows, when a non-focused window has it's alwaysOnTop property changed, either from false to true, or true to false, Swing starts to report it as having focus, and windowGainedFocus fires. The window does not actually get focused (which is correct), it's just wrongly reported as having focus, and is stuck in this state until it actually gets focused.
See the linked bug for reproducer samples.