-
Notifications
You must be signed in to change notification settings - Fork 585
8372398: [macOS] Shown ContextMenu does not close when macOS system menu is clicked #2102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46ff026
Close popup windows on macOS when clicking over the system menu bar
jperedadnr c870109
Address feedback
jperedadnr 6b86e15
Rework menuWillOpenHandler to apply it only to auto-hide popups
jperedadnr a31a617
Remove notification and check if there is a grab instead
jperedadnr 2f114df
undo unneeded changes
jperedadnr 37d54b0
undo unneeded changes
jperedadnr 88f0036
Add system test
jperedadnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
tests/system/src/test/java/test/robot/javafx/scene/SystemMenuBarClickEventsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. | ||
| * 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. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package test.robot.javafx.scene; | ||
|
|
||
| import com.sun.javafx.PlatformUtil; | ||
| import javafx.application.Application; | ||
| import javafx.application.Platform; | ||
| import javafx.geometry.Bounds; | ||
| import javafx.geometry.Pos; | ||
| import javafx.scene.Scene; | ||
| import javafx.scene.control.ContextMenu; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.control.Menu; | ||
| import javafx.scene.control.MenuBar; | ||
| import javafx.scene.control.MenuItem; | ||
| import javafx.scene.input.MouseButton; | ||
| import javafx.scene.layout.VBox; | ||
| import javafx.scene.robot.Robot; | ||
| import javafx.stage.Stage; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Assumptions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import test.util.Util; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
|
|
||
| /** | ||
| * Tests that the system menu bar consumes a mouse click event when there is an auto-hide popup window | ||
| * showing in the active stage, and processes the event when there is a non-auto-hide popup window showing. | ||
| */ | ||
| public class SystemMenuBarClickEventsTest { | ||
|
|
||
| private static final CountDownLatch startupLatch = new CountDownLatch(1); | ||
| private static volatile Menu menu; | ||
| private static volatile ContextMenu contextMenu; | ||
| private static volatile Label label; | ||
|
|
||
| private static final int DELAY = 100; | ||
|
|
||
| // Estimated x, y offset for the menu in the macOS system menu bar (after the Apple menu and java menu). | ||
| private static final int MENU_BAR_X = 140; | ||
| private static final int MENU_BAR_Y = 10; | ||
|
|
||
| private static Robot robot; | ||
|
|
||
| @BeforeAll | ||
| static void initFX() throws Exception { | ||
| Util.launch(startupLatch, TestApp.class); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void exit() { | ||
| Util.shutdown(); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that the system menu bar consumes a mouse click event when there is an | ||
| * auto-hide popup window showing in the active stage: the popup window gets dismissed, | ||
| * and the system menu bar doesn't show the menu. | ||
| */ | ||
| @Test | ||
| void testSystemMenuBarConsumesClickEvent() { | ||
| Assumptions.assumeTrue(PlatformUtil.isMac(), "System menu bar tests only apply to macOS"); | ||
|
|
||
| // set auto-hide popup | ||
| Util.runAndWait(() -> contextMenu.setAutoHide(true)); | ||
|
|
||
| // Right-click on the label, the context menu shows up | ||
| Util.runAndWait(() -> { | ||
| Bounds bounds = label.localToScreen(label.getBoundsInLocal()); | ||
| double x = bounds.getMinX() + bounds.getWidth() / 2; | ||
| double y = bounds.getMinY() + bounds.getHeight() / 2; | ||
| robot.mouseMove((int) x, (int) y); | ||
| robot.mouseClick(MouseButton.SECONDARY); | ||
| }); | ||
|
|
||
| Assertions.assertTrue(contextMenu.isShowing(), "Context menu should be showing after right-click"); | ||
|
|
||
| // Click on the system menu bar, in order to try to show the menu | ||
| Util.runAndWait(() -> { | ||
| robot.mouseMove(MENU_BAR_X, MENU_BAR_Y); | ||
| robot.mouseClick(MouseButton.PRIMARY); | ||
| }); | ||
| Util.sleep(DELAY); | ||
|
|
||
| Assertions.assertFalse(menu.isShowing(), "System menu should not be showing"); | ||
| Assertions.assertFalse(contextMenu.isShowing(), "Context menu should not be showing"); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies that the system menu bar processes a mouse click event when there is a | ||
| * non-auto-hide popup window showing in the active stage: the popup remains visible, | ||
| * and the system menu bar shows the menu. | ||
| */ | ||
| @Test | ||
| void testSystemMenuBarProcessesClickEvent() { | ||
| Assumptions.assumeTrue(PlatformUtil.isMac(), "System menu bar tests only apply to macOS"); | ||
|
|
||
| // set non-auto-hide popup | ||
| Util.runAndWait(() -> contextMenu.setAutoHide(false)); | ||
|
|
||
| // Right-click on the label, the context menu shows up | ||
| Util.runAndWait(() -> { | ||
| Bounds bounds = label.localToScreen(label.getBoundsInLocal()); | ||
| double x = bounds.getMinX() + bounds.getWidth() / 2; | ||
| double y = bounds.getMinY() + bounds.getHeight() / 2; | ||
| robot.mouseMove((int) x, (int) y); | ||
| robot.mouseClick(MouseButton.SECONDARY); | ||
| }); | ||
|
|
||
| Assertions.assertTrue(contextMenu.isShowing(), "Context menu should be showing after right-click"); | ||
|
|
||
| // Click on the system menu bar, in order to open the menu | ||
| Util.runAndWait(() -> { | ||
| robot.mouseMove(MENU_BAR_X, MENU_BAR_Y); | ||
| robot.mouseClick(MouseButton.PRIMARY); | ||
| }); | ||
| Util.sleep(DELAY); | ||
|
|
||
| Assertions.assertTrue(menu.isShowing(), "System menu should be showing"); | ||
| Assertions.assertTrue(contextMenu.isShowing(), "Context menu should be showing after system menu bar click"); | ||
|
|
||
| // hide system menu | ||
| Util.runAndWait(() -> menu.hide()); | ||
| Util.sleep(DELAY); | ||
| } | ||
|
|
||
| public static class TestApp extends Application { | ||
|
|
||
| @Override | ||
| public void start(Stage stage) { | ||
| robot = new Robot(); | ||
|
|
||
| // System menu | ||
| MenuItem fileItem1 = new MenuItem("file item 1"); | ||
| MenuItem fileItem2 = new MenuItem("file item 2"); | ||
| MenuItem fileItem3 = new MenuItem("file item 3"); | ||
| menu = new Menu("TestMenu", null, fileItem1, fileItem2, fileItem3); | ||
| MenuBar menuBar = new MenuBar(menu); | ||
| menuBar.setUseSystemMenuBar(true); | ||
|
|
||
| // Context menu | ||
| MenuItem menuItem1 = new MenuItem("item 1"); | ||
| MenuItem menuItem2 = new MenuItem("item 2"); | ||
| MenuItem menuItem3 = new MenuItem("item 3"); | ||
| contextMenu = new ContextMenu(menuItem1, menuItem2, menuItem3); | ||
|
|
||
| label = new Label("System Menu Bar Mouse Click Test"); | ||
| label.setContextMenu(contextMenu); | ||
|
|
||
| VBox root = new VBox(menuBar, label); | ||
| root.setAlignment(Pos.CENTER); | ||
|
|
||
| Scene scene = new Scene(root, 300, 200); | ||
| stage.setScene(scene); | ||
| stage.setOnShown(_ -> Platform.runLater(startupLatch::countDown)); | ||
| stage.show(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.