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)
- ToolTip: Do not show empty tooltip component if tooltip text is an empty
string. (issue #134)
- Button: Support specifying button border width.
## 0.38

View File

@@ -43,24 +43,24 @@ import com.formdev.flatlaf.util.DerivedColor;
* 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,
* 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
* {@link FlatUIUtils#paintParentBackground} to paint the empty space correctly.
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.innerFocusWidth int or float
* @uiDefault Component.focusColor Color
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault Component.focusedBorderColor Color
* @uiDefault Component.focusWidth int
* @uiDefault Component.innerFocusWidth int or float
* @uiDefault Component.focusColor Color
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault Component.focusedBorderColor Color
*
* @uiDefault Component.error.borderColor Color
* @uiDefault Component.error.focusedBorderColor Color
* @uiDefault Component.warning.borderColor Color
* @uiDefault Component.warning.focusedBorderColor Color
* @uiDefault Component.custom.borderColor Color
* @uiDefault Component.error.borderColor Color
* @uiDefault Component.error.focusedBorderColor Color
* @uiDefault Component.warning.borderColor Color
* @uiDefault Component.warning.focusedBorderColor Color
* @uiDefault Component.custom.borderColor Color
*
* @author Karl Tauber
*/
@@ -93,16 +93,18 @@ public class FlatBorder
float arc = isCellEditor ? 0 : scale( (float) getArc( c ) );
Color outlineColor = getOutlineColor( c );
// paint outer border
if( outlineColor != null || isFocused( c ) ) {
float innerFocusWidth = !(c instanceof JScrollPane)
? (outlineColor != null ? innerOutlineWidth : this.innerFocusWidth)
float innerWidth = !(c instanceof JScrollPane)
? (outlineColor != null ? innerOutlineWidth : innerFocusWidth)
: 0;
g2.setColor( (outlineColor != null) ? outlineColor : getFocusColor( c ) );
FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height, focusWidth,
scale( (float) getLineWidth( c ) ) + scale( innerFocusWidth ), arc );
FlatUIUtils.paintComponentOuterBorder( g2, x, y, width, height,
focusWidth, borderWidth + scale( innerWidth ), arc );
}
// paint border
g2.setPaint( (outlineColor != null) ? outlineColor : getBorderColor( c ) );
FlatUIUtils.paintComponentBorder( g2, x, y, width, height, focusWidth, borderWidth, arc );
} 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 ) {
if( !(c instanceof JComponent) )
return null;

View File

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

View File

@@ -210,7 +210,7 @@ public class FlatUIUtils
* Paints an outer border, which is usually a focus border.
* <p>
* 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}).
*
* @see #paintComponentBorder
@@ -219,6 +219,9 @@ public class FlatUIUtils
public static void paintComponentOuterBorder( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float lineWidth, float arc )
{
if( focusWidth + lineWidth == 0 )
return; // nothing to paint
double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
// 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>
* The outside bounds of the painted border are
* {@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}).
*
* @see #paintComponentOuterBorder
@@ -263,6 +267,9 @@ public class FlatUIUtils
public static void paintComponentBorder( Graphics2D g, int x, int y, int width, int height,
float focusWidth, float lineWidth, float arc )
{
if( lineWidth == 0 )
return; // nothing to paint
double systemScaleFactor = UIScale.getSystemScaleFactor( g );
if( systemScaleFactor != 1 && systemScaleFactor != 2 ) {
// 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
[win]Button.defaultButtonFollowsFocus=true
Button.borderWidth=1
Button.default.borderWidth=1
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.border [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatButtonBorder [UI]
Button.borderColor #5e6060 javax.swing.plaf.ColorUIResource [UI]
Button.borderWidth 1
Button.darkShadow #7e7e7e javax.swing.plaf.ColorUIResource [UI]
Button.default.background #365880 javax.swing.plaf.ColorUIResource [UI]
Button.default.boldText true

View File

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