Skip to content

Commit 20d5609

Browse files
committed
8325435: [macos] Menu or JPopupMenu not closed when main window is resized
Backport-of: 1c514b34c0260823e70f209996ac933a76ac34c2
1 parent 9af7cbf commit 20d5609

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,11 @@ - (void)sendEvent:(NSEvent *)event {
10241024
NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame];
10251025

10261026
// Check if the click happened in the non-client area (title bar)
1027-
if (p.y >= (frame.origin.y + contentRect.size.height)) {
1027+
// Also, non-client area includes the edges at left, right and botton of frame
1028+
if ((p.y >= (frame.origin.y + contentRect.size.height)) ||
1029+
(p.x >= (frame.origin.x + contentRect.size.width - 3)) ||
1030+
(fabs(frame.origin.x - p.x) < 3) ||
1031+
(fabs(frame.origin.y - p.y) < 3)) {
10281032
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
10291033
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
10301034
if (platformWindow != NULL) {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
/*
27+
* @test
28+
* @bug 8267374
29+
* @key headful
30+
* @requires (os.family == "mac")
31+
* @summary Verifies menu closes when main window is resized
32+
* @run main TestUngrab
33+
*/
34+
35+
import java.awt.Point;
36+
import java.awt.Dimension;
37+
import java.awt.Robot;
38+
import java.awt.event.ActionEvent;
39+
import java.awt.event.InputEvent;
40+
import java.awt.event.KeyEvent;
41+
import java.awt.event.MouseEvent;
42+
import javax.swing.JFrame;
43+
import javax.swing.JMenu;
44+
import javax.swing.JMenuBar;
45+
import javax.swing.JMenuItem;
46+
import javax.swing.SwingUtilities;
47+
48+
public class TestUngrab {
49+
/**
50+
* Menu (JMenuBar and JPopupMenu) not hidden when resizing the window.
51+
* -> UngrabEvent not sent.
52+
*/
53+
static JMenu menu;
54+
static JFrame frame;
55+
static volatile Point loc;
56+
static volatile Point point;
57+
static volatile Dimension dim;
58+
static volatile int width;
59+
static volatile int height;
60+
static volatile boolean popupVisible;
61+
62+
private static void createAndShowGUI() {
63+
frame = new JFrame();
64+
JMenuBar mb = new JMenuBar();
65+
menu = new JMenu("Menu");
66+
menu.add(new JMenuItem("Item 1"));
67+
menu.add(new JMenuItem("Item 2"));
68+
mb.add(menu);
69+
70+
frame.setJMenuBar(mb);
71+
frame.setSize(300, 300);
72+
frame.setLocationRelativeTo(null);
73+
frame.setVisible(true);
74+
}
75+
76+
public static void main(String[] args) throws Exception {
77+
try {
78+
Robot robot = new Robot();
79+
robot.setAutoDelay(100);
80+
SwingUtilities.invokeAndWait(() -> createAndShowGUI());
81+
robot.waitForIdle();
82+
robot.delay(1000);
83+
84+
SwingUtilities.invokeAndWait(() -> {
85+
point = menu.getLocationOnScreen();
86+
dim = menu.getSize();
87+
});
88+
robot.mouseMove(point.x + dim.width / 2, point.y + dim.height / 2);
89+
robot.waitForIdle();
90+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
91+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
92+
robot.waitForIdle();
93+
94+
SwingUtilities.invokeAndWait(() -> {
95+
loc = frame.getLocationOnScreen();
96+
width = frame.getWidth();
97+
height = frame.getHeight();
98+
});
99+
robot.mouseMove(loc.x + width, loc.y + height);
100+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
101+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
102+
robot.delay(1000);
103+
104+
SwingUtilities.invokeAndWait(() -> {
105+
popupVisible = menu.isPopupMenuVisible();
106+
});
107+
System.out.println("isPopupMenuVisible " + popupVisible);
108+
if (popupVisible) {
109+
throw new RuntimeException("popup menu not closed on resize");
110+
}
111+
} finally {
112+
SwingUtilities.invokeAndWait(() -> {
113+
if (frame != null) {
114+
frame.dispose();
115+
}
116+
});
117+
}
118+
}
119+
120+
}

0 commit comments

Comments
 (0)