mirror of
https://github.com/JetBrains/JetBrainsRuntime.git
synced 2025-12-06 09:29:38 +01:00
JBR-4463 Activating app-modal dialog brings all app windows to front
(cherry picked from commit2b05925276) includes fix for JBR-4642 regression: "focus follows mouse" broken for modals, I need to click into them (cherry picked from commitsc55bf03680,ec748f84fb) includes fix for JBR-4957 regression: "Modal dialog is hidden by sibling popup on Linux" (cherry picked from commitea8da3cbe5) includes fix for JBR-4968: jb/java/awt/Window/ModalDialogAndPopup.java intermittently fails by TimeoutException (cherry picked from commit2e9ab0cb06) (cherry picked from commit72b9219964)
This commit is contained in:
@@ -27,7 +27,6 @@ package sun.awt.X11;
|
||||
import java.awt.*;
|
||||
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.InvocationEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -1262,6 +1261,18 @@ abstract class XDecoratedPeer extends XWindowPeer {
|
||||
}
|
||||
|
||||
XWindowPeer toFocus = this;
|
||||
|
||||
if (!FULL_MODAL_TRANSIENTS_CHAIN && modalBlocker != null && !haveCommonAncestor(target, modalBlocker)) {
|
||||
toFocus = AWTAccessor.getComponentAccessor().getPeer(modalBlocker);
|
||||
// raising an already top-most window is a no-op, but we perform corresponding
|
||||
// check here to avoid xmonad WM going into an infinite loop - raise request
|
||||
// causes it to refresh internal state and re-send WM_TAKE_FOCUS message
|
||||
if (!toFocus.isTopMostWindow()) {
|
||||
toFocus.toFront();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
while (toFocus.nextTransientFor != null) {
|
||||
toFocus = toFocus.nextTransientFor;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,8 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
|
||||
= "true".equals(System.getProperty("reparenting.check"));
|
||||
private static final boolean ENABLE_DESKTOP_CHECK
|
||||
= "true".equals(System.getProperty("transients.desktop.check", "true"));
|
||||
static final boolean FULL_MODAL_TRANSIENTS_CHAIN
|
||||
= "true".equals(System.getProperty("full.modal.transients.chain"));
|
||||
|
||||
// should be synchronized on awtLock
|
||||
private static Set<XWindowPeer> windows = new HashSet<XWindowPeer>();
|
||||
@@ -1559,7 +1561,9 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
|
||||
if (isReparented() ||
|
||||
ENABLE_REPARENTING_CHECK && XWM.isNonReparentingWM() ||
|
||||
!ENABLE_REPARENTING_CHECK && isMapped()) {
|
||||
removeFromTransientFors();
|
||||
if (FULL_MODAL_TRANSIENTS_CHAIN || haveCommonAncestor(target, d)) {
|
||||
removeFromTransientFors();
|
||||
}
|
||||
} else {
|
||||
delayedModalBlocking = false;
|
||||
}
|
||||
@@ -1756,6 +1760,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
|
||||
|
||||
private void addToTransientFors(XDialogPeer blockerPeer, Vector<XWindowPeer> javaToplevels)
|
||||
{
|
||||
if (!FULL_MODAL_TRANSIENTS_CHAIN && !haveCommonAncestor(target, blockerPeer.target)) return;
|
||||
// blockerPeer chain iterator
|
||||
XWindowPeer blockerChain = blockerPeer;
|
||||
while (blockerChain.prevTransientFor != null) {
|
||||
@@ -2396,4 +2401,42 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
|
||||
public void updateWindow() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
boolean isTopMostWindow() {
|
||||
long curChild = 0;
|
||||
long curParent = window;
|
||||
while (curParent != 0) {
|
||||
XQueryTree qt = new XQueryTree(curParent);
|
||||
try {
|
||||
if (qt.execute() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (curParent == qt.get_root()) {
|
||||
// children are reported in bottom-to-top order,
|
||||
// so we are checking the last one
|
||||
int nChildren = qt.get_nchildren();
|
||||
return nChildren > 0 && Native.getWindow(qt.get_children(), nChildren - 1) == curChild;
|
||||
} else {
|
||||
// our window could have been re-parented by window manager,
|
||||
// so we should get to the direct child of the root window
|
||||
curChild = curParent;
|
||||
curParent = qt.get_parent();
|
||||
}
|
||||
} finally {
|
||||
qt.dispose();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean haveCommonAncestor(Component c1, Component c2) {
|
||||
return getRootOwner(c1) == getRootOwner(c2);
|
||||
}
|
||||
|
||||
static Component getRootOwner(Component c) {
|
||||
while (c.getParent() != null) {
|
||||
c = c.getParent();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
78
test/jdk/jb/java/awt/Window/ModalDialogAndPopup.java
Normal file
78
test/jdk/jb/java/awt/Window/ModalDialogAndPopup.java
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2022 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.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @summary Regression test for JBR-4957 Modal dialog is hidden by sibling popup on Linux
|
||||
* @key headful
|
||||
*/
|
||||
|
||||
public class ModalDialogAndPopup {
|
||||
private static final CompletableFuture<Boolean> modalDialogButtonClicked = new CompletableFuture<>();
|
||||
private static Robot robot;
|
||||
private static JFrame frame;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
robot = new Robot();
|
||||
try {
|
||||
SwingUtilities.invokeLater(ModalDialogAndPopup::initUI);
|
||||
robot.delay(2000);
|
||||
clickAt(350, 350);
|
||||
robot.delay(1000);
|
||||
clickAt(450, 350);
|
||||
modalDialogButtonClicked.get(5, TimeUnit.SECONDS);
|
||||
} finally {
|
||||
SwingUtilities.invokeAndWait(ModalDialogAndPopup::disposeUI);
|
||||
}
|
||||
}
|
||||
|
||||
private static void initUI() {
|
||||
frame = new JFrame("ModalDialogAndPopup");
|
||||
frame.setBounds(200, 200, 500, 300);
|
||||
frame.setVisible(true);
|
||||
JWindow w = new JWindow(frame);
|
||||
w.setBounds(300, 250, 200, 200);
|
||||
w.setVisible(true);
|
||||
JDialog d = new JDialog(frame, true);
|
||||
JButton b = new JButton("button");
|
||||
b.addActionListener(e -> modalDialogButtonClicked.complete(true));
|
||||
d.add(b);
|
||||
d.setBounds(400, 250, 200, 200);
|
||||
d.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);
|
||||
}
|
||||
}
|
||||
123
test/jdk/jb/java/awt/Window/ZOrderOnModalDialogActivation.java
Normal file
123
test/jdk/jb/java/awt/Window/ZOrderOnModalDialogActivation.java
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright 2022 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.*;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @summary Regression test for JBR-4463 Activating app-modal dialog brings all app windows to front
|
||||
* @key headful
|
||||
*/
|
||||
|
||||
public class ZOrderOnModalDialogActivation {
|
||||
static final int EXPECTED_EXIT_CODE = 123;
|
||||
|
||||
private static Robot robot;
|
||||
private static JFrame frame1;
|
||||
private static JFrame frame2;
|
||||
private static Process otherProcess;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
robot = new Robot();
|
||||
try {
|
||||
SwingUtilities.invokeLater(ZOrderOnModalDialogActivation::initUI);
|
||||
robot.delay(1000);
|
||||
launchOtherProcess();
|
||||
robot.delay(1000);
|
||||
clickAt(350, 500); // Button 2 - make sure other process' window is activated
|
||||
robot.delay(1000);
|
||||
clickAt(200, 200); // modal dialog
|
||||
robot.delay(1000);
|
||||
clickAt(250, 500); // Button 1 (assuming other process' window is still shown above)
|
||||
if (!otherProcess.waitFor(5, TimeUnit.SECONDS)) {
|
||||
throw new RuntimeException("Child process hasn't exited");
|
||||
}
|
||||
if (otherProcess.exitValue() != EXPECTED_EXIT_CODE) {
|
||||
throw new RuntimeException("Unexpected exit code: " + otherProcess.exitValue());
|
||||
}
|
||||
} finally {
|
||||
SwingUtilities.invokeAndWait(ZOrderOnModalDialogActivation::disposeUI);
|
||||
}
|
||||
}
|
||||
|
||||
private static void initUI() {
|
||||
frame1 = new JFrame("ZOOMDA 1");
|
||||
frame1.setBounds(100, 100, 200, 200);
|
||||
frame1.setVisible(true);
|
||||
frame2 = new JFrame("ZOOMDA 2");
|
||||
frame2.setBounds(100, 400, 200, 200);
|
||||
frame2.setVisible(true);
|
||||
JDialog d = new JDialog(frame1, "ZOOMDA 3", true);
|
||||
d.setBounds(150, 150, 100, 100);
|
||||
d.setVisible(true);
|
||||
}
|
||||
|
||||
private static void disposeUI() {
|
||||
if (frame1 != null) frame1.dispose();
|
||||
if (frame2 != null) frame2.dispose();
|
||||
if (otherProcess != null) otherProcess.destroyForcibly();
|
||||
}
|
||||
|
||||
private static void launchOtherProcess() throws Exception {
|
||||
otherProcess = Runtime.getRuntime().exec(new String[]{
|
||||
System.getProperty("java.home") + File.separator + "bin" + File.separator + "java",
|
||||
"-cp",
|
||||
System.getProperty("java.class.path"),
|
||||
"ZOrderOnModalDialogActivationChild"
|
||||
});
|
||||
if (otherProcess.getInputStream().read() == -1) {
|
||||
throw new RuntimeException("Error starting process");
|
||||
}
|
||||
}
|
||||
|
||||
private static void clickAt(int x, int y) {
|
||||
robot.mouseMove(x, y);
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
}
|
||||
}
|
||||
|
||||
class ZOrderOnModalDialogActivationChild {
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JFrame f = new JFrame("ZOOMDAC");
|
||||
f.addWindowFocusListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowGainedFocus(WindowEvent e) {
|
||||
System.out.println();
|
||||
}
|
||||
});
|
||||
f.setLayout(new BorderLayout());
|
||||
JButton button1 = new JButton("Button 1");
|
||||
button1.addActionListener(e -> System.exit(ZOrderOnModalDialogActivation.EXPECTED_EXIT_CODE));
|
||||
f.add(button1, BorderLayout.WEST);
|
||||
f.add(new JButton("Button 2"), BorderLayout.EAST);
|
||||
f.setBounds(200, 450, 200, 100);
|
||||
f.setVisible(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -876,4 +876,6 @@ com/sun/java/swing/plaf/windows/Test8173145.java
|
||||
com/sun/java/swing/plaf/windows/AltFocusIssueTest.java JBR-4197 windows-all
|
||||
|
||||
java/awt/event/MouseEvent/AltGraphModifierTest/AltGraphModifierTest.java JBR-4207 windows-all
|
||||
jb/java/awt/keyboard/AltGrMustGenerateAltGrModifierTest4207.java JBR-4207 windows-all
|
||||
jb/java/awt/keyboard/AltGrMustGenerateAltGrModifierTest4207.java JBR-4207 windows-all
|
||||
|
||||
jb/java/awt/Window/ModalDialogAndPopup.java JBR-4984 macosx-all
|
||||
|
||||
Reference in New Issue
Block a user