8302173: Button border overlaps with button icon on macOS system LaF

Reviewed-by: honkar, psadhukhan
This commit is contained in:
Damon Nguyen 2023-02-23 20:41:25 +00:00 committed by Harshitha Onkar
parent 796cdd52f5
commit 1a07871454
2 changed files with 174 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023, 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
@ -25,24 +25,57 @@
package com.apple.laf;
import java.awt.*;
import java.awt.event.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.FocusEvent;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseMotionListener;
import java.beans.PropertyChangeEvent;
import javax.swing.*;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.RootPaneUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicButtonListener;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicGraphicsUtils;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;
import sun.swing.SwingUtilities2;
import apple.laf.JRSUIConstants.Size;
import com.apple.laf.AquaButtonExtendedTypes.TypeSpecifier;
import com.apple.laf.AquaUtilControlSize.Sizeable;
import com.apple.laf.AquaUtils.*;
import com.apple.laf.AquaUtils.RecyclableSingleton;
import com.apple.laf.AquaUtils.RecyclableSingletonFromDefaultConstructor;
import sun.swing.SwingUtilities2;
public class AquaButtonUI extends BasicButtonUI implements Sizeable {
private static final String BUTTON_TYPE = "JButton.buttonType";
@ -306,9 +339,11 @@ public class AquaButtonUI extends BasicButtonUI implements Sizeable {
// performs icon and text rect calculations
final String text;
final Icon icon = b.getIcon();
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
// use zero insets for view since layout only handles text calculations
if (v != null && icon == null) {
// use zero insets for HTML without an icon
// since layout only handles text calculations
text = layoutAndGetText(g, b, aquaBorder, new Insets(0,0,0,0),
viewRect, iconRect, textRect);
} else {
@ -316,7 +351,7 @@ public class AquaButtonUI extends BasicButtonUI implements Sizeable {
}
// Paint the Icon
if (b.getIcon() != null) {
if (icon != null) {
paintIcon(g, b, iconRect);
}

View File

@ -0,0 +1,125 @@
/*
* Copyright (c) 2023, 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.
*
* 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
* @bug 8302173
* @requires (os.family == "mac")
* @summary Tests Aqua button icon positioning with HTML text
* @run main HtmlButtonWithTextAndIcon
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
public class HtmlButtonWithTextAndIcon {
private static Path testDir;
private static JButton button;
private static BufferedImage image;
private static final int OFFSET = 1;
private static final int BUTTON_WIDTH = 59;
private static final int BUTTON_HEIGHT = 28;
private static final int ICON_WIDTH = 16;
private static final int ICON_HEIGHT = 16;
private static final int centerX = 16;
private static final int centerY = 13;
private static final int minX = centerX - (ICON_WIDTH / 2) + OFFSET;
private static final int minY = centerY - (ICON_HEIGHT / 2) + OFFSET;
private static final int maxX = centerX + (ICON_WIDTH / 2) - OFFSET;
private static final int maxY = centerY + (ICON_HEIGHT / 2) - OFFSET;
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
testDir = Path.of(System.getProperty("test.classes", "."));
SwingUtilities.invokeAndWait(HtmlButtonWithTextAndIcon::createButton);
SwingUtilities.invokeAndWait(HtmlButtonWithTextAndIcon::paintButton);
testIconPosition(image.getRGB(centerX, centerY),
image.getRGB(minX, minY),
image.getRGB(minX, maxY),
image.getRGB(maxX, minY),
image.getRGB(maxX, maxY));
System.out.println("PASSED");
}
private static void createButton() {
button = new JButton("<html><nobr><u>Test</u>" +
"</nobr></html>", new TestIcon());
button.setSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
button.setHorizontalAlignment(JButton.LEFT);
}
private static void paintButton() {
image = new BufferedImage(BUTTON_WIDTH, BUTTON_HEIGHT, TYPE_INT_ARGB);
Graphics2D graphics2D = image.createGraphics();
button.paint(graphics2D);
graphics2D.dispose();
}
private static void testIconPosition(int... colors) throws IOException {
for (int c : colors) {
if (c != Color.RED.getRGB()) {
ImageIO.write(image, "png", new File(testDir + "/failImage.png"));
throw new RuntimeException("Icon position incorrect");
}
}
}
public static class TestIcon implements Icon {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.RED);
g.fillRect(x, y, ICON_WIDTH, ICON_HEIGHT);
}
@Override
public int getIconWidth() {
return ICON_WIDTH;
}
@Override
public int getIconHeight() {
return ICON_HEIGHT;
}
}
}