From d67cfc911be265d5d5a69c2e95d7d57ec02685f7 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 2 Nov 2021 15:03:18 +0100 Subject: [PATCH] CheckBox and RadioButton: - added `CheckBox.icon.selectedBorderWidth` - added `CheckBox.icon.disabledSelectedBorderWidth` - added `CheckBox.icon.disabledSelectedBorderColor` - added `CheckBox.icon.disabledSelectedBackground` - changed `CheckBox.icon.focusWidth` from `int` `float` --- .../flatlaf/icons/FlatCheckBoxIcon.java | 63 +++++++++++-------- .../flatlaf/icons/FlatRadioButtonIcon.java | 11 ++-- .../formdev/flatlaf/ui/FlatRadioButtonUI.java | 9 ++- .../flatlaf/ui/TestFlatStyleableInfo.java | 12 +++- .../formdev/flatlaf/ui/TestFlatStyling.java | 14 ++++- .../uidefaults/FlatIntelliJLaf_1.8.0.txt | 2 +- .../dumps/uidefaults/FlatTestLaf_1.8.0.txt | 3 + .../flatlaf/testing/FlatTestLaf.properties | 4 ++ .../flatlaf/themeeditor/FlatLafUIKeys.txt | 10 +++ 9 files changed, 89 insertions(+), 39 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java index ae73dda6..be6346c5 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatCheckBoxIcon.java @@ -44,8 +44,10 @@ import com.formdev.flatlaf.ui.FlatUIUtils; * @uiDefault Component.focusWidth int * @uiDefault Component.borderWidth int * @uiDefault Component.focusColor Color - * @uiDefault CheckBox.icon.focusWidth int optional; defaults to Component.focusWidth + * @uiDefault CheckBox.icon.focusWidth int or float optional; defaults to Component.focusWidth * @uiDefault CheckBox.icon.borderWidth int or float optional; defaults to Component.borderWidth + * @uiDefault CheckBox.icon.selectedBorderWidth int or float optional; defaults to CheckBox.icon.borderWidth + * @uiDefault CheckBox.icon.disabledSelectedBorderWidth int or float optional; defaults to CheckBox.icon.selectedBorderWidth * @uiDefault CheckBox.arc int * * @uiDefault CheckBox.icon.focusColor Color optional; defaults to Component.focusColor @@ -57,6 +59,8 @@ import com.formdev.flatlaf.ui.FlatUIUtils; * * @uiDefault CheckBox.icon.disabledBorderColor Color * @uiDefault CheckBox.icon.disabledBackground Color + * @uiDefault CheckBox.icon.disabledSelectedBorderColor Color optional; CheckBox.icon.disabledBorderColor is used if not specified + * @uiDefault CheckBox.icon.disabledSelectedBackground Color optional; CheckBox.icon.disabledBackground is used if not specified * @uiDefault CheckBox.icon.disabledCheckmarkColor Color * * @uiDefault CheckBox.icon.focusedBorderColor Color optional @@ -83,12 +87,11 @@ public class FlatCheckBoxIcon extends FlatAbstractIcon { protected final String style = UIManager.getString( "CheckBox.icon.style" ); - @Styleable public int focusWidth = getUIInt( "CheckBox.icon.focusWidth", - UIManager.getInt( "Component.focusWidth" ), style ); - @Styleable protected Color focusColor = FlatUIUtils.getUIColor( "CheckBox.icon.focusColor", - UIManager.getColor( "Component.focusColor" ) ); - /** @since 2 */ @Styleable protected float borderWidth = getUIFloat( "CheckBox.icon.borderWidth", - FlatUIUtils.getUIFloat( "Component.borderWidth", 1 ), style ); + @Styleable protected float focusWidth = getUIFloat( "CheckBox.icon.focusWidth", UIManager.getInt( "Component.focusWidth" ), style ); + @Styleable protected Color focusColor = FlatUIUtils.getUIColor( "CheckBox.icon.focusColor", UIManager.getColor( "Component.focusColor" ) ); + /** @since 2 */ @Styleable protected float borderWidth = getUIFloat( "CheckBox.icon.borderWidth", FlatUIUtils.getUIFloat( "Component.borderWidth", 1 ), style ); + /** @since 2 */ @Styleable protected float selectedBorderWidth = getUIFloat( "CheckBox.icon.selectedBorderWidth", Float.MIN_VALUE, style ); + /** @since 2 */ @Styleable protected float disabledSelectedBorderWidth = getUIFloat( "CheckBox.icon.disabledSelectedBorderWidth", Float.MIN_VALUE, style ); @Styleable protected int arc = FlatUIUtils.getUIInt( "CheckBox.arc", 2 ); // enabled @@ -101,6 +104,8 @@ public class FlatCheckBoxIcon // disabled @Styleable protected Color disabledBorderColor = getUIColor( "CheckBox.icon.disabledBorderColor", style ); @Styleable protected Color disabledBackground = getUIColor( "CheckBox.icon.disabledBackground", style ); + /** @since 2 */ @Styleable protected Color disabledSelectedBorderColor = getUIColor( "CheckBox.icon.disabledSelectedBorderColor", style ); + /** @since 2 */ @Styleable protected Color disabledSelectedBackground = getUIColor( "CheckBox.icon.disabledSelectedBackground", style ); @Styleable protected Color disabledCheckmarkColor = getUIColor( "CheckBox.icon.disabledCheckmarkColor", style ); // focused @@ -133,15 +138,6 @@ public class FlatCheckBoxIcon return UIManager.getColor( key ); } - protected static int getUIInt( String key, int defaultValue, String style ) { - if( style != null ) { - int value = FlatUIUtils.getUIInt( styleKey( key, style ), Integer.MIN_VALUE ); - if( value != Integer.MIN_VALUE ) - return value; - } - return FlatUIUtils.getUIInt( key, defaultValue ); - } - /** @since 2 */ protected static float getUIFloat( String key, float defaultValue, String style ) { if( style != null ) { @@ -177,6 +173,11 @@ public class FlatCheckBoxIcon boolean indeterminate = isIndeterminate( c ); boolean selected = indeterminate || isSelected( c ); boolean isFocused = FlatUIUtils.isPermanentFocusOwner( c ); + float bw = selected + ? (disabledSelectedBorderWidth != Float.MIN_VALUE && !c.isEnabled() + ? disabledSelectedBorderWidth + : (selectedBorderWidth != Float.MIN_VALUE ? selectedBorderWidth : borderWidth)) + : borderWidth; // paint focused border if( isFocused && focusWidth > 0 && FlatButtonUI.isFocusPainted( c ) ) { @@ -186,7 +187,7 @@ public class FlatCheckBoxIcon // paint border g.setColor( getBorderColor( c, selected ) ); - paintBorder( c, g ); + paintBorder( c, g, bw ); // paint background Color bg = FlatUIUtils.deriveColor( getBackground( c, selected ), @@ -194,10 +195,10 @@ public class FlatCheckBoxIcon if( bg.getAlpha() < 255 ) { // fill background with default color before filling with non-opaque background g.setColor( selected ? selectedBackground : background ); - paintBackground( c, g ); + paintBackground( c, g, bw ); } g.setColor( bg ); - paintBackground( c, g ); + paintBackground( c, g, bw ); // paint checkmark if( selected ) { @@ -211,20 +212,23 @@ public class FlatCheckBoxIcon protected void paintFocusBorder( Component c, Graphics2D g ) { // the outer focus border is painted outside of the icon - int wh = ICON_SIZE - 1 + (focusWidth * 2); - int arcwh = arc + (focusWidth * 2); - g.fillRoundRect( -focusWidth + 1, -focusWidth, wh, wh, arcwh, arcwh ); + float wh = ICON_SIZE - 1 + (focusWidth * 2); + float arcwh = arc + (focusWidth * 2); + g.fill( new RoundRectangle2D.Float( -focusWidth + 1, -focusWidth, wh, wh, arcwh, arcwh ) ); } - protected void paintBorder( Component c, Graphics2D g ) { + protected void paintBorder( Component c, Graphics2D g, float borderWidth ) { + if( borderWidth == 0 ) + return; + int arcwh = arc; g.fillRoundRect( 1, 0, 14, 14, arcwh, arcwh ); } - protected void paintBackground( Component c, Graphics2D g ) { + protected void paintBackground( Component c, Graphics2D g, float borderWidth ) { float xy = borderWidth; float wh = 14 - (borderWidth * 2); - int arcwh = arc - 1; + float arcwh = arc - borderWidth; g.fill( new RoundRectangle2D.Float( 1 + xy, xy, wh, wh, arcwh, arcwh ) ); } @@ -250,6 +254,11 @@ public class FlatCheckBoxIcon return c instanceof AbstractButton && ((AbstractButton)c).isSelected(); } + /** @since 2 */ + public float getFocusWidth() { + return focusWidth; + } + protected Color getFocusColor( Component c ) { return focusColor; } @@ -257,7 +266,7 @@ public class FlatCheckBoxIcon protected Color getBorderColor( Component c, boolean selected ) { return FlatButtonUI.buttonStateColor( c, selected ? selectedBorderColor : borderColor, - disabledBorderColor, + (selected && disabledSelectedBorderColor != null) ? disabledSelectedBorderColor : disabledBorderColor, (selected && focusedSelectedBorderColor != null) ? focusedSelectedBorderColor : focusedBorderColor, (selected && hoverSelectedBorderColor != null) ? hoverSelectedBorderColor : hoverBorderColor, (selected && pressedSelectedBorderColor != null) ? pressedSelectedBorderColor : pressedBorderColor ); @@ -266,7 +275,7 @@ public class FlatCheckBoxIcon protected Color getBackground( Component c, boolean selected ) { return FlatButtonUI.buttonStateColor( c, selected ? selectedBackground : background, - disabledBackground, + (selected && disabledSelectedBackground != null) ? disabledSelectedBackground : disabledBackground, (selected && focusedSelectedBackground != null) ? focusedSelectedBackground : focusedBackground, (selected && hoverSelectedBackground != null) ? hoverSelectedBackground : hoverBackground, (selected && pressedSelectedBackground != null) ? pressedSelectedBackground : pressedBackground ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatRadioButtonIcon.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatRadioButtonIcon.java index 14ad53df..c2636c84 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatRadioButtonIcon.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/icons/FlatRadioButtonIcon.java @@ -41,17 +41,20 @@ public class FlatRadioButtonIcon @Override protected void paintFocusBorder( Component c, Graphics2D g ) { // the outer focus border is painted outside of the icon - int wh = ICON_SIZE + (focusWidth * 2); - g.fillOval( -focusWidth, -focusWidth, wh, wh ); + float wh = ICON_SIZE + (focusWidth * 2); + g.fill( new Ellipse2D.Float( -focusWidth, -focusWidth, wh, wh ) ); } @Override - protected void paintBorder( Component c, Graphics2D g ) { + protected void paintBorder( Component c, Graphics2D g, float borderWidth ) { + if( borderWidth == 0 ) + return; + g.fillOval( 0, 0, 15, 15 ); } @Override - protected void paintBackground( Component c, Graphics2D g ) { + protected void paintBackground( Component c, Graphics2D g, float borderWidth ) { float xy = borderWidth; float wh = 15 - (borderWidth * 2); g.fill( new Ellipse2D.Float( xy, xy, wh, wh ) ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java index 409a2e97..60c4fa83 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRadioButtonUI.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.Objects; import javax.swing.AbstractButton; import javax.swing.CellRendererPane; +import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.LookAndFeel; import javax.swing.UIManager; @@ -279,8 +280,12 @@ public class FlatRadioButtonUI private int getIconFocusWidth( JComponent c ) { AbstractButton b = (AbstractButton) c; - return (b.getIcon() == null && getDefaultIcon() instanceof FlatCheckBoxIcon) - ? UIScale.scale( ((FlatCheckBoxIcon)getDefaultIcon()).focusWidth ) + Icon icon = b.getIcon(); + if( icon == null ) + icon = getDefaultIcon(); + + return (icon instanceof FlatCheckBoxIcon) + ? Math.round( UIScale.scale( ((FlatCheckBoxIcon)icon).getFocusWidth() ) ) : 0; } diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java index adf092d4..c85f6003 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java @@ -469,9 +469,11 @@ public class TestFlatStyleableInfo //---- icon ---- - "icon.focusWidth", int.class, + "icon.focusWidth", float.class, "icon.focusColor", Color.class, "icon.borderWidth", float.class, + "icon.selectedBorderWidth", float.class, + "icon.disabledSelectedBorderWidth", float.class, "icon.arc", int.class, // enabled @@ -484,6 +486,8 @@ public class TestFlatStyleableInfo // disabled "icon.disabledBorderColor", Color.class, "icon.disabledBackground", Color.class, + "icon.disabledSelectedBorderColor", Color.class, + "icon.disabledSelectedBackground", Color.class, "icon.disabledCheckmarkColor", Color.class, // focused @@ -1045,9 +1049,11 @@ public class TestFlatStyleableInfo private void flatCheckBoxIcon( Map> expected ) { expectedMap( expected, - "focusWidth", int.class, + "focusWidth", float.class, "focusColor", Color.class, "borderWidth", float.class, + "selectedBorderWidth", float.class, + "disabledSelectedBorderWidth", float.class, "arc", int.class, // enabled @@ -1060,6 +1066,8 @@ public class TestFlatStyleableInfo // disabled "disabledBorderColor", Color.class, "disabledBackground", Color.class, + "disabledSelectedBorderColor", Color.class, + "disabledSelectedBackground", Color.class, "disabledCheckmarkColor", Color.class, // focused diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java index ba5385a8..4aa654ab 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java @@ -608,9 +608,11 @@ public class TestFlatStyling //---- icon ---- - ui.applyStyle( b, "icon.focusWidth: 2" ); + ui.applyStyle( b, "icon.focusWidth: 1.5" ); ui.applyStyle( b, "icon.focusColor: #fff" ); - ui.applyStyle( b, "icon.borderWidth: 1" ); + ui.applyStyle( b, "icon.borderWidth: 1.5" ); + ui.applyStyle( b, "icon.selectedBorderWidth: 1.5" ); + ui.applyStyle( b, "icon.disabledSelectedBorderWidth: 1.5" ); ui.applyStyle( b, "icon.arc: 5" ); // enabled @@ -623,6 +625,8 @@ public class TestFlatStyling // disabled ui.applyStyle( b, "icon.disabledBorderColor: #fff" ); ui.applyStyle( b, "icon.disabledBackground: #fff" ); + ui.applyStyle( b, "icon.disabledSelectedBorderColor: #fff" ); + ui.applyStyle( b, "icon.disabledSelectedBackground: #fff" ); ui.applyStyle( b, "icon.disabledCheckmarkColor: #fff" ); // focused @@ -1268,9 +1272,11 @@ public class TestFlatStyling } private void flatCheckBoxIcon( FlatCheckBoxIcon icon ) { - icon.applyStyleProperty( "focusWidth", 2 ); + icon.applyStyleProperty( "focusWidth", 1.5f ); icon.applyStyleProperty( "focusColor", Color.WHITE ); icon.applyStyleProperty( "borderWidth", 1.5f ); + icon.applyStyleProperty( "selectedBorderWidth", 1.5f ); + icon.applyStyleProperty( "disabledSelectedBorderWidth", 1.5f ); icon.applyStyleProperty( "arc", 5 ); // enabled @@ -1283,6 +1289,8 @@ public class TestFlatStyling // disabled icon.applyStyleProperty( "disabledBorderColor", Color.WHITE ); icon.applyStyleProperty( "disabledBackground", Color.WHITE ); + icon.applyStyleProperty( "disabledSelectedBorderColor", Color.WHITE ); + icon.applyStyleProperty( "disabledSelectedBackground", Color.WHITE ); icon.applyStyleProperty( "disabledCheckmarkColor", Color.WHITE ); // focused diff --git a/flatlaf-testing/dumps/uidefaults/FlatIntelliJLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatIntelliJLaf_1.8.0.txt index 80784f67..df1a31e8 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatIntelliJLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatIntelliJLaf_1.8.0.txt @@ -49,7 +49,7 @@ + CheckBox.icon.style filled -- CheckBox.icon[filled].selectedFocusedCheckmarkColor #eaf3fb HSL 208 68 95 javax.swing.plaf.ColorUIResource [UI] +- CheckBox.icon[filled].focusedCheckmarkColor #eaf3fb HSL 208 68 95 javax.swing.plaf.ColorUIResource [UI] #---- ComboBox ---- diff --git a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt index 51a88b32..6110addd 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt @@ -133,6 +133,9 @@ CheckBox.icon.checkmarkColor #ffffff HSL 0 0 100 javax.swing.plaf.Colo CheckBox.icon.disabledBackground #f2f2f2 HSL 0 0 95 javax.swing.plaf.ColorUIResource [UI] CheckBox.icon.disabledBorderColor #bdbdbd HSL 0 0 74 javax.swing.plaf.ColorUIResource [UI] CheckBox.icon.disabledCheckmarkColor #ababab HSL 0 0 67 javax.swing.plaf.ColorUIResource [UI] +CheckBox.icon.disabledSelectedBackground #f2f2f2 HSL 0 0 95 javax.swing.plaf.ColorUIResource [UI] +CheckBox.icon.disabledSelectedBorderColor #dbdbdb HSL 0 0 86 javax.swing.plaf.ColorUIResource [UI] +CheckBox.icon.disabledSelectedBorderWidth 2 CheckBox.icon.focusedBackground #00ffff HSL 180 100 50 javax.swing.plaf.ColorUIResource [UI] CheckBox.icon.focusedBorderColor #7b9fc7 HSL 212 40 63 javax.swing.plaf.ColorUIResource [UI] CheckBox.icon.focusedSelectedBorderColor #accff7 HSL 212 82 82 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index f2e48de9..9e47fa47 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -110,6 +110,8 @@ Button.toolbar.selectedBackground = #ddd #---- CheckBox ---- +CheckBox.icon.disabledSelectedBorderWidth = 2 + # enabled CheckBox.icon.borderColor = #878787 CheckBox.icon.background = #fff @@ -120,6 +122,8 @@ CheckBox.icon.checkmarkColor = #fff # disabled CheckBox.icon.disabledBorderColor = #BDBDBD CheckBox.icon.disabledBackground = #F2F2F2 +CheckBox.icon.disabledSelectedBorderColor = #dbdbdb +CheckBox.icon.disabledSelectedBackground = #F2F2F2 CheckBox.icon.disabledCheckmarkColor = #ABABAB # focused diff --git a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt index 38cfddc3..565950b2 100644 --- a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt +++ b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt @@ -81,6 +81,9 @@ CheckBox.icon.checkmarkColor CheckBox.icon.disabledBackground CheckBox.icon.disabledBorderColor CheckBox.icon.disabledCheckmarkColor +CheckBox.icon.disabledSelectedBackground +CheckBox.icon.disabledSelectedBorderColor +CheckBox.icon.disabledSelectedBorderWidth CheckBox.icon.focusColor CheckBox.icon.focusWidth CheckBox.icon.focusedBackground @@ -100,14 +103,20 @@ CheckBox.icon.pressedSelectedBackground CheckBox.icon.pressedSelectedBorderColor CheckBox.icon.selectedBackground CheckBox.icon.selectedBorderColor +CheckBox.icon.selectedBorderWidth CheckBox.icon.style CheckBox.iconTextGap CheckBox.icon[filled].background CheckBox.icon[filled].borderColor +CheckBox.icon[filled].borderWidth CheckBox.icon[filled].checkmarkColor CheckBox.icon[filled].disabledBackground CheckBox.icon[filled].disabledBorderColor CheckBox.icon[filled].disabledCheckmarkColor +CheckBox.icon[filled].disabledSelectedBackground +CheckBox.icon[filled].disabledSelectedBorderColor +CheckBox.icon[filled].disabledSelectedBorderWidth +CheckBox.icon[filled].focusWidth CheckBox.icon[filled].focusedBackground CheckBox.icon[filled].focusedBorderColor CheckBox.icon[filled].focusedCheckmarkColor @@ -125,6 +134,7 @@ CheckBox.icon[filled].pressedSelectedBackground CheckBox.icon[filled].pressedSelectedBorderColor CheckBox.icon[filled].selectedBackground CheckBox.icon[filled].selectedBorderColor +CheckBox.icon[filled].selectedBorderWidth CheckBox.margin CheckBox.rollover CheckBox.textIconGap