Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaMenuUI.java
41154 views
/*1* Copyright (c) 2011, 2018, 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.*;28import java.awt.event.MouseEvent;2930import javax.swing.*;31import javax.swing.event.*;32import javax.swing.plaf.ComponentUI;33import javax.swing.plaf.basic.BasicMenuUI;3435public class AquaMenuUI extends BasicMenuUI implements AquaMenuPainter.Client {36public static ComponentUI createUI(final JComponent x) {37return new AquaMenuUI();38}3940protected ChangeListener createChangeListener(final JComponent c) {41return new ChangeHandler((JMenu)c, this);42}4344protected void installDefaults() {45super.installDefaults();4647// [3361625]48// In Aqua, the menu delay is 8 ticks, according to Eric Schlegel.49// That makes the millisecond delay 8 ticks * 1 second / 60 ticks * 1000 milliseconds/second50((JMenu)menuItem).setDelay(8 * 1000 / 60);51}5253protected void paintMenuItem(final Graphics g, final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final Color background, final Color foreground, final int localDefaultTextIconGap) {54AquaMenuPainter.instance().paintMenuItem(this, g, c, localCheckIcon, localArrowIcon, background, foreground, disabledForeground, selectionForeground, localDefaultTextIconGap, acceleratorFont);55}5657protected Dimension getPreferredMenuItemSize(final JComponent c, final Icon localCheckIcon, final Icon localArrowIcon, final int localDefaultTextIconGap) {58final Dimension d = AquaMenuPainter.instance().getPreferredMenuItemSize(c, localCheckIcon, localArrowIcon, localDefaultTextIconGap, acceleratorFont);59if (c.getParent() instanceof JMenuBar) d.height = Math.max(d.height, 21);60return d;61}6263public void paintBackground(final Graphics g, final JComponent c, final int menuWidth, final int menuHeight) {64final Container parent = c.getParent();65final boolean parentIsMenuBar = parent instanceof JMenuBar;6667final ButtonModel model = ((JMenuItem)c).getModel();68if (model.isArmed() || model.isSelected()) {69if (parentIsMenuBar) {70AquaMenuPainter.instance().paintSelectedMenuTitleBackground(g, menuWidth, menuHeight);71} else {72AquaMenuPainter.instance().paintSelectedMenuItemBackground(g, menuWidth, menuHeight);73}74} else {75if (parentIsMenuBar) {76AquaMenuPainter.instance().paintMenuBarBackground(g, menuWidth, menuHeight, c);77} else {78g.setColor(c.getBackground());79g.fillRect(0, 0, menuWidth, menuHeight);80}81}82}8384protected MouseInputListener createMouseInputListener(final JComponent c) {85return new AquaMouseInputHandler();86}8788protected MenuDragMouseListener createMenuDragMouseListener(final JComponent c) {89//return super.createMenuDragMouseListener(c);90return new MenuDragMouseHandler();91}9293class MenuDragMouseHandler implements MenuDragMouseListener {94public void menuDragMouseDragged(final MenuDragMouseEvent e) {95if (menuItem.isEnabled() == false) return;9697final MenuSelectionManager manager = e.getMenuSelectionManager();98final MenuElement[] path = e.getPath();99100// In Aqua, we always respect the menu's delay, if one is set.101// Doesn't matter how the menu is clicked on or otherwise moused over.102final Point p = e.getPoint();103if (p.x >= 0 && p.x < menuItem.getWidth() && p.y >= 0 && p.y < menuItem.getHeight()) {104final JMenu menu = (JMenu)menuItem;105final MenuElement[] selectedPath = manager.getSelectedPath();106if (!(selectedPath.length > 0 && selectedPath[selectedPath.length - 1] == menu.getPopupMenu())) {107if (menu.getDelay() == 0) {108appendPath(path, menu.getPopupMenu());109} else {110manager.setSelectedPath(path);111setupPostTimer(menu);112}113}114} else if (e.getID() == MouseEvent.MOUSE_RELEASED) {115final Component comp = manager.componentForPoint(e.getComponent(), e.getPoint());116if (comp == null) manager.clearSelectedPath();117}118}119120public void menuDragMouseEntered(final MenuDragMouseEvent e) { }121public void menuDragMouseExited(final MenuDragMouseEvent e) { }122public void menuDragMouseReleased(final MenuDragMouseEvent e) { }123}124125static void appendPath(final MenuElement[] path, final MenuElement elem) {126final MenuElement[] newPath = new MenuElement[path.length + 1];127System.arraycopy(path, 0, newPath, 0, path.length);128newPath[path.length] = elem;129MenuSelectionManager.defaultManager().setSelectedPath(newPath);130}131132protected class AquaMouseInputHandler extends MouseInputHandler {133/**134* Invoked when the cursor enters the menu. This method sets the selected135* path for the MenuSelectionManager and handles the case136* in which a menu item is used to pop up an additional menu, as in a137* hierarchical menu system.138*139* @param e the mouse event; not used140*/141public void mouseEntered(final MouseEvent e) {142final JMenu menu = (JMenu)menuItem;143if (!menu.isEnabled()) return;144145final MenuSelectionManager manager = MenuSelectionManager.defaultManager();146final MenuElement[] selectedPath = manager.getSelectedPath();147148// In Aqua, we always have a menu delay, regardless of where the menu is.149if (!(selectedPath.length > 0 && selectedPath[selectedPath.length - 1] == menu.getPopupMenu())) {150// the condition below prevents from activating menu in other frame151if (!menu.isTopLevelMenu() || (selectedPath.length > 0 &&152selectedPath[0] == menu.getParent())) {153if (menu.getDelay() == 0) {154appendPath(getPath(), menu.getPopupMenu());155} else {156manager.setSelectedPath(getPath());157setupPostTimer(menu);158}159}160}161}162}163}164165166