Button: make icon-only buttons square (no minimum width, smaller left/right insets)

This commit is contained in:
Karl Tauber
2019-10-04 18:13:41 +02:00
parent 26a2446a4d
commit 37c70f6c9e
7 changed files with 50 additions and 1 deletions

View File

@@ -20,8 +20,11 @@ import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.plaf.UIResource;
/**
* Border for {@link javax.swing.JButton}.
@@ -73,6 +76,17 @@ public class FlatButtonBorder
null );
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
insets = super.getBorderInsets( c, insets );
// use smaller left and right insets for icon-only buttons (so that they are square)
if( FlatButtonUI.isIconOnlyButton( c ) && ((JButton)c).getMargin() instanceof UIResource )
insets.left = insets.right = Math.min( insets.top, insets.bottom );
return insets;
}
@Override
protected float getArc() {
return scale( (float) arc );

View File

@@ -146,6 +146,13 @@ public class FlatButtonUI
return c instanceof JButton && ((JButton)c).isDefaultButton();
}
static boolean isIconOnlyButton( Component c ) {
String text;
return c instanceof JButton &&
((JButton)c).getIcon() != null &&
((text = ((JButton)c).getText()) == null || text.isEmpty());
}
static boolean isHelpButton( Component c ) {
return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP );
}
@@ -259,8 +266,11 @@ public class FlatButtonUI
return new Dimension( helpButtonIcon.getIconWidth(), helpButtonIcon.getIconHeight() );
Dimension prefSize = super.getPreferredSize( c );
if( !isToolBarButton( c ) )
// apply minimum width, if not in toolbar and not a icon-only button
if( !isToolBarButton( c ) && !isIconOnlyButton( c ) )
prefSize.width = Math.max( prefSize.width, scale( minimumWidth + (focusWidth * 2) ) );
return prefSize;
}
}