Skip to content

8359899: Stage.isFocused() returns invalid value when Stage fails to receive focus #1849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ LRESULT FullScreenWindow::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
case WM_ACTIVATE:
{
// The fActive shouldn't be WA_INACTIVE && the window shouldn't be minimized:
const bool isFocusGained = LOWORD(wParam) != WA_INACTIVE && HIWORD(wParam) == 0;
const bool isActive = LOWORD(wParam) != WA_INACTIVE && HIWORD(wParam) == 0;

if (!isFocusGained && IsCommonDialogOwner()) {
if (!isActive && IsCommonDialogOwner()) {
// Remain in full screen while a file dialog is showing
break;
}

HWND hWndInsertAfter = isFocusGained ? HWND_TOPMOST : HWND_BOTTOM;
HWND hWndInsertAfter = isActive ? HWND_TOPMOST : HWND_BOTTOM;

if (m_bgWindow) {
::SetWindowPos(m_bgWindow->GetHWND(), hWndInsertAfter, 0, 0, 0, 0,
Expand All @@ -282,14 +282,14 @@ LRESULT FullScreenWindow::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam)

GlassWindow * window = GlassWindow::FromHandle(m_oldViewParent);
if (window) {
window->HandleActivateEvent(isFocusGained ?
window->HandleActivateEvent(isActive ?
com_sun_glass_events_WindowEvent_FOCUS_GAINED :
com_sun_glass_events_WindowEvent_FOCUS_LOST);

// Child windows don't have a taskbar button, therefore
// we force exiting from the FS mode if the window looses
// focus.
if (!isFocusGained) {
if (!isActive) {
ExitFullScreenMode(FALSE);
}
}
Expand Down
37 changes: 25 additions & 12 deletions modules/javafx.graphics/src/main/native-glass/win/GlassWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,17 @@ LRESULT GlassWindow::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
case WM_ACTIVATE:
{
// The fActive shouldn't be WA_INACTIVE && the window shouldn't be minimized:
const bool isFocusGained = LOWORD(wParam) != WA_INACTIVE && HIWORD(wParam) == 0;
const bool isActive = LOWORD(wParam) != WA_INACTIVE && HIWORD(wParam) == 0;

if (IsInFullScreenMode()) {
HWND hWndInsertAfter = isFocusGained ? HWND_TOPMOST : HWND_BOTTOM;
HWND hWndInsertAfter = isActive ? HWND_TOPMOST : HWND_BOTTOM;
::SetWindowPos(GetHWND(), hWndInsertAfter, 0, 0, 0, 0,
SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE);
}
if (!GetDelegateWindow()) {
HandleActivateEvent(isFocusGained ?
com_sun_glass_events_WindowEvent_FOCUS_GAINED :
com_sun_glass_events_WindowEvent_FOCUS_LOST);
HandleActivateEvent(isActive ?
com_sun_glass_events_WindowEvent_FOCUS_GAINED :
com_sun_glass_events_WindowEvent_FOCUS_LOST);
}
}
// Let the DefWindowProc() set the focus to this window
Expand All @@ -411,12 +411,12 @@ LRESULT GlassWindow::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
break;
case WM_SETFOCUS:
if (!GetDelegateWindow()) {
SetFocused(true);
HandleFocusEvent(com_sun_glass_events_WindowEvent_FOCUS_GAINED);
}
break;
case WM_KILLFOCUS:
if (!GetDelegateWindow()) {
SetFocused(false);
HandleFocusEvent(com_sun_glass_events_WindowEvent_FOCUS_LOST);
}
break;
case WM_GETMINMAXINFO:
Expand Down Expand Up @@ -810,12 +810,23 @@ void GlassWindow::HandleSizeEvent(int type, RECT *pRect)

void GlassWindow::HandleActivateEvent(jint event)
{
const bool active = event != com_sun_glass_events_WindowEvent_FOCUS_LOST;
const bool focus = event != com_sun_glass_events_WindowEvent_FOCUS_LOST;

if (!active) {
if (!focus) {
UngrabFocus();
}

HandleFocusEvent(event);
}

void GlassWindow::HandleFocusEvent(jint event)
{
const bool focus = event != com_sun_glass_events_WindowEvent_FOCUS_LOST;

if (focus == m_isFocused) return;

SetFocused(focus);

JNIEnv* env = GetEnv();
env->CallVoidMethod(m_grefThis, javaIDs.Window.notifyFocus, event);
CheckAndClearException(env);
Expand Down Expand Up @@ -1869,13 +1880,15 @@ JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_win_WinWindow__1setVisible
}
}


::ShowWindow(hWnd, visible ? SW_SHOW : SW_HIDE);
::ShowWindow(hWnd, visible ? SW_SHOWNA : SW_HIDE);

if (visible) {
if (pWindow) {
if (pWindow->IsFocusable()) {
::SetForegroundWindow(hWnd);
BOOL success = ::SetForegroundWindow(hWnd);
if (success == FALSE) {
pWindow->HandleFocusEvent(com_sun_glass_events_WindowEvent_FOCUS_LOST);
}
} else {
// JDK-8112905:
// On some latest platform versions, unfocusable windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class GlassWindow : public BaseWnd, public ViewContainer {
HWND GetDelegateWindow() { return m_delegateWindow; }

void HandleActivateEvent(jint event);
void HandleFocusEvent(jint event);
void HandleCloseEvent();

virtual BOOL EnterFullScreenMode(GlassView * view, BOOL animate, BOOL keepRatio);
Expand Down