Skip to content

Commit 2ee38b7

Browse files
committed
8342096: Popup menus that request focus are not shown on Linux with Wayland
1 parent 68aa4d4 commit 2ee38b7

File tree

3 files changed

+129
-12
lines changed

3 files changed

+129
-12
lines changed

src/java.desktop/unix/classes/sun/awt/UNIXToolkit.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import java.io.BufferedReader;
5252
import java.io.IOException;
5353
import java.io.InputStreamReader;
54-
import java.util.Arrays;
5554

5655
import sun.awt.X11.XBaseWindow;
5756
import com.sun.java.swing.plaf.gtk.GTKConstants.TextDirection;
@@ -521,6 +520,18 @@ public boolean isRunningOnWayland() {
521520
// application icons).
522521
private static final WindowFocusListener waylandWindowFocusListener;
523522

523+
private static boolean containsWaylandWindowFocusListener(Window window) {
524+
if (window == null) {
525+
return false;
526+
}
527+
for (WindowFocusListener focusListener : window.getWindowFocusListeners()) {
528+
if (focusListener == waylandWindowFocusListener) {
529+
return true;
530+
}
531+
}
532+
return false;
533+
}
534+
524535
static {
525536
if (isOnWayland()) {
526537
waylandWindowFocusListener = new WindowAdapter() {
@@ -530,10 +541,10 @@ public void windowLostFocus(WindowEvent e) {
530541
Window oppositeWindow = e.getOppositeWindow();
531542

532543
// The focus can move between the window calling the popup,
533-
// and the popup window itself.
544+
// and the popup window itself or its children.
534545
// We only dismiss the popup in other cases.
535546
if (oppositeWindow != null) {
536-
if (window == oppositeWindow.getParent() ) {
547+
if (containsWaylandWindowFocusListener(oppositeWindow.getOwner())) {
537548
addWaylandWindowFocusListenerToWindow(oppositeWindow);
538549
return;
539550
}
@@ -557,10 +568,7 @@ public void windowLostFocus(WindowEvent e) {
557568
}
558569

559570
private static void addWaylandWindowFocusListenerToWindow(Window window) {
560-
if (!Arrays
561-
.asList(window.getWindowFocusListeners())
562-
.contains(waylandWindowFocusListener)
563-
) {
571+
if (!containsWaylandWindowFocusListener(window)) {
564572
window.addWindowFocusListener(waylandWindowFocusListener);
565573
}
566574
}

test/jdk/javax/swing/JPopupMenu/FocusablePopupDismissTest.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/*
2525
* @test
2626
* @key headful
27-
* @bug 8319103
27+
* @bug 8319103 8342096
2828
* @requires (os.family == "linux")
2929
* @library /java/awt/regtesthelpers
3030
* @build PassFailJFrame
@@ -35,6 +35,7 @@
3535

3636
import javax.swing.JButton;
3737
import javax.swing.JFrame;
38+
import javax.swing.JMenu;
3839
import javax.swing.JPopupMenu;
3940
import javax.swing.JTextField;
4041
import java.awt.Window;
@@ -47,14 +48,15 @@ public class FocusablePopupDismissTest {
4748
4849
Click on the "Click me" button.
4950
50-
If the JTextField popup with "Some text" is not showing on the screen,
51-
click Fail.
51+
A menu should appear next to the window. If you move the cursor over it,
52+
the JTextField popup with "Some text" should appear on the screen.
53+
If it doesn't, click Fail.
5254
5355
The following steps require some focusable system window to be displayed
5456
on the screen. This could be a system settings window, file manager, etc.
5557
5658
Click on the "Click me" button if the popup is not displayed
57-
on the screen.
59+
on the screen, move the mouse pointer over the menu.
5860
5961
While the popup is displayed, click on some other window on the desktop.
6062
If the popup has disappeared, click Pass, otherwise click Fail.
@@ -84,7 +86,10 @@ static List<Window> createTestUI() {
8486
button.addActionListener(e -> {
8587
JPopupMenu popupMenu = new JPopupMenu();
8688
JTextField textField = new JTextField("Some text", 10);
87-
popupMenu.add(textField);
89+
90+
JMenu menu = new JMenu("Menu");
91+
menu.add(textField);
92+
popupMenu.add(menu);
8893
popupMenu.show(button, 0, button.getHeight());
8994
});
9095
frame.pack();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @summary tests if nested menu is displayed on Wayland
27+
* @key headful
28+
* @bug 8342096
29+
* @requires (os.family == "linux")
30+
*/
31+
32+
import javax.swing.JButton;
33+
import javax.swing.JFrame;
34+
import javax.swing.JMenu;
35+
import javax.swing.JPanel;
36+
import javax.swing.JPopupMenu;
37+
import javax.swing.SwingUtilities;
38+
import java.awt.Dimension;
39+
import java.awt.Point;
40+
import java.awt.Robot;
41+
import java.awt.event.InputEvent;
42+
43+
public class NestedFocusablePopupTest {
44+
45+
static volatile JMenu menu;
46+
static volatile JPopupMenu popupMenu;
47+
static volatile JFrame frame;
48+
49+
public static void main(String[] args) throws Exception {
50+
if (System.getenv("WAYLAND_DISPLAY") == null) {
51+
//test is valid only when running on Wayland.
52+
return;
53+
}
54+
55+
Robot robot = new Robot();
56+
robot.setAutoDelay(50);
57+
58+
try {
59+
SwingUtilities.invokeAndWait(NestedFocusablePopupTest::initAndShowGui);
60+
robot.waitForIdle();
61+
robot.delay(500);
62+
63+
Point frameLocation = frame.getLocationOnScreen();
64+
robot.mouseMove(frameLocation.x + frame.getWidth() / 2,
65+
frameLocation.y + frame.getHeight() / 2);
66+
67+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
68+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
69+
70+
robot.waitForIdle();
71+
robot.delay(100);
72+
73+
Point menuLocation = menu.getLocationOnScreen();
74+
robot.mouseMove(menuLocation.x + 5, menuLocation.y + 5);
75+
robot.waitForIdle();
76+
robot.delay(200);
77+
78+
if (!popupMenu.isVisible()) {
79+
throw new RuntimeException("Popup is not visible");
80+
}
81+
} finally {
82+
SwingUtilities.invokeAndWait(frame::dispose);
83+
}
84+
}
85+
86+
87+
private static void initAndShowGui() {
88+
frame = new JFrame("NestedFocusablePopupTest");
89+
JPanel panel = new JPanel();
90+
panel.setPreferredSize(new Dimension(200,180));
91+
92+
93+
popupMenu = new JPopupMenu();
94+
menu = new JMenu("menu to hover");
95+
menu.add(new JButton("JButton"));
96+
popupMenu.add(menu);
97+
98+
panel.setComponentPopupMenu(popupMenu);
99+
frame.add(panel);
100+
frame.pack();
101+
frame.setLocationRelativeTo(null);
102+
frame.setVisible(true);
103+
}
104+
}

0 commit comments

Comments
 (0)