Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuBarUI.java
41154 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.apple.laf;2627import java.awt.Component;28import java.awt.Dimension;29import java.awt.Graphics;30import java.awt.MenuBar;31import java.security.AccessController;3233import javax.swing.JComponent;34import javax.swing.JFrame;35import javax.swing.JMenuBar;36import javax.swing.plaf.ComponentUI;37import javax.swing.plaf.basic.BasicMenuBarUI;3839import sun.lwawt.macosx.LWCToolkit;40import sun.security.action.GetBooleanAction;4142// MenuBar implementation for Mac L&F43@SuppressWarnings("removal")44public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider {4546static {47java.security.AccessController.doPrivileged(48(java.security.PrivilegedAction<Void>) () -> {49System.loadLibrary("osxui");50return null;51});52}5354// Utilities55public void uninstallUI(final JComponent c) {56if (fScreenMenuBar != null) {57final JFrame frame = (JFrame)(c.getTopLevelAncestor());58if (frame != null && frame.getMenuBar() == fScreenMenuBar) {59frame.setMenuBar((MenuBar)null);60}61fScreenMenuBar = null;62}63super.uninstallUI(c);64}6566// Create PLAF67public static ComponentUI createUI(final JComponent c) {68return new AquaMenuBarUI();69}7071// [3320390] -- If the screen menu bar is in use, don't register keyboard actions that72// show the menus when F10 is pressed.73protected void installKeyboardActions() {74if (!useScreenMenuBar) {75super.installKeyboardActions();76}77}7879protected void uninstallKeyboardActions() {80if (!useScreenMenuBar) {81super.uninstallKeyboardActions();82}83}8485// Paint Methods86public void paint(final Graphics g, final JComponent c) {87AquaMenuPainter.instance().paintMenuBarBackground(g, c.getWidth(), c.getHeight(), c);88}8990public Dimension getPreferredSize(final JComponent c) {91if (isScreenMenuBar((JMenuBar)c)) {92if (setScreenMenuBar((JFrame)(c.getTopLevelAncestor()))) {93return new Dimension(0, 0);94}95}96return null;97}9899void clearScreenMenuBar(final JFrame frame) {100if (useScreenMenuBar) {101frame.setMenuBar(null);102}103}104105boolean setScreenMenuBar(final JFrame frame) {106if (useScreenMenuBar) {107try {108getScreenMenuBar();109} catch(final Throwable t) {110return false;111}112113frame.setMenuBar(fScreenMenuBar);114}115116return true;117}118119public ScreenMenuBar getScreenMenuBar() {120// Lazy init of member variables means we should use a synchronized block.121synchronized(this) {122if (fScreenMenuBar == null) fScreenMenuBar = new ScreenMenuBar(this.menuBar);123}124return fScreenMenuBar;125}126127// JMenuBars are in frame unless we're using ScreenMenuBars *and* it's128// been set by JFrame.setJMenuBar129// unless the JFrame has a normal java.awt.MenuBar (it's possible!)130// Other JMenuBars appear where the programmer puts them - top of window or elsewhere131public static final boolean isScreenMenuBar(final JMenuBar c) {132final javax.swing.plaf.ComponentUI ui = c.getUI();133if (ui instanceof AquaMenuBarUI) {134if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false;135136final Component parent = c.getTopLevelAncestor();137if (parent instanceof JFrame) {138final MenuBar mb = ((JFrame)parent).getMenuBar();139final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c);140if (mb == null) return thisIsTheJMenuBar;141return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar);142}143}144return false;145}146147ScreenMenuBar fScreenMenuBar;148boolean useScreenMenuBar = getScreenMenuBarProperty();149150public static boolean getScreenMenuBarProperty() {151// Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit152if (LWCToolkit.isEmbedded()) return false;153return AccessController.doPrivileged(new GetBooleanAction(154AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"));155}156}157158159