8359899: Stage.isFocused() returns invalid value when Stage fails to receive focus #1849
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NOTE: For now I submitted this change as a DRAFT PR in order to get some feedback on whether this is the way to fix the problem, as well as to check for potential problems with this solution that I might've missed.
This PR fixes
isFocused()
returning invalid value when Stage fails to receive focus after callingStage.show()
. This problem is Windows-only.In Windows the
SetForegroundWindow()
API lists a set of conditions to successfully grant focus to a Window. If any of the conditions are not met, the API will return FALSE. JavaFX did not respect that and instead assumed that receivingWM_ACTIVATE
with our Window being activated is enough to assume the Window is in focus (which in some cases is not true).I first tried reacting to
WM_SETFOCUS
andWM_KILLFOCUS
but it seems those messages are not sent when the window is shown for the first time (insteadWM_ACTIVATE
is used).To correct this behavior, I noticed the following path is the most reliable:
ShowWindow()
usingSW_SHOWNA
instead ofSW_SHOW
- that makes the window visible but does NOT activate itSetForegroundWindow()
- that will attempt to give the Window focus and will also activate it if it is successfulnotifyFocus
callback will be called viaWM_ACTIVATE
handlernotifyFocus
callback manually informing the upper layers the focus is lost. This establishes the correct state ofWindow.focused
property.With this change I observed that all tests pass as intended as long as two conditions are met (these are needed to satisfy
SetForegroundWindow()
restrictions):If any of above two conditions is not met, some tests (including canary test from #1804) now timeout/fail when checking whether
Window.isFocused()
is true.Manually started JavaFX apps (ex. Ensemble) run as they used to and still receive focus upon Stage showing.
Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx.git pull/1849/head:pull/1849
$ git checkout pull/1849
Update a local copy of the PR:
$ git checkout pull/1849
$ git pull https://git.openjdk.org/jfx.git pull/1849/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 1849
View PR using the GUI difftool:
$ git pr show -t 1849
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/1849.diff