Button: support non-square icon-only buttons (issue #110)

This commit is contained in:
Karl Tauber
2020-06-03 15:55:14 +02:00
parent 6a8bf2acc5
commit 049dae6584
3 changed files with 15 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ FlatLaf Change Log
- ComboBox: Minimum width is now 72 pixels (was ~50 for non-editable and ~130 - ComboBox: Minimum width is now 72 pixels (was ~50 for non-editable and ~130
for editable comboboxes). for editable comboboxes).
- ComboBox: Support custom borders in combobox editors. (issue #102) - ComboBox: Support custom borders in combobox editors. (issue #102)
- Button: Support non-square icon-only buttons. (issue #110)
- Ubuntu Linux: Fixed poorly rendered font. (issue #105) - Ubuntu Linux: Fixed poorly rendered font. (issue #105)
- macOS Catalina: Use Helvetica Neue font. - macOS Catalina: Use Helvetica Neue font.

View File

@@ -114,8 +114,8 @@ public class FlatButtonBorder
} else { } else {
insets = super.getBorderInsets( c, insets ); insets = super.getBorderInsets( c, insets );
// use smaller left and right insets for icon-only buttons (so that they are square) // use smaller left and right insets for icon-only or single-character buttons (so that they are square)
if( FlatButtonUI.isIconOnlyButton( c ) && ((AbstractButton)c).getMargin() instanceof UIResource ) if( FlatButtonUI.isIconOnlyOrSingleCharacterButton( c ) && ((AbstractButton)c).getMargin() instanceof UIResource )
insets.left = insets.right = Math.min( insets.top, insets.bottom ); insets.left = insets.right = Math.min( insets.top, insets.bottom );
} }

View File

@@ -224,7 +224,11 @@ public class FlatButtonUI
return c instanceof JButton && ((JButton)c).isDefaultButton(); return c instanceof JButton && ((JButton)c).isDefaultButton();
} }
static boolean isIconOnlyButton( Component c ) { /**
* Returns true if the button has an icon but no text,
* or it it does not have an icon and the text is either "..." or one character.
*/
static boolean isIconOnlyOrSingleCharacterButton( Component c ) {
if( !(c instanceof JButton) && !(c instanceof JToggleButton) ) if( !(c instanceof JButton) && !(c instanceof JToggleButton) )
return false; return false;
@@ -409,11 +413,13 @@ public class FlatButtonUI
if( prefSize == null ) if( prefSize == null )
return null; return null;
// make button square if it is a icon-only button // make button square if it is a single-character button
// or apply minimum width, if not in toolbar and not a icon-only button // or apply minimum width, if not in toolbar and not a icon-only or single-character button
if( isIconOnlyButton( c ) ) if( isIconOnlyOrSingleCharacterButton( c ) ) {
prefSize.width = Math.max( prefSize.width, prefSize.height ); // make only single-character buttons square to allow non-square icon-only buttons
else if( !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) { if( ((AbstractButton)c).getIcon() == null )
prefSize.width = Math.max( prefSize.width, prefSize.height );
} else if( !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) {
float focusWidth = FlatUIUtils.getBorderFocusWidth( c ); float focusWidth = FlatUIUtils.getBorderFocusWidth( c );
prefSize.width = Math.max( prefSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) ) + Math.round( focusWidth * 2 ) ); prefSize.width = Math.max( prefSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) ) + Math.round( focusWidth * 2 ) );
prefSize.height = Math.max( prefSize.height, scale( FlatUIUtils.minimumHeight( c, 0 ) ) + Math.round( focusWidth * 2 ) ); prefSize.height = Math.max( prefSize.height, scale( FlatUIUtils.minimumHeight( c, 0 ) ) + Math.round( focusWidth * 2 ) );