Skip to content

Commit

Permalink
8282772: JButton text set as HTML content has unwanted padding
Browse files Browse the repository at this point in the history
Reviewed-by: prr, psadhukhan
  • Loading branch information
DamonGuy authored and prrace committed May 11, 2022
1 parent 1586bf8 commit ccbe8fa
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -580,7 +598,15 @@ public Component.BaselineResizeBehavior getBaselineResizeBehavior(

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@
*/
package javax.swing.plaf.synth;

import sun.swing.SwingUtilities2;
import sun.swing.MenuItemLayoutHelper;

import java.awt.*;
import javax.swing.*;
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 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.
Expand Down Expand Up @@ -380,10 +392,19 @@ public void paintText(SynthContext ss, Graphics g, String text,
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;
Expand All @@ -409,8 +430,6 @@ public void paintText(SynthContext ss, Graphics g, String text,
}

if (text != null) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);

if (v != null) {
v.paint(g, paintTextR);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -41,6 +40,7 @@
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;

Expand All @@ -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 {
Expand Down Expand Up @@ -105,11 +124,22 @@ private static boolean checkRedColor(int rgb) {
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;
}
}
}

1 comment on commit ccbe8fa

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.