8166683: On macOS (Mac OS X) getting a ScreenMenuBar when not running "com.apple.laf.AquaLookAndFeel"
authorazvegint
Wed, 07 Dec 2016 15:52:33 -0800
changeset 42739 36f46e978c31
parent 42738 67c248b945d5
child 42740 ad57210989cd
8166683: On macOS (Mac OS X) getting a ScreenMenuBar when not running "com.apple.laf.AquaLookAndFeel" Reviewed-by: prr, serb
jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuBarUI.java
jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java
jdk/src/java.desktop/share/classes/javax/swing/JMenuBar.java
jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java
--- a/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuBarUI.java	Wed Dec 07 17:54:55 2016 +0300
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuBarUI.java	Wed Dec 07 15:52:33 2016 -0800
@@ -34,10 +34,18 @@
 
 import sun.lwawt.macosx.LWCToolkit;
 import sun.security.action.GetBooleanAction;
-import sun.security.action.GetPropertyAction;
 
 // MenuBar implementation for Mac L&F
 public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {
+
+    static {
+        java.security.AccessController.doPrivileged(
+                (java.security.PrivilegedAction<Void>) () -> {
+            System.loadLibrary("osxui");
+            return null;
+        });
+    }
+
     // Utilities
     public void uninstallUI(final JComponent c) {
         if (fScreenMenuBar != null) {
@@ -134,7 +142,7 @@
     ScreenMenuBar fScreenMenuBar;
     boolean useScreenMenuBar = getScreenMenuBarProperty();
 
-    static boolean getScreenMenuBarProperty() {
+    public static boolean getScreenMenuBarProperty() {
         // Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit
         if (LWCToolkit.isEmbedded()) return false;
         if (AccessController.doPrivileged(
--- a/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Wed Dec 07 17:54:55 2016 +0300
+++ b/jdk/src/java.desktop/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Wed Dec 07 15:52:33 2016 -0800
@@ -25,6 +25,7 @@
 
 package sun.lwawt.macosx;
 
+import com.apple.laf.AquaMenuBarUI;
 import java.awt.peer.TaskbarPeer;
 import java.awt.*;
 import java.awt.datatransfer.Clipboard;
@@ -43,6 +44,7 @@
 import java.util.*;
 import java.util.concurrent.Callable;
 import java.net.MalformedURLException;
+import javax.swing.UIManager;
 
 import sun.awt.*;
 import sun.awt.datatransfer.DataTransferer;
@@ -935,4 +937,13 @@
     protected PlatformWindow getPlatformWindowUnderMouse() {
         return CPlatformWindow.nativeGetTopmostPlatformWindowUnderMouse();
     }
+
+    @Override
+    public void updateScreenMenuBarUI() {
+        if (AquaMenuBarUI.getScreenMenuBarProperty())  {
+            UIManager.put("MenuBarUI", "com.apple.laf.AquaMenuBarUI");
+        } else {
+            UIManager.put("MenuBarUI", null);
+        }
+    }
 }
--- a/jdk/src/java.desktop/share/classes/javax/swing/JMenuBar.java	Wed Dec 07 17:54:55 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JMenuBar.java	Wed Dec 07 15:52:33 2016 -0800
@@ -27,6 +27,7 @@
 import java.awt.Component;
 import java.awt.Graphics;
 import java.awt.Insets;
+import java.awt.Toolkit;
 import java.awt.event.*;
 import java.beans.JavaBean;
 import java.beans.BeanProperty;
@@ -41,6 +42,8 @@
 import javax.swing.plaf.*;
 import javax.accessibility.*;
 
+import sun.awt.SunToolkit;
+
 /**
  * An implementation of a menu bar. You add <code>JMenu</code> objects to the
  * menu bar to construct a menu. When the user selects a <code>JMenu</code>
@@ -144,6 +147,10 @@
      * @see JComponent#updateUI
      */
     public void updateUI() {
+        Toolkit tk = Toolkit.getDefaultToolkit();
+        if (tk instanceof SunToolkit) {
+            ((SunToolkit)tk).updateScreenMenuBarUI();
+        }
         setUI((MenuBarUI)UIManager.getUI(this));
     }
 
--- a/jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java	Wed Dec 07 17:54:55 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/awt/SunToolkit.java	Wed Dec 07 15:52:33 2016 -0800
@@ -1857,6 +1857,9 @@
         return time == null ? -1 : time;
     }
 
+    public void updateScreenMenuBarUI() {
+    }
+
     // Cosntant alpha
     public boolean isWindowOpacitySupported() {
         return false;