Path: blob/master/src/java.desktop/macosx/classes/com/apple/laf/AquaPopupMenuUI.java
41154 views
/*1* Copyright (c) 2011, 2012, 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.plaf.ComponentUI;32import javax.swing.plaf.basic.BasicPopupMenuUI;3334public class AquaPopupMenuUI extends BasicPopupMenuUI {35public static ComponentUI createUI(final JComponent x) {36return new AquaPopupMenuUI();37}3839public boolean isPopupTrigger(final MouseEvent e) {40// Use the awt popup trigger code since this only runs on our OS!41return e.isPopupTrigger();42}4344@Override45public void paint(final Graphics g, final JComponent c) {46if (!(g instanceof Graphics2D)) {47super.paint(g, c);48return;49}5051if (!(PopupFactory.getSharedInstance() instanceof ScreenPopupFactory)) {52super.paint(g, c);53return;54}5556// round off and put back edges in a new Graphics57final Graphics2D g2d = (Graphics2D)g.create();58final Rectangle popupBounds = popupMenu.getBounds(); // NB: origin is still at 0,059paintRoundRect(g2d, popupBounds);60clipEdges(g2d, popupBounds);61g2d.dispose();6263// if any subsequent drawing occurs over these corners, the window is square again64super.paint(g, c);65}6667protected void paintRoundRect(final Graphics2D g2d, final Rectangle popupBounds) {68// setup the graphics context to blast alpha for every primitive we draw69g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);70g2d.setComposite(AlphaComposite.Clear);7172// draw the 3px round-rect line around the outer bounds of the window,73// this gives the appearance of rounded corners74g2d.setStroke(new BasicStroke(3.0f));75g2d.drawRoundRect(-2, -2, popupBounds.width + 3, popupBounds.height + 3, 12, 12);76}7778static final int OVERLAP_SLACK = 10;79protected void clipEdges(final Graphics2D g2d, final Rectangle popupBounds) {80final Component invoker = popupMenu.getInvoker();81if (!(invoker instanceof JMenu)) return; // only point corners originating from menu items8283final Rectangle invokerBounds = invoker.getBounds();8485// only get location on screen when necessary86invokerBounds.setLocation(invoker.getLocationOnScreen());87popupBounds.setLocation(popupMenu.getLocationOnScreen());8889final Point invokerCenter = new Point((int)invokerBounds.getCenterX(), (int)invokerBounds.getCenterY());90if (popupBounds.contains(invokerCenter)) {91// invoker is "behind" the popup, no corners should be pointed92return;93}9495// blast opaque background over the corners we want to "put back"96g2d.setComposite(AlphaComposite.SrcOver);97g2d.setColor(popupMenu.getBackground());9899final Point popupCenter = new Point((int)popupBounds.getCenterX(), (int)popupBounds.getCenterY());100final boolean invokerMidpointAbovePopupMidpoint = invokerCenter.y <= popupCenter.y;101102if (invokerBounds.x + invokerBounds.width < popupBounds.x + OVERLAP_SLACK) {103// popup is far right of invoker104if (invokerMidpointAbovePopupMidpoint) {105// point upper left corner, most common case106g2d.fillRect(-2, -2, 8, 8);107return;108}109// point lower left corner110g2d.fillRect(-2, popupBounds.height - 6, 8, 8);111return;112}113114if (popupBounds.x + popupBounds.width < invokerBounds.x + OVERLAP_SLACK) {115// popup is far left of invoker116if (invokerMidpointAbovePopupMidpoint) {117// point upper right corner118g2d.fillRect(popupBounds.width - 6, -2, 8, 8);119return;120}121// point lower right corner122g2d.fillRect(popupBounds.width - 6, popupBounds.height - 6, 8, 8);123return;124}125126// popup is neither "far right" or "far left" of it's invoker127if (invokerBounds.y + invokerBounds.height < popupBounds.y + OVERLAP_SLACK) {128// popup is "middle" below it's invoker,129// this is probably the "connected" case where both upper corners should touch130g2d.fillRect(-2, -2, popupBounds.width + 4, 8);131return;132}133134// if none of these cases match...don't make any corners pointed135}136}137138139