8282772: JButton text set as HTML content has unwanted padding

Reviewed-by: prr, psadhukhan
This commit is contained in:
Damon Nguyen 2022-05-11 23:29:06 +00:00 committed by Phil Race
parent 1586bf862b
commit ccbe8faa00
3 changed files with 110 additions and 35 deletions
src/java.desktop/share/classes/javax/swing/plaf
test/jdk/javax/swing/JButton/HtmlButtonImageTest

@ -25,22 +25,40 @@
package javax.swing.plaf.basic;
import sun.swing.SwingUtilities2;
import sun.awt.AppContext;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import sun.swing.SwingUtilities2;
import java.awt.AWTKeyStroke;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseMotionListener;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.text.View;
/**
@ -580,7 +598,15 @@ public class BasicButtonUI extends ButtonUI{
private String layout(AbstractButton b, FontMetrics fm,
int width, int height) {
Insets i = b.getInsets();
Insets i;
final View v = (View)b.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
i = new Insets(0, 0, 0, 0);
} else {
i = b.getInsets();
}
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = width - (i.right + viewRect.x);

@ -24,13 +24,25 @@
*/
package javax.swing.plaf.synth;
import sun.swing.SwingUtilities2;
import sun.swing.MenuItemLayoutHelper;
import sun.swing.SwingUtilities2;
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.Insets;
import java.awt.Rectangle;
import java.awt.*;
import javax.swing.*;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.*;
import javax.swing.text.View;
/**
* Wrapper for primitive graphics calls.
@ -380,10 +392,19 @@ public class SynthGraphicsUtils {
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
Insets insets = SynthLookAndFeel.getPaintingInsets(ss, paintInsets);
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = c.getWidth() - (insets.left + insets.right);
paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (c instanceof JButton && v != null) {
paintViewR.x = 0;
paintViewR.y = 0;
paintViewR.width = c.getWidth();
paintViewR.height = c.getHeight();
} else {
paintViewR.x = insets.left;
paintViewR.y = insets.top;
paintViewR.width = c.getWidth() - (insets.left + insets.right);
paintViewR.height = c.getHeight() - (insets.top + insets.bottom);
}
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
@ -409,8 +430,6 @@ public class SynthGraphicsUtils {
}
if (text != null) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, paintTextR);
} else {

@ -24,8 +24,7 @@
/*
* @test
* @bug 8015854
* @requires (os.family == "mac")
* @summary Tests HTML image as JButton text for unwanted padding on macOS Aqua LAF
* @summary Tests HTML image as JButton text for unwanted padding
* @run main HtmlButtonImageTest
*/
@ -41,6 +40,7 @@ import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
@ -60,19 +60,38 @@ public final class HtmlButtonImageTest {
private static final int maxX = centerX + (SQUARE_WIDTH / 2);
private static final int maxY = centerY + (SQUARE_HEIGHT / 2);
private static boolean supportedLaf;
private static int failCount = 0;
private static String currentLaf = new String();
private static StringBuffer failedLafs = new StringBuffer();
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
testDir = Path.of(System.getProperty("test.classes", "."));
generateRedSquare();
SwingUtilities.invokeAndWait(HtmlButtonImageTest::createButton);
SwingUtilities.invokeAndWait(HtmlButtonImageTest::paintButton);
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
if(supportedLaf) {
currentLaf = laf.getName();
SwingUtilities.invokeAndWait(HtmlButtonImageTest::createButton);
SwingUtilities.invokeAndWait(HtmlButtonImageTest::paintButton);
testImageCentering(image.getRGB(centerX, centerY),
image.getRGB(minX, minY),
image.getRGB(minX, maxY),
image.getRGB(maxX, minY),
image.getRGB(maxX, maxY));
testImageCentering(image.getRGB(centerX, centerY),
image.getRGB(minX, minY),
image.getRGB(minX, maxY),
image.getRGB(maxX, minY),
image.getRGB(maxX, maxY));
}
}
if(!failedLafs.isEmpty()) {
if(failCount > 1) {
failedLafs.setLength(failedLafs.length() - 2);
}
throw new RuntimeException("HTML image not centered in button " +
"for these L&F's: " + failedLafs);
}
}
private static void generateRedSquare() throws IOException {
@ -105,11 +124,22 @@ public final class HtmlButtonImageTest {
private static void testImageCentering(int... colors) throws IOException {
for (int c : colors) {
if (!checkRedColor(c)) {
ImageIO.write(image, "png",
new File(testDir + "/fail_image.png"));
throw new RuntimeException("HTML image not centered in button");
failCount++;
ImageIO.write(image, "png", new File(testDir + "/fail_image_" +
currentLaf.replaceAll("[^\\w\\s]","") + ".png"));
failedLafs.append(currentLaf + ", ");
break;
}
}
System.out.println("Passed");
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
supportedLaf = true;
} catch (UnsupportedLookAndFeelException | ClassNotFoundException |
InstantiationException | IllegalAccessException e) {
supportedLaf = false;
}
}
}