Skip to content

Commit b697b48

Browse files
author
Alisen Chung
committed
8315655: [macos] Right click and dragging over a component with a popup menu will open the popup
Reviewed-by: dnguyen, psadhukhan
1 parent 2c1da6c commit b697b48

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -87,7 +87,7 @@ void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
8787
jmodifiers |= MouseEvent.getMaskForButton(jbuttonNumber);
8888
}
8989

90-
boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
90+
boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers, jeventType);
9191

9292
eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
9393
x, y, absX, absY, jmodifiers, jclickCount,

src/java.desktop/macosx/classes/sun/lwawt/macosx/CTrayIcon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private void handleMouseEvent(NSEvent nsEvent) {
253253

254254
int jmodifiers = NSEvent.nsToJavaModifiers(
255255
nsEvent.getModifierFlags());
256-
boolean isPopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
256+
boolean isPopupTrigger = NSEvent.isPopupTrigger(jmodifiers, jeventType);
257257

258258
int eventButtonMask = (jbuttonNumber > 0)?
259259
MouseEvent.getMaskForButton(jbuttonNumber) : 0;

src/java.desktop/macosx/classes/sun/lwawt/macosx/NSEvent.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -269,7 +269,12 @@ static int nsToJavaEventType(int nsEventType) {
269269
*/
270270
static native char nsToJavaChar(char nsChar, int modifierFlags, boolean spaceKeyTyped);
271271

272-
static boolean isPopupTrigger(int jmodifiers) {
272+
static boolean isPopupTrigger(int jmodifiers, int jeventType) {
273+
if (jeventType != MouseEvent.MOUSE_PRESSED
274+
&& jeventType != MouseEvent.MOUSE_RELEASED) {
275+
return false;
276+
}
277+
273278
final boolean isRightButtonDown = ((jmodifiers & InputEvent.BUTTON3_DOWN_MASK) != 0);
274279
final boolean isLeftButtonDown = ((jmodifiers & InputEvent.BUTTON1_DOWN_MASK) != 0);
275280
final boolean isControlDown = ((jmodifiers & InputEvent.CTRL_DOWN_MASK) != 0);
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
import java.awt.Dimension;
25+
import java.awt.Point;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
28+
import javax.swing.JFrame;
29+
import javax.swing.JLabel;
30+
import javax.swing.JPanel;
31+
import javax.swing.JPopupMenu;
32+
import javax.swing.SwingUtilities;
33+
import javax.swing.event.PopupMenuEvent;
34+
import javax.swing.event.PopupMenuListener;
35+
36+
/*
37+
* @test
38+
* @bug 8315655
39+
* @summary Verifies Right click and dragging over a component with a popup menu will not open the popup
40+
* @key headful
41+
* @run main MouseDragPopupTest
42+
*/
43+
public class MouseDragPopupTest {
44+
static JFrame frame;
45+
static JPanel panel;
46+
static Robot robot;
47+
static volatile boolean failed;
48+
static volatile Point srcPoint;
49+
static volatile Dimension d;
50+
51+
public static void main(String[] args) throws Exception {
52+
try {
53+
robot = new Robot();
54+
robot.setAutoWaitForIdle(true);
55+
robot.setAutoDelay(100);
56+
57+
SwingUtilities.invokeAndWait(() -> {
58+
createAndShowGUI();
59+
});
60+
robot.delay(1000);
61+
62+
SwingUtilities.invokeAndWait(() -> {
63+
srcPoint = frame.getLocationOnScreen();
64+
d = frame.getSize();
65+
});
66+
srcPoint.translate(2 * d.width / 3, 3 * d.height / 4);
67+
68+
final Point dstPoint = new Point(srcPoint);
69+
dstPoint.translate(4 * d.width / 15, 0);
70+
71+
robot.mouseMove(srcPoint.x, srcPoint.y);
72+
73+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
74+
75+
while (!srcPoint.equals(dstPoint)) {
76+
srcPoint.translate(sign(dstPoint.x - srcPoint.x), 0);
77+
robot.mouseMove(srcPoint.x, srcPoint.y);
78+
}
79+
80+
if (failed) {
81+
throw new RuntimeException("Popup was shown, Test Failed.");
82+
}
83+
} finally {
84+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
85+
SwingUtilities.invokeAndWait(() -> {
86+
if (frame != null) {
87+
frame.dispose();
88+
}
89+
});
90+
}
91+
}
92+
93+
public static int sign(int n) {
94+
return n < 0 ? -1 : n == 0 ? 0 : 1;
95+
}
96+
97+
static void createAndShowGUI() {
98+
frame = new JFrame("MouseDragPopupTest");
99+
panel = new JPanel();
100+
JPanel innerPanel = new JPanel();
101+
JPopupMenu menu = new JPopupMenu();
102+
103+
menu.addPopupMenuListener(new PopupMenuListener() {
104+
@Override
105+
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
106+
failed = true;
107+
}
108+
109+
@Override
110+
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
111+
112+
@Override
113+
public void popupMenuCanceled(PopupMenuEvent e) {}
114+
});
115+
116+
menu.add("This should not appear");
117+
innerPanel.setComponentPopupMenu(menu);
118+
119+
panel.add(new JLabel("Right click and drag from here"));
120+
panel.add(innerPanel);
121+
panel.add(new JLabel("to here"));
122+
123+
frame.add(panel);
124+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
125+
frame.pack();
126+
frame.setLocationRelativeTo(null);
127+
frame.setVisible(true);
128+
}
129+
}

0 commit comments

Comments
 (0)