Blame SOURCES/jdk8231991-mouse_wheel_focus.patch

0b657e
# HG changeset patch
0b657e
# User neugens
0b657e
# Date 1573570464 -3600
0b657e
#      Tue Nov 12 15:54:24 2019 +0100
0b657e
# Node ID d5af26ef7b959383e4a79b0aa96f0527fc3a9f4c
0b657e
# Parent  571089680cb28b3c6399f3a95e6ebcb4593b792c
0b657e
8231991: Mouse wheel change focus on awt/swing windows
0b657e
Summary: Avoid focus logic when only mouse wheel is moved up/down.
0b657e
Reviewed-by: serb, dmarkov
0b657e
0b657e
diff --git openjdk.orig/jdk/src/solaris/classes/sun/awt/X11/XBaseWindow.java openjdk/jdk/src/solaris/classes/sun/awt/X11/XBaseWindow.java
0b657e
--- openjdk.orig/jdk/src/solaris/classes/sun/awt/X11/XBaseWindow.java
0b657e
+++ openjdk/jdk/src/solaris/classes/sun/awt/X11/XBaseWindow.java
0b657e
@@ -1004,29 +1004,37 @@
0b657e
          * InputEvent.BUTTON_DOWN_MASK.
0b657e
          * One more bit is reserved for FIRST_HIGH_BIT.
0b657e
          */
0b657e
-        if (xbe.get_button() > SunToolkit.MAX_BUTTONS_SUPPORTED) {
0b657e
+        int theButton = xbe.get_button();
0b657e
+        if (theButton > SunToolkit.MAX_BUTTONS_SUPPORTED) {
0b657e
             return;
0b657e
         }
0b657e
         int buttonState = 0;
0b657e
         buttonState = xbe.get_state() & XConstants.ALL_BUTTONS_MASK;
0b657e
-        switch (xev.get_type()) {
0b657e
-        case XConstants.ButtonPress:
0b657e
-            if (buttonState == 0) {
0b657e
-                XWindowPeer parent = getToplevelXWindow();
0b657e
-                // See 6385277, 6981400.
0b657e
-                if (parent != null && parent.isFocusableWindow()) {
0b657e
-                    // A click in a client area drops the actual focused window retaining.
0b657e
-                    parent.setActualFocusedWindow(null);
0b657e
-                    parent.requestWindowFocus(xbe.get_time(), true);
0b657e
-                }
0b657e
-                XAwtState.setAutoGrabWindow(this);
0b657e
+
0b657e
+        boolean isWheel = (theButton != XConstants.MouseWheelUp ||
0b657e
+                           theButton != XConstants.MouseWheelDown);
0b657e
+
0b657e
+        // don't give focus if it's just the mouse wheel turning
0b657e
+        if (!isWheel) {
0b657e
+            switch (xev.get_type()) {
0b657e
+                case XConstants.ButtonPress:
0b657e
+                    if (buttonState == 0) {
0b657e
+                        XWindowPeer parent = getToplevelXWindow();
0b657e
+                        // See 6385277, 6981400.
0b657e
+                        if (parent != null && parent.isFocusableWindow()) {
0b657e
+                            // A click in a client area drops the actual focused window retaining.
0b657e
+                            parent.setActualFocusedWindow(null);
0b657e
+                            parent.requestWindowFocus(xbe.get_time(), true);
0b657e
+                        }
0b657e
+                        XAwtState.setAutoGrabWindow(this);
0b657e
+                    }
0b657e
+                    break;
0b657e
+                case XConstants.ButtonRelease:
0b657e
+                    if (isFullRelease(buttonState, xbe.get_button())) {
0b657e
+                        XAwtState.setAutoGrabWindow(null);
0b657e
+                    }
0b657e
+                    break;
0b657e
             }
0b657e
-            break;
0b657e
-        case XConstants.ButtonRelease:
0b657e
-            if (isFullRelease(buttonState, xbe.get_button())) {
0b657e
-                XAwtState.setAutoGrabWindow(null);
0b657e
-            }
0b657e
-            break;
0b657e
         }
0b657e
     }
0b657e
     public void handleMotionNotify(XEvent xev) {
0b657e
diff --git openjdk.orig/jdk/src/solaris/classes/sun/awt/X11/XConstants.java openjdk/jdk/src/solaris/classes/sun/awt/X11/XConstants.java
0b657e
--- openjdk.orig/jdk/src/solaris/classes/sun/awt/X11/XConstants.java
0b657e
+++ openjdk/jdk/src/solaris/classes/sun/awt/X11/XConstants.java
0b657e
@@ -206,6 +206,11 @@
0b657e
 
0b657e
     public static final int buttons [] = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
0b657e
 
0b657e
+    // those should probably be wrapped in a method or such
0b657e
+    // as it may be possible to remap them via x11 configuration files
0b657e
+    public static final int MouseWheelUp = buttons[4];
0b657e
+    public static final int MouseWheelDown = buttons[5];
0b657e
+
0b657e
     /* Notify modes */
0b657e
 
0b657e
     public static final int NotifyNormal = 0 ;
0b657e
diff --git openjdk/jdk/test/java/awt/event/MouseWheelEvent/WheelModifier/MouseWheelOnBackgroundComponent.java openjdk/jdk/test/java/awt/event/MouseWheelEvent/WheelModifier/MouseWheelOnBackgroundComponent.java
0b657e
new file mode 100644
0b657e
--- /dev/null
0b657e
+++ openjdk/jdk/test/java/awt/event/MouseWheelEvent/WheelModifier/MouseWheelOnBackgroundComponent.java
0b657e
@@ -0,0 +1,170 @@
0b657e
+/*
0b657e
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
0b657e
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0b657e
+ *
0b657e
+ * This code is free software; you can redistribute it and/or modify it
0b657e
+ * under the terms of the GNU General Public License version 2 only, as
0b657e
+ * published by the Free Software Foundation.
0b657e
+ *
0b657e
+ * This code is distributed in the hope that it will be useful, but WITHOUT
0b657e
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0b657e
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0b657e
+ * version 2 for more details (a copy is included in the LICENSE file that
0b657e
+ * accompanied this code).
0b657e
+ *
0b657e
+ * You should have received a copy of the GNU General Public License version
0b657e
+ * 2 along with this work; if not, write to the Free Software Foundation,
0b657e
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0b657e
+ *
0b657e
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0b657e
+ * or visit www.oracle.com if you need additional information or have any
0b657e
+ * questions.
0b657e
+ */
0b657e
+
0b657e
+/*
0b657e
+  @test
0b657e
+  @key headful
0b657e
+  @bug 8231991
0b657e
+  @summary Mouse wheel change focus on awt/swing windows
0b657e
+  @run main MouseWheelOnBackgroundComponent
0b657e
+*/
0b657e
+
0b657e
+import javax.swing.*;
0b657e
+import java.awt.*;
0b657e
+import java.awt.event.FocusEvent;
0b657e
+import java.awt.event.FocusListener;
0b657e
+import java.awt.event.WindowEvent;
0b657e
+
0b657e
+/**
0b657e
+ * MouseWheelOnBackgroundComponent
0b657e
+ *
0b657e
+ * Tests that wheel events don't change focus on background components
0b657e
+ */
0b657e
+public class MouseWheelOnBackgroundComponent {
0b657e
+
0b657e
+    private static final String FG = "Foreground";
0b657e
+    private static final String BG = "Background";
0b657e
+
0b657e
+    private static final String SCROLL_PANE = "scroller_";
0b657e
+    private static final String TEXT_PANE = "text_";
0b657e
+
0b657e
+    private static JFrame background;
0b657e
+    private static JFrame foreground;
0b657e
+
0b657e
+    private static final String LOREM_IPSUM = "Sed ut perspiciatis unde omnis iste natus " +
0b657e
+            "error sit voluptatem accusantium doloremque laudantium, totam " +
0b657e
+            "rem aperiam, eaque ipsa quae ab illo inventore veritatis et " +
0b657e
+            "quasi architecto beatae vitae dicta sunt explicabo. Nemo enim " +
0b657e
+            "ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, " +
0b657e
+            "sed quia consequuntur magni dolores eos qui ratione voluptatem sequi " +
0b657e
+            "nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit " +
0b657e
+            "amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora " +
0b657e
+            "incidunt ut labore et dolore magnam aliquam quaerat voluptatem. " +
0b657e
+            "Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis " +
0b657e
+            "suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis " +
0b657e
+            "autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil " +
0b657e
+            "molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla " +
0b657e
+            "pariatur?";
0b657e
+
0b657e
+    private static class FocusReporter implements FocusListener {
0b657e
+
0b657e
+        private JComponent pane;
0b657e
+
0b657e
+        Component lastFocusedComponent;
0b657e
+
0b657e
+        public FocusReporter() {
0b657e
+        }
0b657e
+
0b657e
+        @Override
0b657e
+        public void focusGained(FocusEvent e) {
0b657e
+            lastFocusedComponent = e.getComponent();
0b657e
+        }
0b657e
+
0b657e
+        @Override
0b657e
+        public void focusLost(FocusEvent e) {
0b657e
+            lastFocusedComponent = null;
0b657e
+        }
0b657e
+    }
0b657e
+
0b657e
+    private static FocusReporter reporter;
0b657e
+
0b657e
+    public static void main(String[] args) throws Exception  {
0b657e
+
0b657e
+        boolean passed = false;
0b657e
+
0b657e
+        SwingUtilities.invokeAndWait(() -> {
0b657e
+            reporter = new FocusReporter();
0b657e
+
0b657e
+            foreground = createFrame(FG, 100, 0, reporter);
0b657e
+            background = createFrame(BG, 0, 100, reporter);
0b657e
+
0b657e
+            background.pack();
0b657e
+            background.setVisible(true);
0b657e
+
0b657e
+            foreground.pack();
0b657e
+            foreground.setVisible(true);
0b657e
+        });
0b657e
+
0b657e
+        SwingUtilities.invokeAndWait(() -> {
0b657e
+            foreground.toFront();
0b657e
+        });
0b657e
+
0b657e
+        Robot robot = new Robot();
0b657e
+        robot.waitForIdle();
0b657e
+
0b657e
+        robot.mouseMove(50, 300);
0b657e
+        robot.waitForIdle();
0b657e
+
0b657e
+        robot.mouseWheel(-100);
0b657e
+        robot.waitForIdle();
0b657e
+
0b657e
+        String shouldBeFocusedComponentName = TEXT_PANE + FG;
0b657e
+        Component actual = reporter.lastFocusedComponent;
0b657e
+        if (reporter.lastFocusedComponent != null &&
0b657e
+            shouldBeFocusedComponentName.equals(actual.getName()))
0b657e
+        {
0b657e
+            passed = true;
0b657e
+        }
0b657e
+
0b657e
+        robot.waitForIdle();
0b657e
+
0b657e
+        SwingUtilities.invokeAndWait(() -> {
0b657e
+            foreground.dispatchEvent(new WindowEvent(foreground, WindowEvent.WINDOW_CLOSING));
0b657e
+            background.dispatchEvent(new WindowEvent(background, WindowEvent.WINDOW_CLOSING));
0b657e
+        });
0b657e
+
0b657e
+        robot.waitForIdle();
0b657e
+
0b657e
+        if (!passed) {
0b657e
+            throw new RuntimeException("Wrong component has focus: " + actual);
0b657e
+        }
0b657e
+    }
0b657e
+
0b657e
+    private static JFrame createFrame(String name, int x, int y, FocusReporter reporter) {
0b657e
+        JFrame frame = new JFrame(name);
0b657e
+        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
0b657e
+        frame.setBounds(x, y, 600, 600);
0b657e
+        frame.setPreferredSize(new Dimension(600, 600));
0b657e
+
0b657e
+        JTextArea text = new JTextArea();
0b657e
+        text.setText(LOREM_IPSUM);
0b657e
+        for (int i = 0; i < 10; i++) {
0b657e
+            text.append(LOREM_IPSUM);
0b657e
+        }
0b657e
+
0b657e
+        text.setLineWrap(true);
0b657e
+        text.setName(TEXT_PANE + name);
0b657e
+        text.addFocusListener(reporter);
0b657e
+
0b657e
+        JScrollPane scroller = new JScrollPane(text);
0b657e
+        scroller.setName(SCROLL_PANE + name);
0b657e
+        scroller.setWheelScrollingEnabled(true);
0b657e
+        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
0b657e
+        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
0b657e
+        scroller.addFocusListener(reporter);
0b657e
+
0b657e
+        frame.add(scroller);
0b657e
+
0b657e
+        return frame;
0b657e
+    }
0b657e
+}