RFR: 8359899: Stage.isFocused() returns invalid value when Stage fails to receive focus [v2]
Lukasz Kostyra
lkostyra at openjdk.org
Thu Oct 30 13:46:52 UTC 2025
On Wed, 29 Oct 2025 20:38:57 GMT, Kevin Rushforth <kcr at openjdk.org> wrote:
>> Lukasz Kostyra has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:
>>
>> - Merge remote-tracking branch 'origin/master' into isFocused_fix-8359899
>> - Adjust window focus management for Windows Glass
>
> modules/javafx.graphics/src/main/native-glass/win/GlassWindow.cpp line 1959:
>
>> 1957: }
>> 1958:
>> 1959: ::ShowWindow(hWnd, visible ? SW_SHOWNA : SW_HIDE);
>
> Is this change needed? I presume that the idea behind it is that if it is a focusable window, it will be activated anyway?
Yes, this is part of the fix. The values in question are:
- `SW_SHOW` - shows a window and activates it, sending the `WM_ACTIVATE` message
- `SW_SHOWNA` - shows a window but doesn't activate it - activation is postponed to us later calling `::SetForegroundWindow()`
By using `SW_SHOWNA` we can postpone the window activation and have `::SetForegroundWindow()` trigger it. If `::SetForegroundWindow()` succeeds, window gets activated and gains focus, and `WM_ACTIVATE` gets sent, which will then be captured by JavaFX message loop so we can notify Java-side that we gained focus. If `::SetForegroundWindow()` fails, we see that in this function and we can notify Java-side the focus has been lost.
This is unfortunately a bit hacky, but I couldn't find another way of establishing whether we actually have focus or not, especially combining that with what `::SetForegroundWindow()` returns. With `SW_SHOW` the `WM_ACTIVATE` message would go through despite `::SetForegroundWindow()` failing and us ultimately _not_ having focus.
My first attempt also considered leaving this part as-is and simply notifying Java-side if we lost focus or not based on `::SetForegroundWindow()` result, but that wasn't consistent. Postponing the window activation seemed to me as the best bet here.
-------------
PR Review Comment: https://git.openjdk.org/jfx/pull/1849#discussion_r2478173850
More information about the openjfx-dev
mailing list