Closed mgroth0 closed 1 month ago
Reproduced with 1.6.10-rc01 too.
The problem here is that you're calling Window.requestFocus
, which transfers focus to the window component itself. Compose, then, doesn't receive the key events, because they go to the window, rather than to the component on which Compose listens to key events.
You simply need to do window.toFront()
instead.
P.S. All the workarounds in https://github.com/JetBrains/compose-multiplatform/issues/4231 are needed because in that ticket the window receiving focus does not exist yet when the request is being made (and the app is not even in the foreground), which is not the case here.
Describe the bug
A window will only focus properly if certain conditions are met.
While researching this issue, I came accross the
WindowAdapter
workaround in https://github.com/JetBrains/compose-multiplatform/issues/4231 by @m-sasha. This is one of the requirements that must be met, but it alone is not enough.In the scenario we have two windows. Window 1 has a text field, and the window 2 has a button. The button in window 2 should focus window 1 and its text field so that the user can start typing.
The expectation is that it will just work without complication.
What actually happens is that numerous strange requirements exist. It can work, but only if all of the following are true:
WindowAdapter
workaround, but the simple version in whichfocus.requestFocus()
is called right away only works about 50% of the time. In order for it to work 100% of the time, we need to run it after a short delay.FocusRequester
must request focus (if there are multiple components in the window with the text field)Requirement 3 is not a bug, but 1 and 2 are definitely bugs. The reason I include 3 here is to emphasize that even without 1 and 2, this operation is already quite complex and easy to mess up, which increases the cognitive burden caused by 1 and 2
Affected platforms
Versions
To Reproduce
Part 1
Part 2
Context
In the end, it seems that the second issue does have an ok workaround. It adds complexity to the code and the variable delay also adds room for error, but its ok.
But the first issue is worse, in my opinion, because the only workaround I have found so far is that the user has to literally click the window, if that could even be considered a workaround. Maybe
java.awt.Robot
could click the window, but I don't think I would want to go there so I didn't try it.