RFR: 8359899: Stage.isFocused() returns invalid value when Stage fails to receive focus [v2]

Martin Fox mfox at openjdk.org
Mon Nov 3 12:52:27 UTC 2025


On Thu, 30 Oct 2025 13:43:58 GMT, Lukasz Kostyra <lkostyra at openjdk.org> wrote:

>> 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.

On my system the `SetForegroundWindow` call activates the window and makes it the focus window. We get `WM_ACTIVATE` and `WM_SETFOCUS` and tell the Java side to set the focused property to true. Then `SetForegroundWindow` returns false and we tell the Java side to clear the focused property.

-------------

PR Review Comment: https://git.openjdk.org/jfx/pull/1849#discussion_r2486382236


More information about the openjfx-dev mailing list