Button and ToggleButton:

- support per component minimum height (issue #44)
- do not apply minimum width if button border was changed (is no longer an instance of `FlatButtonBorder`)
- ToggleButton: no longer use focus width for underline style toggle buttons to compute component size, which reduces/fixes component size in "Flat IntelliJ" and "Flat Darcula" themes
- revalidate/repaint client properties minimum width/height or buttonType change
This commit is contained in:
Karl Tauber
2020-01-14 18:42:06 +01:00
parent 655bf112ac
commit 74909da110
7 changed files with 96 additions and 12 deletions

View File

@@ -8,6 +8,13 @@ FlatLaf Change Log
- TabbedPane: In scroll-tab-layout, the cropped line is now hidden. (issue #40)
- Tree: UI default value `Tree.textBackground` now has a valid color and is no
longer `null`.
- Button and ToggleButton: Support per component minimum height (set client
property `JComponent.minimumHeight` to an integer). (issue #44)
- Button and ToggleButton: Do not apply minimum width if button border was
changed (is no longer an instance of `FlatButtonBorder`).
- ToggleButton: No longer use focus width for underline style toggle buttons to
compute component size, which reduces/fixes component size in "Flat IntelliJ"
and "Flat Darcula" themes.
## 0.24

View File

@@ -79,11 +79,19 @@ public interface FlatClientProperties
/**
* Specifies minimum width of a component.
* <p>
* <strong>Component</strong> {@link javax.swing.JButton} and {@link javax.swing.text.JTextComponent}<br>
* <strong>Component</strong> {@link javax.swing.JButton}, {@link javax.swing.JToggleButton} and {@link javax.swing.text.JTextComponent}<br>
* <strong>Value type</strong> {@link java.lang.Integer}<br>
*/
String MINIMUM_WIDTH = "JComponent.minimumWidth";
/**
* Specifies minimum height of a component.
* <p>
* <strong>Component</strong> {@link javax.swing.JButton} and {@link javax.swing.JToggleButton}<br>
* <strong>Value type</strong> {@link java.lang.Integer}<br>
*/
String MINIMUM_HEIGHT = "JComponent.minimumHeight";
/**
* Specifies whether the decrease/increase arrow buttons of a scrollbar are shown.
* <p>
@@ -131,4 +139,13 @@ public interface FlatClientProperties
Object value = c.getClientProperty( key );
return (value instanceof Boolean) ? (boolean) value : defaultValue;
}
/**
* Checks whether a client property of a component is an integer and returns its value.
* If the client property is not set, or not an integer, defaultValue is returned.
*/
static int clientPropertyInt( JComponent c, String key, int defaultValue ) {
Object value = c.getClientProperty( key );
return (value instanceof Integer) ? (int) value : defaultValue;
}
}

View File

@@ -73,7 +73,7 @@ public class FlatBorder
FlatUIUtils.setRenderingHints( g2 );
boolean isCellEditor = isTableCellEditor( c );
float focusWidth = isCellEditor ? 0 : getFocusWidth();
float focusWidth = isCellEditor ? 0 : getFocusWidth( c );
float borderWidth = getBorderWidth( c );
float arc = isCellEditor ? 0 : getArc( c );
@@ -82,7 +82,7 @@ public class FlatBorder
g2.setColor( getFocusColor( c ) );
FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height, focusWidth,
getLineWidth() + scale( innerFocusWidth ), arc );
getLineWidth( c ) + scale( innerFocusWidth ), arc );
}
g2.setPaint( getBorderColor( c ) );
@@ -153,7 +153,7 @@ public class FlatBorder
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
boolean isCellEditor = isTableCellEditor( c );
float ow = (isCellEditor ? 0 : getFocusWidth()) + getLineWidth();
float ow = (isCellEditor ? 0 : getFocusWidth( c )) + getLineWidth( c );
insets = super.getBorderInsets( c, insets );
insets.top = Math.round( scale( (float) insets.top ) + ow );
@@ -163,16 +163,16 @@ public class FlatBorder
return insets;
}
protected float getFocusWidth() {
protected float getFocusWidth( Component c ) {
return scale( (float) focusWidth );
}
protected float getLineWidth() {
protected float getLineWidth( Component c ) {
return scale( 1f );
}
protected float getBorderWidth( Component c ) {
return getLineWidth();
return getLineWidth( c );
}
protected float getArc( Component c ) {

View File

@@ -104,6 +104,11 @@ public class FlatButtonBorder
return insets;
}
@Override
protected float getFocusWidth( Component c ) {
return FlatToggleButtonUI.isUnderlineButton( c ) ? 0 : super.getFocusWidth(c );
}
@Override
protected float getBorderWidth( Component c ) {
return FlatButtonUI.isDefaultButton( c ) ? scale( (float) defaultBorderWidth ) : super.getBorderWidth( c );

View File

@@ -28,6 +28,7 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.RoundRectangle2D;
import java.beans.PropertyChangeEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
@@ -39,6 +40,7 @@ import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicButtonListener;
import javax.swing.plaf.basic.BasicButtonUI;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.UIScale;
@@ -180,7 +182,7 @@ public class FlatButtonUI
LookAndFeel.installProperty( b, "opaque", false );
LookAndFeel.installProperty( b, "iconTextGap", scale( iconTextGap ) );
MigLayoutVisualPadding.install( b, focusWidth );
MigLayoutVisualPadding.install( b, getFocusWidth( b ) );
}
@Override
@@ -191,6 +193,26 @@ public class FlatButtonUI
defaults_initialized = false;
}
@Override
protected BasicButtonListener createButtonListener( AbstractButton b ) {
return new BasicButtonListener( b ) {
@Override
public void propertyChange( PropertyChangeEvent e ) {
super.propertyChange( e );
FlatButtonUI.this.propertyChange( b, e );
}
};
}
protected void propertyChange( AbstractButton b, PropertyChangeEvent e ) {
switch( e.getPropertyName() ) {
case MINIMUM_WIDTH:
case MINIMUM_HEIGHT:
b.revalidate();
break;
}
}
static boolean isContentAreaFilled( Component c ) {
return !(c instanceof AbstractButton) || ((AbstractButton)c).isContentAreaFilled();
}
@@ -246,7 +268,7 @@ public class FlatButtonUI
FlatUIUtils.setRenderingHints( g2 );
Border border = c.getBorder();
float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0;
float focusWidth = (border instanceof FlatBorder) ? scale( (float) getFocusWidth( c ) ) : 0;
float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton( c ))
? scale( (float) this.arc ) : 0;
boolean def = isDefaultButton( c );
@@ -362,9 +384,16 @@ public class FlatButtonUI
// or apply minimum width, if not in toolbar and not a icon-only button
if( isIconOnlyButton( c ) )
prefSize.width = Math.max( prefSize.width, prefSize.height );
else if( !isToolBarButton( c ) )
else if( !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) {
int focusWidth = getFocusWidth( c );
prefSize.width = Math.max( prefSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) + (focusWidth * 2) ) );
prefSize.height = Math.max( prefSize.height, scale( FlatUIUtils.minimumHeight( c, 0 ) + (focusWidth * 2) ) );
}
return prefSize;
}
protected int getFocusWidth( JComponent c ) {
return focusWidth;
}
}

View File

@@ -20,6 +20,7 @@ import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
@@ -129,6 +130,23 @@ public class FlatToggleButtonUI
defaults_initialized = false;
}
@Override
protected void propertyChange( AbstractButton b, PropertyChangeEvent e ) {
super.propertyChange( b, e );
switch( e.getPropertyName() ) {
case BUTTON_TYPE:
if( BUTTON_TYPE_UNDERLINE.equals( e.getOldValue() ) || BUTTON_TYPE_UNDERLINE.equals( e.getNewValue() ) ) {
MigLayoutVisualPadding.uninstall( b );
MigLayoutVisualPadding.install( b, getFocusWidth( b ) );
b.revalidate();
}
b.repaint();
break;
}
}
static boolean isUnderlineButton( Component c ) {
return c instanceof JToggleButton && clientPropertyEquals( (JToggleButton) c, BUTTON_TYPE, BUTTON_TYPE_UNDERLINE );
}
@@ -186,4 +204,9 @@ public class FlatToggleButtonUI
return super.getForeground( c );
}
@Override
protected int getFocusWidth( JComponent c ) {
return isUnderlineButton( c ) ? 0 : super.getFocusWidth( c );
}
}

View File

@@ -113,8 +113,11 @@ public class FlatUIUtils
}
public static int minimumWidth( JComponent c, int minimumWidth ) {
Object p = c.getClientProperty( FlatClientProperties.MINIMUM_WIDTH );
return (p instanceof Integer) ? ((Integer)p).intValue() : minimumWidth;
return FlatClientProperties.clientPropertyInt( c, FlatClientProperties.MINIMUM_WIDTH, minimumWidth );
}
public static int minimumHeight( JComponent c, int minimumHeight ) {
return FlatClientProperties.clientPropertyInt( c, FlatClientProperties.MINIMUM_HEIGHT, minimumHeight );
}
public static boolean isTableCellEditor( Component c ) {