JBR-5720 Wrong modifiers are reported for mouse middle and right buttons' release/clicked events

This commit is contained in:
Dmitry Batrak
2023-06-19 10:48:40 +03:00
parent 8198343f81
commit 64dad2a50a
2 changed files with 126 additions and 3 deletions

View File

@@ -4452,6 +4452,7 @@ class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.LightweightDispatcher");
private static final int BUTTONS_DOWN_MASK;
private static final int BUTTONS_COUNT;
static {
int[] buttonsDownMask = AWTAccessor.getInputEventAccessor().
@@ -4461,6 +4462,7 @@ class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
mask |= buttonDownMask;
}
BUTTONS_DOWN_MASK = mask;
BUTTONS_COUNT = buttonsDownMask.length;
}
LightweightDispatcher(Container nativeContainer) {
@@ -4880,6 +4882,19 @@ class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
return; // mouse is over another hw component or target is disabled
}
int modifiers = e.getModifiersEx();
int button = e.getButton();
if ((id == MouseEvent.MOUSE_ENTERED || id == MouseEvent.MOUSE_EXITED) &&
(button > 0 && button <= BUTTONS_COUNT)) {
// generated entered/exited events logically come before the base event (and are dispatched so),
// hence we need to adjust the modifiers accordingly
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
modifiers &= ~InputEvent.getMaskForButton(button);
} else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
modifiers |= InputEvent.getMaskForButton(button);
}
}
int x = e.getX(), y = e.getY();
Component component;
@@ -4901,7 +4916,7 @@ class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
retargeted = new MouseWheelEvent(target,
id,
e.getWhen(),
e.getModifiersEx() | e.getModifiers(),
modifiers,
x,
y,
e.getXOnScreen(),
@@ -4917,14 +4932,14 @@ class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
retargeted = new MouseEvent(target,
id,
e.getWhen(),
e.getModifiersEx() | e.getModifiers(),
modifiers,
x,
y,
e.getXOnScreen(),
e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
e.getButton());
button);
MouseEventAccessor meAccessor = AWTAccessor.getMouseEventAccessor();
meAccessor.setCausedByTouchEvent(retargeted,
meAccessor.isCausedByTouchEvent(e));

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2023 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 java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
/**
* @test
* @summary Regression test for JBR-5720 Wrong modifiers are reported for mouse middle and right buttons' release/clicked events
* @key headful
*/
public class ReleaseAndClickModifiers {
private static final List<Integer> modifiers = Collections.synchronizedList(new ArrayList<>());
private static Robot robot;
private static JFrame frame;
private static JPanel panel;
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(50);
try {
SwingUtilities.invokeAndWait(ReleaseAndClickModifiers::initUI);
robot.delay(1000);
clickOn(panel, MouseEvent.BUTTON1);
clickOn(panel, MouseEvent.BUTTON2);
clickOn(panel, MouseEvent.BUTTON3);
robot.delay(1000);
if (!List.of(InputEvent.BUTTON1_DOWN_MASK, 0, 0,
InputEvent.BUTTON2_DOWN_MASK, 0, 0,
InputEvent.BUTTON3_DOWN_MASK, 0, 0)
.equals(modifiers)) {
throw new RuntimeException("Wrong modifiers: " + modifiers);
}
} finally {
SwingUtilities.invokeAndWait(ReleaseAndClickModifiers::disposeUI);
}
}
private static void initUI() {
frame = new JFrame("ReleaseAndClickModifiers");
panel = new JPanel();
panel.setPreferredSize(new Dimension(200, 200));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
logModifiers(e);
}
@Override
public void mouseReleased(MouseEvent e) {
logModifiers(e);
}
@Override
public void mouseClicked(MouseEvent e) {
logModifiers(e);
}
private void logModifiers(MouseEvent e) {
modifiers.add(e.getModifiersEx());
}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private static void disposeUI() {
if (frame != null) frame.dispose();
}
private static void clickAt(int x, int y, int button) {
robot.mouseMove(x, y);
int buttonMask = InputEvent.getMaskForButton(button);
robot.mousePress(buttonMask);
robot.mouseRelease(buttonMask);
}
private static void clickOn(Component component, int button) {
Point location = component.getLocationOnScreen();
clickAt(location.x + component.getWidth() / 2, location.y + component.getHeight() / 2, button);
}
}