changeset 10559:38826887e440

8167139: Menu's selection disappears on collapsing Reviewed-by: aghaisas
author pkbalakr
date Thu, 13 Jul 2017 15:43:14 +1200
parents 0a97c117cb9d
children 52434928052b
files modules/javafx.controls/src/main/java/javafx/scene/control/skin/MenuBarSkin.java modules/javafx.controls/src/test/java/test/javafx/scene/control/MenuBarTest.java
diffstat 2 files changed, 45 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/skin/MenuBarSkin.java	Wed Jul 12 12:38:48 2017 -0700
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/skin/MenuBarSkin.java	Thu Jul 13 15:43:14 2017 +1200
@@ -956,8 +956,11 @@
             });
             menuButton.showingProperty().addListener((observable, oldValue, isShowing) -> {
                 if (isShowing) {
+                    if(openMenuButton == null && focusedMenuIndex != -1)
+                        openMenuButton = (MenuBarButton)container.getChildren().get(focusedMenuIndex);
+
                     if (openMenuButton != null && openMenuButton != menuButton) {
-                        openMenuButton.hide();
+                        openMenuButton.clearHover();
                     }
                     openMenuButton = menuButton;
                     showMenu(menu);
@@ -1051,11 +1054,6 @@
         if (openMenu != null) {
             openMenu.hide();
             openMenu = null;
-            openMenuButton = (MenuBarButton)container.getChildren().get(focusedMenuIndex);
-            openMenuButton.clearHover();
-            openMenuButton.disarm();
-            openMenuButton = null;
-            menuModeEnd();
         }
     }
 
@@ -1152,6 +1150,7 @@
          for(Node n : container.getChildren()) {
             if (n.isHover()) {
                 ((MenuBarButton)n).clearHover();
+                ((MenuBarButton)n).disarm();
                 return;
             }
         }
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/MenuBarTest.java	Wed Jul 12 12:38:48 2017 -0700
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/MenuBarTest.java	Thu Jul 13 15:43:14 2017 +1200
@@ -618,4 +618,44 @@
         MenuBar menuBar = new MenuBar();
         MenuBarSkin menuBarSkin = new MenuBarSkin(menuBar);
     }
+
+    @Test public void testMenuButtonMouseSelection() {
+        VBox root = new VBox();
+        Menu menu1 = new Menu("Menu1");
+        MenuItem menuItem1 = new MenuItem("MenuItem1");
+
+        menu1.getItems().add(menuItem1);
+
+        menuBar.getMenus().addAll(menu1);
+        root.getChildren().addAll(menuBar);
+        startApp(root);
+        tk.firePulse();
+
+        MenuBarSkin skin = (MenuBarSkin)menuBar.getSkin();
+        assertTrue(skin != null);
+
+        double xval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinX();
+        double yval = (menuBar.localToScene(menuBar.getLayoutBounds())).getMinY();
+
+        MenuButton mb = MenuBarSkinShim.getNodeForMenu(skin, 0);
+        mb.getScene().getWindow().requestFocus();
+
+        // Expand menu1 by mouse
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+20, yval+20));
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+20, yval+20));
+        assertTrue(menu1.isShowing());
+
+        // Collapse menu1 by mouse
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval+20, yval+20));
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_RELEASED, xval+20, yval+20));
+        assertFalse(menu1.isShowing());
+
+        // check if focusedMenuIndex is 0 (menu1 is still in selected state).
+        int focusedIndex = MenuBarSkinShim.getFocusedIndex(skin);
+        assertEquals(focusedIndex, 0);
+    }
 }