You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ge...@apache.org on 2021/03/29 20:07:11 UTC

[netbeans] branch master updated: Popup tab switcher should hide when another application steals focus

This is an automated email from the ASF dual-hosted git repository.

geertjan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new e640446  Popup tab switcher should hide when another application steals focus
     new 4cd5a53  Merge pull request #2711 from timboudreau/popup-switcher-fix
e640446 is described below

commit e64044698c335a354a408325d23cad747073d89e
Author: Tim Boudreau <ti...@timboudreau.com>
AuthorDate: Mon Jan 25 18:41:48 2021 -0500

    Popup tab switcher should hide when another application steals focus
    
    Fixes a problem I've run into several times on Mac OS - if another application steals focus
    while the popup switcher is open, the switcher can wind up in a state where it is listening
    twice, and is hidden as soon as you release the mouse button.
    
    Fix is to add AWTEvent.WINDOW_EVENT_MASK to the set of AWTEvents listened for, check that
    there really is no active window (to avoid confusion around heavyweight AWT popups) and
    hide the popup in that case.
---
 .../nbproject/project.properties                   |  2 +-
 .../swing/tabcontrol/ButtonPopupSwitcher.java      | 22 +++++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/platform/o.n.swing.tabcontrol/nbproject/project.properties b/platform/o.n.swing.tabcontrol/nbproject/project.properties
index 24e5c05..5a3258b 100644
--- a/platform/o.n.swing.tabcontrol/nbproject/project.properties
+++ b/platform/o.n.swing.tabcontrol/nbproject/project.properties
@@ -17,7 +17,7 @@
 
 is.autoload=true
 javac.compilerargs=-Xlint:unchecked
-javac.source=1.6
+javac.source=1.7
 javadoc.arch=${basedir}/arch.xml
 javadoc.apichanges=${basedir}/apichanges.xml
 
diff --git a/platform/o.n.swing.tabcontrol/src/org/netbeans/swing/tabcontrol/ButtonPopupSwitcher.java b/platform/o.n.swing.tabcontrol/src/org/netbeans/swing/tabcontrol/ButtonPopupSwitcher.java
index c357cab..635e9e9 100644
--- a/platform/o.n.swing.tabcontrol/src/org/netbeans/swing/tabcontrol/ButtonPopupSwitcher.java
+++ b/platform/o.n.swing.tabcontrol/src/org/netbeans/swing/tabcontrol/ButtonPopupSwitcher.java
@@ -21,11 +21,13 @@ package org.netbeans.swing.tabcontrol;
 
 import java.awt.AWTEvent;
 import java.awt.Component;
+import static java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager;
 import java.awt.Point;
 import java.awt.Toolkit;
 import java.awt.event.AWTEventListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
+import java.awt.event.WindowEvent;
 import java.util.Arrays;
 import javax.swing.BorderFactory;
 import javax.swing.JComponent;
@@ -83,6 +85,8 @@ final class ButtonPopupSwitcher implements MouseInputListener, AWTEventListener,
     private boolean isDragging = true;
 
     private final TabDisplayer displayer;
+    private static final long AWT_EVENT_MASK = AWTEvent.KEY_EVENT_MASK |
+            AWTEvent.WINDOW_EVENT_MASK;
     
     /**
      * Creates and shows the popup with given <code>items</code>. When user
@@ -119,7 +123,7 @@ final class ButtonPopupSwitcher implements MouseInputListener, AWTEventListener,
 
         displayer.getModel().addComplexListDataListener( this );
 
-        Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
+        Toolkit.getDefaultToolkit().addAWTEventListener(this, AWT_EVENT_MASK);
 
         popup = new JPopupMenu();
         popup.setBorderPainted( false );
@@ -300,6 +304,22 @@ final class ButtonPopupSwitcher implements MouseInputListener, AWTEventListener,
                     ((KeyEvent)event).consume();
                 }
             }
+        } else if (event instanceof WindowEvent) {
+            // In the case that some other application grabs focus while the 
+            // popup is open, it is possible to wind up double-listening, resulting
+            // in a popup which appears and disappears as soon as the user releases
+            // the mouse-button, so detect that and auto-hide
+            WindowEvent we = (WindowEvent) event;
+            switch(we.getID()) {
+                case WindowEvent.WINDOW_DEACTIVATED :
+                    // Make sure focus has really left the application, not a
+                    // heavyweight Swing popup for the switcher became the active
+                    // window, which would be a false-positive and cause the
+                    // popup to close as soon as it opens
+                    if (getCurrentKeyboardFocusManager().getActiveWindow() == null) {
+                        hidePopup();
+                    }
+            }
         }
     }
     

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists