Button: support specifying button border width

This commit is contained in:
Karl Tauber
2020-07-28 23:51:02 +02:00
parent 7b35325f9a
commit e29436da04
8 changed files with 39 additions and 19 deletions

View File

@@ -14,6 +14,7 @@ FlatLaf Change Log
pixels. On Windows, it is now 10 pixels. (issue #131) pixels. On Windows, it is now 10 pixels. (issue #131)
- ToolTip: Do not show empty tooltip component if tooltip text is an empty - ToolTip: Do not show empty tooltip component if tooltip text is an empty
string. (issue #134) string. (issue #134)
- Button: Support specifying button border width.
## 0.38 ## 0.38

View File

@@ -43,24 +43,24 @@ import com.formdev.flatlaf.util.DerivedColor;
* Border for various components (e.g. {@link javax.swing.JTextField}). * Border for various components (e.g. {@link javax.swing.JTextField}).
* *
* There is empty space around the component border, if Component.focusWidth is greater than zero, * There is empty space around the component border, if Component.focusWidth is greater than zero,
* which is used to paint focus border. * which is used to paint outer focus border.
* *
* Because there is empty space (if focus border is not painted), * Because there is empty space (if outer focus border is not painted),
* UI delegates that use this border (or subclasses) must invoke * UI delegates that use this border (or subclasses) must invoke
* {@link FlatUIUtils#paintParentBackground} to paint the empty space correctly. * {@link FlatUIUtils#paintParentBackground} to paint the empty space correctly.
* *
* @uiDefault Component.focusWidth int * @uiDefault Component.focusWidth int
* @uiDefault Component.innerFocusWidth int or float * @uiDefault Component.innerFocusWidth int or float
* @uiDefault Component.focusColor Color * @uiDefault Component.focusColor Color
* @uiDefault Component.borderColor Color * @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color * @uiDefault Component.disabledBorderColor Color
* @uiDefault Component.focusedBorderColor Color * @uiDefault Component.focusedBorderColor Color
* *
* @uiDefault Component.error.borderColor Color * @uiDefault Component.error.borderColor Color
* @uiDefault Component.error.focusedBorderColor Color * @uiDefault Component.error.focusedBorderColor Color
* @uiDefault Component.warning.borderColor Color * @uiDefault Component.warning.borderColor Color
* @uiDefault Component.warning.focusedBorderColor Color * @uiDefault Component.warning.focusedBorderColor Color
* @uiDefault Component.custom.borderColor Color * @uiDefault Component.custom.borderColor Color
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -93,16 +93,18 @@ public class FlatBorder
float arc = isCellEditor ? 0 : scale( (float) getArc( c ) ); float arc = isCellEditor ? 0 : scale( (float) getArc( c ) );
Color outlineColor = getOutlineColor( c ); Color outlineColor = getOutlineColor( c );
// paint outer border
if( outlineColor != null || isFocused( c ) ) { if( outlineColor != null || isFocused( c ) ) {
float innerFocusWidth = !(c instanceof JScrollPane) float innerWidth = !(c instanceof JScrollPane)
? (outlineColor != null ? innerOutlineWidth : this.innerFocusWidth) ? (outlineColor != null ? innerOutlineWidth : innerFocusWidth)
: 0; : 0;
g2.setColor( (outlineColor != null) ? outlineColor : getFocusColor( c ) ); g2.setColor( (outlineColor != null) ? outlineColor : getFocusColor( c ) );
FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height, focusWidth, FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height,
scale( (float) getLineWidth( c ) ) + scale( innerFocusWidth ), arc ); focusWidth, borderWidth + scale( innerWidth ), arc );
} }
// paint border
g2.setPaint( (outlineColor != null) ? outlineColor : getBorderColor( c ) ); g2.setPaint( (outlineColor != null) ? outlineColor : getBorderColor( c ) );
FlatUIUtils.paintComponentBorder( g2, x, y, width, height, focusWidth, borderWidth, arc ); FlatUIUtils.paintComponentBorder( g2, x, y, width, height, focusWidth, borderWidth, arc );
} finally { } finally {
@@ -110,6 +112,10 @@ public class FlatBorder
} }
} }
/**
* Returns the outline color of the component border specified in client property
* {@link FlatClientProperties#OUTLINE}.
*/
protected Color getOutlineColor( Component c ) { protected Color getOutlineColor( Component c ) {
if( !(c instanceof JComponent) ) if( !(c instanceof JComponent) )
return null; return null;

View File

@@ -42,6 +42,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Button.default.hoverBorderColor Color optional * @uiDefault Button.default.hoverBorderColor Color optional
* @uiDefault Button.default.focusedBorderColor Color * @uiDefault Button.default.focusedBorderColor Color
* @uiDefault Button.default.focusColor Color * @uiDefault Button.default.focusColor Color
* @uiDefault Button.borderWidth int
* @uiDefault Button.default.borderWidth int * @uiDefault Button.default.borderWidth int
* @uiDefault Button.toolbar.margin Insets * @uiDefault Button.toolbar.margin Insets
* @uiDefault Button.toolbar.spacingInsets Insets * @uiDefault Button.toolbar.spacingInsets Insets
@@ -62,6 +63,7 @@ public class FlatButtonBorder
protected final Color defaultHoverBorderColor = UIManager.getColor( "Button.default.hoverBorderColor" ); protected final Color defaultHoverBorderColor = UIManager.getColor( "Button.default.hoverBorderColor" );
protected final Color defaultFocusedBorderColor = UIManager.getColor( "Button.default.focusedBorderColor" ); protected final Color defaultFocusedBorderColor = UIManager.getColor( "Button.default.focusedBorderColor" );
protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" ); protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" );
protected final int borderWidth = UIManager.getInt( "Button.borderWidth" );
protected final int defaultBorderWidth = UIManager.getInt( "Button.default.borderWidth" ); protected final int defaultBorderWidth = UIManager.getInt( "Button.default.borderWidth" );
protected final Insets toolbarMargin = UIManager.getInsets( "Button.toolbar.margin" ); protected final Insets toolbarMargin = UIManager.getInsets( "Button.toolbar.margin" );
protected final Insets toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" ); protected final Insets toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" );
@@ -134,7 +136,7 @@ public class FlatButtonBorder
@Override @Override
protected int getBorderWidth( Component c ) { protected int getBorderWidth( Component c ) {
return FlatButtonUI.isDefaultButton( c ) ? defaultBorderWidth : super.getBorderWidth( c ); return FlatButtonUI.isDefaultButton( c ) ? defaultBorderWidth : borderWidth;
} }
@Override @Override

View File

@@ -210,7 +210,7 @@ public class FlatUIUtils
* Paints an outer border, which is usually a focus border. * Paints an outer border, which is usually a focus border.
* <p> * <p>
* The outside bounds of the painted border are {@code x,y,width,height}. * The outside bounds of the painted border are {@code x,y,width,height}.
* The line width of the painted border is {@code focusWidth + lineWidth}. * The line thickness of the painted border is {@code focusWidth + lineWidth}.
* The given arc diameter refers to the inner rectangle ({@code x,y,width,height} minus {@code focusWidth}). * The given arc diameter refers to the inner rectangle ({@code x,y,width,height} minus {@code focusWidth}).
* *
* @see #paintComponentBorder * @see #paintComponentBorder
@@ -219,6 +219,9 @@ public class FlatUIUtils
public static void paintComponentOuterBorder( Graphics2D g, int x, int y, int width, int height, public static void paintComponentOuterBorder( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float lineWidth, float arc ) float focusWidth, float lineWidth, float arc )
{ {
if( focusWidth + lineWidth == 0 )
return; // nothing to paint
double systemScaleFactor = UIScale.getSystemScaleFactor( g ); double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) { if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
// paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175% // paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175%
@@ -255,6 +258,7 @@ public class FlatUIUtils
* <p> * <p>
* The outside bounds of the painted border are * The outside bounds of the painted border are
* {@code x + focusWidth, y + focusWidth, width - (focusWidth * 2), height - (focusWidth * 2)}. * {@code x + focusWidth, y + focusWidth, width - (focusWidth * 2), height - (focusWidth * 2)}.
* The line thickness of the painted border is {@code lineWidth}.
* The given arc diameter refers to the painted rectangle (and not to {@code x,y,width,height}). * The given arc diameter refers to the painted rectangle (and not to {@code x,y,width,height}).
* *
* @see #paintComponentOuterBorder * @see #paintComponentOuterBorder
@@ -263,6 +267,9 @@ public class FlatUIUtils
public static void paintComponentBorder( Graphics2D g, int x, int y, int width, int height, public static void paintComponentBorder( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float lineWidth, float arc ) float focusWidth, float lineWidth, float arc )
{ {
if( lineWidth == 0 )
return; // nothing to paint
double systemScaleFactor = UIScale.getSystemScaleFactor( g ); double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) { if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
// paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175% // paint at scale 1x to avoid clipping on right and bottom edges at 125%, 150% or 175%

View File

@@ -135,6 +135,7 @@ Button.rollover=true
Button.defaultButtonFollowsFocus=false Button.defaultButtonFollowsFocus=false
[win]Button.defaultButtonFollowsFocus=true [win]Button.defaultButtonFollowsFocus=true
Button.borderWidth=1
Button.default.borderWidth=1 Button.default.borderWidth=1
Button.toolbar.margin=3,3,3,3 Button.toolbar.margin=3,3,3,3

View File

@@ -65,6 +65,7 @@ Button.arc 6
Button.background #4c5052 javax.swing.plaf.ColorUIResource [UI] Button.background #4c5052 javax.swing.plaf.ColorUIResource [UI]
Button.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] Button.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI]
Button.borderColor #5e6060 javax.swing.plaf.ColorUIResource [UI] Button.borderColor #5e6060 javax.swing.plaf.ColorUIResource [UI]
Button.borderWidth 1
Button.darkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI] Button.darkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI]
Button.default.background #365880 javax.swing.plaf.ColorUIResource [UI] Button.default.background #365880 javax.swing.plaf.ColorUIResource [UI]
Button.default.boldText true Button.default.boldText true

View File

@@ -65,6 +65,7 @@ Button.arc 6
Button.background #ffffff javax.swing.plaf.ColorUIResource [UI] Button.background #ffffff javax.swing.plaf.ColorUIResource [UI]
Button.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI] Button.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI]
Button.borderColor #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Button.borderColor #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
Button.borderWidth 1
Button.darkShadow #9e9e9e javax.swing.plaf.ColorUIResource [UI] Button.darkShadow #9e9e9e javax.swing.plaf.ColorUIResource [UI]
Button.default.background #ffffff javax.swing.plaf.ColorUIResource [UI] Button.default.background #ffffff javax.swing.plaf.ColorUIResource [UI]
Button.default.borderColor #4f9ee3 javax.swing.plaf.ColorUIResource [UI] Button.default.borderColor #4f9ee3 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -12,6 +12,7 @@ Button.arc
Button.background Button.background
Button.border Button.border
Button.borderColor Button.borderColor
Button.borderWidth
Button.darkShadow Button.darkShadow
Button.default.background Button.default.background
Button.default.boldText Button.default.boldText