JBR-2759 Typeahead issue on Linux

(cherry picked from commits 76bdaf1131, b20c56ff3e)
This commit is contained in:
Dmitry Batrak
2020-10-09 13:57:15 +03:00
committed by Vitaly Provodin
parent f4e184b6da
commit b34ea2d32f
4 changed files with 39 additions and 122 deletions

View File

@@ -152,6 +152,17 @@ public abstract class KeyboardFocusManager
public Container getCurrentFocusCycleRoot() {
return KeyboardFocusManager.currentFocusCycleRoot;
}
@Override
public void enqueueKeyEvents(Component untilFocused) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().enqueueKeyEvents(
Toolkit.getEventQueue().getMostRecentKeyEventTime(), untilFocused);
}
@Override
public void dequeueKeyEvents(Component untilFocused) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().dequeueKeyEvents(-1, untilFocused);
}
}
);
}

View File

@@ -463,6 +463,10 @@ public final class AWTAccessor {
* Return the current focus cycle root
*/
Container getCurrentFocusCycleRoot();
void enqueueKeyEvents(Component untilFocused);
void dequeueKeyEvents(Component untilFocused);
}
/**

View File

@@ -49,8 +49,6 @@ import java.awt.event.WindowEvent;
import java.awt.peer.ComponentPeer;
import java.awt.peer.WindowPeer;
import java.io.UnsupportedEncodingException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
@@ -850,7 +848,16 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
focusLog.fine("Requesting window focus");
}
requestWindowFocus(time, timeProvided, () -> {}, () -> {});
Runnable finishRunnable = this::dequeueKeyEvents;
requestWindowFocus(time, timeProvided, finishRunnable, finishRunnable);
}
private void dequeueKeyEvents() {
AWTAccessor.getKeyboardFocusManagerAccessor().dequeueKeyEvents(target);
}
private void enqueueKeyEvents() {
AWTAccessor.getKeyboardFocusManagerAccessor().enqueueKeyEvents(target);
}
public final boolean focusAllowedFor() {
@@ -1125,6 +1132,18 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
warningWindow.setSecurityWarningVisible(false, false);
}
boolean refreshChildsTransientFor = isVisible() != vis;
if (vis && isSimpleWindow() && shouldFocusOnMapNotify()) {
// We enable type-ahead mechanism only for showing of simple windows. That's because, when enabling it,
// we need to be sure that the final state (focusing) of the target window will definitely be available
// very soon, not to block key events for a long period of time. As simple windows are not focused natively,
// the only event we should wait for before focusing them internally is MapNotify, and that event usually
// comes quite fast after map request. There are known cases when window manager delays mapping the window
// for a long time (e.g. i3wm does this when another window is in full-screen mode), but no known cases
// when it does it for popup windows, so we hope that we're safe here with simple windows. For decorated
// windows we also wait for the native focus to be transferred to the target window (FocusIn event),
// which adds to the uncertainty - the focus might or might not be transferred.
enqueueKeyEvents();
}
super.setVisible(vis);
if (refreshChildsTransientFor) {
for (Window child : ((Window) target).getOwnedWindows()) {
@@ -1429,6 +1448,8 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
if (shouldFocusOnMapNotify()) {
focusLog.fine("Automatically request focus on window");
requestInitialFocus();
} else {
dequeueKeyEvents();
}
isUnhiding = false;
isBeforeFirstMapNotify = false;

View File

@@ -1,119 +0,0 @@
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* 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 java.awt.Dialog;
import java.awt.Frame;
import java.io.*;
import javax.swing.*;
import sun.awt.SunToolkit;
import java.util.concurrent.atomic.AtomicReference;
/**
* @test
* @key headful
* @bug 8043705
* @summary Can't exit color chooser dialog when running as an applet
* @modules java.desktop/sun.awt
* @run main CloseDialogTest
*/
public class CloseDialogTest {
private static volatile Frame frame;
private static volatile Dialog dialog;
private static volatile InputStream testErrorStream;
private static final PrintStream systemErrStream = System.err;
private static final AtomicReference<Exception> caughtException
= new AtomicReference<>();
public static void main(String[] args) throws Exception {
// redirect System err
PipedOutputStream errorOutputStream = new PipedOutputStream();
testErrorStream = new PipedInputStream(errorOutputStream);
System.setErr(new PrintStream(errorOutputStream));
ThreadGroup swingTG = new ThreadGroup(getRootThreadGroup(), "SwingTG");
try {
new Thread(swingTG, () -> {
SunToolkit.createNewAppContext();
SwingUtilities.invokeLater(() -> {
frame = new Frame();
frame.setSize(300, 300);
frame.setVisible(true);
dialog = new Dialog(frame);
dialog.setSize(200, 200);
dialog.setModal(true);
dialog.setVisible(true);
});
}).start();
Thread.sleep(400);
Thread disposeThread = new Thread(swingTG, () ->
SwingUtilities.invokeLater(() -> {
try {
while (dialog == null || !dialog.isVisible()) {
Thread.sleep(100);
}
dialog.setVisible(false);
dialog.dispose();
frame.dispose();
} catch (Exception e) {
caughtException.set(e);
}
}));
disposeThread.start();
disposeThread.join();
Thread.sleep(500);
// read System err
final char[] buffer = new char[2048];
System.err.print("END");
System.setErr(systemErrStream);
try (Reader in = new InputStreamReader(testErrorStream, "UTF-8")) {
int size = in.read(buffer, 0, buffer.length);
String errorString = new String(buffer, 0, size);
if (!errorString.startsWith("END")) {
System.err.println(errorString.
substring(0, errorString.length() - 4));
throw new RuntimeException("Error output is not empty!");
}
}
} finally {
if (caughtException.get() != null) {
throw new RuntimeException("Failed. Caught exception!",
caughtException.get());
}
}
}
private static ThreadGroup getRootThreadGroup() {
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
while (threadGroup.getParent() != null) {
threadGroup = threadGroup.getParent();
}
return threadGroup;
}
}