8146310: [macosx] com.apple.eawt.Application.setDefaultMenuBar does not initialize screen menu bar

Reviewed-by: serb
This commit is contained in:
Alan Snyder 2018-08-31 18:05:38 -07:00
parent d0a60f53ee
commit 78eb57c7a6
3 changed files with 219 additions and 16 deletions

View File

@ -46,6 +46,7 @@ class _AppMenuBarHandler {
private static native void nativeSetMenuState(final int menu, final boolean visible, final boolean enabled);
private static native void nativeSetDefaultMenuBar(final long menuBarPeer);
private static native void nativeActivateDefaultMenuBar(final long menuBarPeer);
static final _AppMenuBarHandler instance = new _AppMenuBarHandler();
static _AppMenuBarHandler getInstance() {
@ -78,26 +79,18 @@ class _AppMenuBarHandler {
void setDefaultMenuBar(final JMenuBar menuBar) {
installDefaultMenuBar(menuBar);
if (menuBar == null) {
return;
}
}
static boolean isMenuBarActivationNeeded() {
// scan the current frames, and see if any are foreground
final Frame[] frames = Frame.getFrames();
for (final Frame frame : frames) {
if (frame.isVisible() && !isFrameMinimized(frame)) {
return;
return false;
}
}
// if we have no foreground frames, then we have to "kick" the menubar
final JFrame pingFrame = new JFrame();
pingFrame.getRootPane().putClientProperty("Window.alpha", Float.valueOf(0.0f));
pingFrame.setUndecorated(true);
pingFrame.setVisible(true);
pingFrame.toFront();
pingFrame.setVisible(false);
pingFrame.dispose();
return true;
}
static boolean isFrameMinimized(final Frame frame) {
@ -150,6 +143,11 @@ class _AppMenuBarHandler {
// grab the pointer to the CMenuBar, and retain it in native
((CMenuBar) peer).execute(_AppMenuBarHandler::nativeSetDefaultMenuBar);
// if there is no currently active frame, install the default menu bar in the application main menu
if (isMenuBarActivationNeeded()) {
((CMenuBar) peer).execute(_AppMenuBarHandler::nativeActivateDefaultMenuBar);
}
}
void setAboutMenuItemVisible(final boolean present) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, 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
@ -35,10 +35,10 @@
#import <JavaNativeFoundation/JavaNativeFoundation.h>
#import "CPopupMenu.h"
#import "CMenuBar.h"
#import "ThreadUtilities.h"
#import "NSApplicationAWT.h"
#pragma mark App Menu helpers
// The following is a AWT convention?
@ -201,11 +201,11 @@ AWT_ASSERT_APPKIT_THREAD;
self.fPreferencesMenu = (NSMenuItem*)[appMenu itemWithTag:PREFERENCES_TAG];
self.fAboutMenu = (NSMenuItem*)[appMenu itemAtIndex:0];
NSDockTile *dockTile = [NSApp dockTile];
self.fProgressIndicator = [[NSProgressIndicator alloc]
initWithFrame:NSMakeRect(3.f, 0.f, dockTile.size.width - 6.f, 20.f)];
[fProgressIndicator setStyle:NSProgressIndicatorBarStyle];
[fProgressIndicator setIndeterminate:NO];
[[dockTile contentView] addSubview:fProgressIndicator];
@ -824,3 +824,23 @@ JNF_COCOA_ENTER(env);
JNF_COCOA_EXIT(env);
}
/*
* Class: com_apple_eawt__AppMenuBarHandler
* Method: nativeActivateDefaultMenuBar
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_apple_eawt__1AppMenuBarHandler_nativeActivateDefaultMenuBar
(JNIEnv *env, jclass clz, jlong cMenuBarPtr)
{
JNF_COCOA_ENTER(env);
CMenuBar *menu = (CMenuBar *)jlong_to_ptr(cMenuBarPtr);
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
if (menu) {
[CMenuBar activate:menu modallyDisabled:NO];
}
}];
JNF_COCOA_EXIT(env);
}

View File

@ -0,0 +1,185 @@
/*
* Copyright (c) 2018, 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.
*/
/**
* @test
* @key headful
* @bug 8146310
* @summary [macosx] setDefaultMenuBar does not initialize screen menu bar
* @author Alan Snyder
* @run main/othervm TestNoScreenMenuBar
* @requires (os.family == "mac")
*/
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class TestNoScreenMenuBar
{
static TestNoScreenMenuBar theTest;
private Robot robot;
private boolean isApplicationOpened;
private boolean isActionPerformed;
public TestNoScreenMenuBar(String[] args)
{
try {
robot = new Robot();
robot.setAutoDelay(50);
} catch (AWTException ex) {
throw new RuntimeException(ex);
}
if (!(args.length > 0 && args[0].equals("baseline"))) {
// activate another application
openOtherApplication();
robot.delay(500);
}
// The failure mode is installing the default menu bar while the application is inactive
Desktop desktop = Desktop.getDesktop();
desktop.setDefaultMenuBar(createMenuBar());
robot.delay(500);
desktop.requestForeground(true);
robot.delay(500);
}
JMenuBar createMenuBar()
{
JMenuBar mb = new JMenuBar();
// A very long name makes it more likely that the robot will hit the menu
JMenu menu = new JMenu("TestTestTestTestTestTestTestTestTestTest");
mb.add(menu);
JMenuItem item = new JMenuItem("TestTestTestTestTestTestTestTestTestTest");
item.addActionListener(ev -> {
isActionPerformed = true;
});
menu.add(item);
return mb;
}
void dispose()
{
closeOtherApplication();
Desktop.getDesktop().setDefaultMenuBar(null);
}
private void performMenuItemTest()
{
// Find the menu on the screen menu bar
// The location depends upon the application name which is the name of the first menu.
// Unfortunately, the application name can vary based on how the application is run.
// The work around is to make the menu and the menu item names very long.
int menuBarX = 250;
int menuBarY = 11;
int menuItemX = menuBarX;
int menuItemY = 34;
robot.mouseMove(menuBarX, menuBarY);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
robot.mouseMove(menuItemX, menuItemY);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
waitForAction();
}
private synchronized void waitForAction()
{
try {
for (int i = 0; i < 10; i++) {
if (isActionPerformed) {
return;
}
wait(100);
}
} catch (InterruptedException ex) {
}
throw new RuntimeException("Test failed: menu item action was not performed");
}
private void openOtherApplication() {
String[] cmd = { "/usr/bin/open", "/Applications/System Preferences.app" };
execute(cmd);
isApplicationOpened = true;
}
private void closeOtherApplication() {
if (isApplicationOpened) {
String[] cmd = { "/usr/bin/osascript", "-e", "tell application \"System Preferences\" to close window 1" };
execute(cmd);
}
}
private void execute(String[] cmd) {
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch (IOException | InterruptedException ex) {
throw new RuntimeException("Unable to execute command");
}
}
private static void runSwing(Runnable r)
{
try {
SwingUtilities.invokeAndWait(r);
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
if (!System.getProperty("os.name").contains("OS X")) {
System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
return;
}
System.setProperty("apple.laf.useScreenMenuBar", "true");
try {
runSwing(() -> theTest = new TestNoScreenMenuBar(args));
theTest.performMenuItemTest();
} finally {
if (theTest != null) {
runSwing(() -> theTest.dispose());
}
}
}
}