mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-12 23:07:15 -06:00
Button: make icon-only buttons square (no minimum width, smaller left/right insets)
This commit is contained in:
@@ -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 );
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user