JBR-4021 Unexpected focus event order on window showing

(cherry picked from commit 2a398ebb24)

includes fix for JBR-4131 Popup doesn't get focus if created from context menu

(cherry picked from commit 685562aafc)
This commit is contained in:
Dmitry Batrak
2021-11-24 09:22:26 +03:00
committed by alexey.ushakov@jetbrains.com
parent 624a2ba241
commit ce245aaa9c
6 changed files with 235 additions and 13 deletions

View File

@@ -259,7 +259,7 @@ public class XComponentPeer extends XWindow implements ComponentPeer, DropTarget
int result = XKeyboardFocusManagerPeer.
shouldNativelyFocusHeavyweight(target, lightweightChild,
temporary, focusedWindowChangeAllowed,
time, cause, false);
time, cause, true);
switch (result) {
case XKeyboardFocusManagerPeer.SNFH_FAILURE:

View File

@@ -35,6 +35,7 @@ import java.util.Map;
import java.util.Optional;
import sun.awt.IconInfo;
import sun.awt.PeerEvent;
import sun.util.logging.PlatformLogger;
import sun.awt.AWTAccessor;
@@ -1306,7 +1307,7 @@ abstract class XDecoratedPeer extends XWindowPeer {
* WM_TAKE_FOCUS (when FocusIn is generated via XSetInputFocus call) but
* definetely before the Frame gets FocusIn event (when this method is called).
*/
postEvent(new InvocationEvent(target, new Runnable() {
postEvent(new PeerEvent(target, new Runnable() {
public void run() {
XWindowPeer fw = null;
synchronized (getStateLock()) {
@@ -1318,7 +1319,7 @@ abstract class XDecoratedPeer extends XWindowPeer {
}
fw.handleWindowFocusIn_Dispatch();
}
}));
}, PeerEvent.ULTIMATE_PRIORITY_EVENT));
}
}

View File

@@ -105,7 +105,7 @@ public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
{
return KeyboardFocusManagerPeerImpl.deliverFocus(lightweightChild,
target,
false,
true,
cause,
getInstance().getCurrentFocusOwner());
}

View File

@@ -611,20 +611,13 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
// NOTE: This method may be called by privileged threads.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
public void handleWindowFocusIn(long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS);
/* wrap in Sequenced, then post*/
XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow((Window) target);
postEvent(wrapInSequenced((AWTEvent) we));
handleWindowFocusInSync(serial, () -> {});
}
// NOTE: This method may be called by privileged threads.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
public void handleWindowFocusOut(Window oppositeWindow, long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow);
XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow(null);
XKeyboardFocusManagerPeer.getInstance().setCurrentFocusOwner(null);
/* wrap in Sequenced, then post*/
postEvent(wrapInSequenced((AWTEvent) we));
handleWindowFocusOutSync(oppositeWindow, serial);
}
public void handleWindowFocusOutSync(Window oppositeWindow, long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow);

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2021 JetBrains s.r.o.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @test
* @summary Regression test for JBR-4021 Unexpected focus event order on window showing
* @key headful
*/
public class EventsOnPopupShowing {
private static final AtomicInteger gainedCount = new AtomicInteger();
private static Robot robot;
private static JFrame frame;
private static JButton button;
public static void main(String[] args) throws Exception {
robot = new Robot();
try {
SwingUtilities.invokeAndWait(EventsOnPopupShowing::initUI);
robot.delay(1000);
clickOn(button);
robot.delay(3000);
if (gainedCount.get() != 1) {
throw new RuntimeException("Unexpected 'focus gained' count: " + gainedCount);
}
} finally {
SwingUtilities.invokeAndWait(EventsOnPopupShowing::disposeUI);
}
}
private static void initUI() {
frame = new JFrame("EventsOnPopupShowing");
button = new JButton("Start");
button.addActionListener(e -> {
JDialog d = new JDialog(frame, "Dialog", true);
d.setBounds(300, 300, 200, 200);
new Timer(1000, ee -> d.dispose()) {{ setRepeats(false); }}.start();
d.setVisible(true);
JWindow w = new JWindow(frame);
w.add(new JTextField());
w.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
gainedCount.incrementAndGet();
}
});
w.setBounds(300, 300, 200, 200);
w.setVisible(true);
try {
Thread.sleep(1000); // make sure all native events are processed
} catch (InterruptedException ex) {
ex.printStackTrace();
}
});
frame.add(button);
frame.setBounds(200, 200, 200, 200);
frame.setVisible(true);
}
private static void disposeUI() {
if (frame != null) frame.dispose();
}
private static void clickAt(int x, int y) {
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
private static void clickOn(Component component) {
Point location = component.getLocationOnScreen();
clickAt(location.x + component.getWidth() / 2, location.y + component.getHeight() / 2);
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2021 JetBrains s.r.o.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
/**
* @test
* @summary Regression test for JBR-4131 Popup doesn't get focus if created from context menu
* @key headful
*/
public class PopupFromMenuTest {
private static final CompletableFuture<Boolean> typedInPopup = new CompletableFuture<>();
private static Robot robot;
private static JFrame frame;
private static JTextField frameField;
private static JMenuItem menuItem;
public static void main(String[] args) throws Exception {
robot = new Robot();
try {
SwingUtilities.invokeAndWait(PopupFromMenuTest::initUI);
robot.delay(1000); // wait for frame to appear
rightClickOn(frameField);
robot.delay(1000); // wait for context menu to appear
clickOn(menuItem);
robot.delay(1000); // wait for popup to appear
type();
typedInPopup.get(5, TimeUnit.SECONDS);
} finally {
SwingUtilities.invokeAndWait(PopupFromMenuTest::disposeUI);
}
}
private static void initUI() {
frame = new JFrame("PopupFromMenuTest");
frameField = new JTextField(20);
JPopupMenu menu = new JPopupMenu();
menuItem = new JMenuItem(new AbstractAction("open popup") {
@Override
public void actionPerformed(ActionEvent e) {
JWindow popup = new JWindow(frame);
JTextField popupField = new JTextField(10);
popupField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
typedInPopup.complete(true);
}
@Override
public void removeUpdate(DocumentEvent e) {}
@Override
public void changedUpdate(DocumentEvent e) {}
});
popup.add(popupField);
popup.pack();
popup.setVisible(true);
popupField.requestFocus();
LockSupport.parkNanos(100_000_000);
}
});
menu.add(menuItem);
frameField.setComponentPopupMenu(menu);
frame.add(frameField);
frame.pack();
frame.setVisible(true);
}
private static void disposeUI() {
if (frame != null) frame.dispose();
}
private static void type() {
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
}
private static void clickAt(int x, int y, int buttons) {
robot.mouseMove(x, y);
robot.delay(50); // without this test fails sometimes on macOS (due to popup menu closing at once)
robot.mousePress(buttons);
robot.mouseRelease(buttons);
}
private static void clickOn(Component component, int buttons) {
Point location = component.getLocationOnScreen();
clickAt(location.x + component.getWidth() / 2, location.y + component.getHeight() / 2, buttons);
}
private static void clickOn(Component component) {
clickOn(component, InputEvent.BUTTON1_DOWN_MASK);
}
private static void rightClickOn(Component component) {
clickOn(component, InputEvent.BUTTON3_DOWN_MASK);
}
}