Styling: support Help button

This commit is contained in:
Karl Tauber
2021-06-25 14:27:44 +02:00
parent b4f7b1d71d
commit afdbf711f7
4 changed files with 89 additions and 23 deletions

View File

@@ -24,7 +24,9 @@ import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D; import java.awt.geom.Path2D;
import javax.swing.UIManager; import javax.swing.UIManager;
import com.formdev.flatlaf.ui.FlatButtonUI; import com.formdev.flatlaf.ui.FlatButtonUI;
import com.formdev.flatlaf.ui.FlatStyleSupport;
import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
/** /**
* Help button icon for {@link javax.swing.JButton}. * Help button icon for {@link javax.swing.JButton}.
@@ -50,29 +52,34 @@ import com.formdev.flatlaf.ui.FlatUIUtils;
public class FlatHelpButtonIcon public class FlatHelpButtonIcon
extends FlatAbstractIcon extends FlatAbstractIcon
{ {
protected final int focusWidth = UIManager.getInt( "Component.focusWidth" ); @Styleable protected int focusWidth = UIManager.getInt( "Component.focusWidth" );
protected final Color focusColor = UIManager.getColor( "Component.focusColor" ); @Styleable protected Color focusColor = UIManager.getColor( "Component.focusColor" );
protected final float innerFocusWidth = FlatUIUtils.getUIFloat( "HelpButton.innerFocusWidth", FlatUIUtils.getUIFloat( "Component.innerFocusWidth", 0 ) ); @Styleable protected float innerFocusWidth = FlatUIUtils.getUIFloat( "HelpButton.innerFocusWidth", FlatUIUtils.getUIFloat( "Component.innerFocusWidth", 0 ) );
protected final int borderWidth = FlatUIUtils.getUIInt( "HelpButton.borderWidth", 1 ); @Styleable protected int borderWidth = FlatUIUtils.getUIInt( "HelpButton.borderWidth", 1 );
protected final Color borderColor = UIManager.getColor( "HelpButton.borderColor" ); @Styleable protected Color borderColor = UIManager.getColor( "HelpButton.borderColor" );
protected final Color disabledBorderColor = UIManager.getColor( "HelpButton.disabledBorderColor" ); @Styleable protected Color disabledBorderColor = UIManager.getColor( "HelpButton.disabledBorderColor" );
protected final Color focusedBorderColor = UIManager.getColor( "HelpButton.focusedBorderColor" ); @Styleable protected Color focusedBorderColor = UIManager.getColor( "HelpButton.focusedBorderColor" );
protected final Color hoverBorderColor = UIManager.getColor( "HelpButton.hoverBorderColor" ); @Styleable protected Color hoverBorderColor = UIManager.getColor( "HelpButton.hoverBorderColor" );
protected final Color background = UIManager.getColor( "HelpButton.background" ); @Styleable protected Color background = UIManager.getColor( "HelpButton.background" );
protected final Color disabledBackground = UIManager.getColor( "HelpButton.disabledBackground" ); @Styleable protected Color disabledBackground = UIManager.getColor( "HelpButton.disabledBackground" );
protected final Color focusedBackground = UIManager.getColor( "HelpButton.focusedBackground" ); @Styleable protected Color focusedBackground = UIManager.getColor( "HelpButton.focusedBackground" );
protected final Color hoverBackground = UIManager.getColor( "HelpButton.hoverBackground" ); @Styleable protected Color hoverBackground = UIManager.getColor( "HelpButton.hoverBackground" );
protected final Color pressedBackground = UIManager.getColor( "HelpButton.pressedBackground" ); @Styleable protected Color pressedBackground = UIManager.getColor( "HelpButton.pressedBackground" );
protected final Color questionMarkColor = UIManager.getColor( "HelpButton.questionMarkColor" ); @Styleable protected Color questionMarkColor = UIManager.getColor( "HelpButton.questionMarkColor" );
protected final Color disabledQuestionMarkColor = UIManager.getColor( "HelpButton.disabledQuestionMarkColor" ); @Styleable protected Color disabledQuestionMarkColor = UIManager.getColor( "HelpButton.disabledQuestionMarkColor" );
protected final int iconSize = 22 + (focusWidth * 2);
public FlatHelpButtonIcon() { public FlatHelpButtonIcon() {
super( 0, 0, null ); super( 0, 0, null );
} }
/**
* @since TODO
*/
public Object applyStyleProperty( String key, Object value ) {
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
@Override @Override
protected void paintIcon( Component c, Graphics2D g2 ) { protected void paintIcon( Component c, Graphics2D g2 ) {
/* /*
@@ -89,7 +96,7 @@ public class FlatHelpButtonIcon
boolean focused = FlatUIUtils.isPermanentFocusOwner( c ); boolean focused = FlatUIUtils.isPermanentFocusOwner( c );
float xy = 0.5f; float xy = 0.5f;
float wh = iconSize - 1; float wh = iconSize() - 1;
// paint outer focus border // paint outer focus border
if( focused && FlatButtonUI.isFocusPainted( c ) ) { if( focused && FlatButtonUI.isFocusPainted( c ) ) {
@@ -151,11 +158,15 @@ public class FlatHelpButtonIcon
@Override @Override
public int getIconWidth() { public int getIconWidth() {
return scale( iconSize ); return scale( iconSize() );
} }
@Override @Override
public int getIconHeight() { public int getIconHeight() {
return scale( iconSize ); return scale( iconSize() );
}
private int iconSize() {
return 22 + (focusWidth * 2);
} }
} }

View File

@@ -46,6 +46,7 @@ import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicButtonListener; import javax.swing.plaf.basic.BasicButtonListener;
import javax.swing.plaf.basic.BasicButtonUI; import javax.swing.plaf.basic.BasicButtonUI;
import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.icons.FlatHelpButtonIcon;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable; import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStyleSupport.UnknownStyleException; import com.formdev.flatlaf.ui.FlatStyleSupport.UnknownStyleException;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
@@ -137,6 +138,7 @@ public class FlatButtonUI
private final boolean shared; private final boolean shared;
private boolean borderShared = true; private boolean borderShared = true;
private boolean helpButtonIconShared = true;
private boolean defaults_initialized = false; private boolean defaults_initialized = false;
private Map<String, Object> oldStyleValues; private Map<String, Object> oldStyleValues;
@@ -274,6 +276,19 @@ public class FlatButtonUI
* @since TODO * @since TODO
*/ */
protected Object applyStyleProperty( AbstractButton b, String key, Object value ) { protected Object applyStyleProperty( AbstractButton b, String key, Object value ) {
if( key.startsWith( "help." ) ) {
if( !(helpButtonIcon instanceof FlatHelpButtonIcon) )
return new UnknownStyleException( key );
if( helpButtonIconShared ) {
helpButtonIcon = FlatStyleSupport.cloneIcon( helpButtonIcon );
helpButtonIconShared = false;
}
key = key.substring( "help.".length() );
return ((FlatHelpButtonIcon)helpButtonIcon).applyStyleProperty( key, value );
}
try { try {
return FlatStyleSupport.applyToAnnotatedObject( this, key, value ); return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
} catch( UnknownStyleException ex ) { } catch( UnknownStyleException ex ) {

View File

@@ -37,6 +37,7 @@ import javax.swing.plaf.basic.BasicRadioButtonUI;
import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.icons.FlatCheckBoxIcon; import com.formdev.flatlaf.icons.FlatCheckBoxIcon;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable; import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStyleSupport.UnknownStyleException;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
/** /**
@@ -164,16 +165,15 @@ public class FlatRadioButtonUI
protected Object applyStyleProperty( String key, Object value ) { protected Object applyStyleProperty( String key, Object value ) {
// style icon // style icon
if( key.startsWith( "icon." ) ) { if( key.startsWith( "icon." ) ) {
key = key.substring( "icon.".length() );
if( !(icon instanceof FlatCheckBoxIcon) ) if( !(icon instanceof FlatCheckBoxIcon) )
return null; return new UnknownStyleException( key );
if( iconShared ) { if( iconShared ) {
icon = FlatStyleSupport.cloneIcon( icon ); icon = FlatStyleSupport.cloneIcon( icon );
iconShared = false; iconShared = false;
} }
key = key.substring( "icon.".length() );
return ((FlatCheckBoxIcon)icon).applyStyleProperty( key, value ); return ((FlatCheckBoxIcon)icon).applyStyleProperty( key, value );
} }

View File

@@ -70,6 +70,25 @@ public class FlatStylingTests
ui.installUI( b ); ui.installUI( b );
button( b, ui ); button( b, ui );
//---- FlatHelpButtonIcon ----
ui.applyStyle( b, "help.focusWidth: 2" );
ui.applyStyle( b, "help.focusColor: #fff" );
ui.applyStyle( b, "help.innerFocusWidth: {float}0.5" );
ui.applyStyle( b, "help.borderWidth: 1" );
ui.applyStyle( b, "help.borderColor: #fff" );
ui.applyStyle( b, "help.disabledBorderColor: #fff" );
ui.applyStyle( b, "help.focusedBorderColor: #fff" );
ui.applyStyle( b, "help.hoverBorderColor: #fff" );
ui.applyStyle( b, "help.background: #fff" );
ui.applyStyle( b, "help.disabledBackground: #fff" );
ui.applyStyle( b, "help.focusedBackground: #fff" );
ui.applyStyle( b, "help.hoverBackground: #fff" );
ui.applyStyle( b, "help.pressedBackground: #fff" );
ui.applyStyle( b, "help.questionMarkColor: #fff" );
ui.applyStyle( b, "help.disabledQuestionMarkColor: #fff" );
} }
private void button( AbstractButton b, FlatButtonUI ui ) { private void button( AbstractButton b, FlatButtonUI ui ) {
@@ -821,4 +840,25 @@ public class FlatStylingTests
icon.applyStyleProperty( "disabledArrowColor", Color.WHITE ); icon.applyStyleProperty( "disabledArrowColor", Color.WHITE );
icon.applyStyleProperty( "selectionForeground", Color.WHITE ); icon.applyStyleProperty( "selectionForeground", Color.WHITE );
} }
@Test
void flatHelpButtonIcon() {
FlatHelpButtonIcon icon = new FlatHelpButtonIcon();
icon.applyStyleProperty( "focusWidth", 2 );
icon.applyStyleProperty( "focusColor", Color.WHITE );
icon.applyStyleProperty( "innerFocusWidth", 0.5f );
icon.applyStyleProperty( "borderWidth", 1 );
icon.applyStyleProperty( "borderColor", Color.WHITE );
icon.applyStyleProperty( "disabledBorderColor", Color.WHITE );
icon.applyStyleProperty( "focusedBorderColor", Color.WHITE );
icon.applyStyleProperty( "hoverBorderColor", Color.WHITE );
icon.applyStyleProperty( "background", Color.WHITE );
icon.applyStyleProperty( "disabledBackground", Color.WHITE );
icon.applyStyleProperty( "focusedBackground", Color.WHITE );
icon.applyStyleProperty( "hoverBackground", Color.WHITE );
icon.applyStyleProperty( "pressedBackground", Color.WHITE );
icon.applyStyleProperty( "questionMarkColor", Color.WHITE );
icon.applyStyleProperty( "disabledQuestionMarkColor", Color.WHITE );
}
} }