Button: prefer explicitly set background/foreground over focused background and "default" background/foreground (issue #116)

This commit is contained in:
Karl Tauber
2020-06-24 12:43:49 +02:00
parent b72916187a
commit 6914a6132c

View File

@@ -93,6 +93,9 @@ public class FlatButtonUI
protected int minimumWidth; protected int minimumWidth;
protected int iconTextGap; protected int iconTextGap;
protected Color background;
protected Color foreground;
protected Color startBackground; protected Color startBackground;
protected Color endBackground; protected Color endBackground;
protected Color focusedBackground; protected Color focusedBackground;
@@ -139,6 +142,9 @@ public class FlatButtonUI
minimumWidth = UIManager.getInt( prefix + "minimumWidth" ); minimumWidth = UIManager.getInt( prefix + "minimumWidth" );
iconTextGap = FlatUIUtils.getUIInt( prefix + "iconTextGap", 4 ); iconTextGap = FlatUIUtils.getUIInt( prefix + "iconTextGap", 4 );
background = UIManager.getColor( prefix + "background" );
foreground = UIManager.getColor( prefix + "foreground" );
startBackground = UIManager.getColor( prefix + "startBackground" ); startBackground = UIManager.getColor( prefix + "startBackground" );
endBackground = UIManager.getColor( prefix + "endBackground" ); endBackground = UIManager.getColor( prefix + "endBackground" );
focusedBackground = UIManager.getColor( prefix + "focusedBackground" ); focusedBackground = UIManager.getColor( prefix + "focusedBackground" );
@@ -280,7 +286,9 @@ public class FlatButtonUI
protected void paintBackground( Graphics g, JComponent c ) { protected void paintBackground( Graphics g, JComponent c ) {
Color background = getBackground( c ); Color background = getBackground( c );
if( background != null ) { if( background == null )
return;
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
@@ -320,14 +328,13 @@ public class FlatButtonUI
if( background == startBg && endBg != null && !startBg.equals( endBg ) ) if( background == startBg && endBg != null && !startBg.equals( endBg ) )
g2.setPaint( new GradientPaint( 0, 0, startBg, 0, height, endBg ) ); g2.setPaint( new GradientPaint( 0, 0, startBg, 0, height, endBg ) );
else else
g2.setColor( FlatUIUtils.deriveColor( background, def ? defaultBackground : c.getBackground() ) ); g2.setColor( FlatUIUtils.deriveColor( background, getBackgroundBase( c, def ) ) );
FlatUIUtils.paintComponentBackground( g2, x, y, width, height, focusWidth, arc ); FlatUIUtils.paintComponentBackground( g2, x, y, width, height, focusWidth, arc );
} finally { } finally {
g2.dispose(); g2.dispose();
} }
} }
}
@Override @Override
public void paint( Graphics g, JComponent c ) { public void paint( Graphics g, JComponent c ) {
@@ -380,13 +387,26 @@ public class FlatButtonUI
boolean def = isDefaultButton( c ); boolean def = isDefaultButton( c );
return buttonStateColor( c, return buttonStateColor( c,
def ? defaultBackground : c.getBackground(), getBackgroundBase( c, def ),
null, null,
def ? defaultFocusedBackground : focusedBackground, isCustomBackground( c.getBackground() ) ? null : (def ? defaultFocusedBackground : focusedBackground),
def ? defaultHoverBackground : hoverBackground, def ? defaultHoverBackground : hoverBackground,
def ? defaultPressedBackground : pressedBackground ); def ? defaultPressedBackground : pressedBackground );
} }
protected Color getBackgroundBase( JComponent c, boolean def ) {
// use component background if explicitly set
Color bg = c.getBackground();
if( isCustomBackground( bg ) )
return bg;
return def ? defaultBackground : bg;
}
protected boolean isCustomBackground( Color bg ) {
return bg != background && (startBackground == null || bg != startBackground);
}
public static Color buttonStateColor( Component c, Color enabledColor, Color disabledColor, public static Color buttonStateColor( Component c, Color enabledColor, Color disabledColor,
Color focusedColor, Color hoverColor, Color pressedColor ) Color focusedColor, Color hoverColor, Color pressedColor )
{ {
@@ -411,8 +431,17 @@ public class FlatButtonUI
if( !c.isEnabled() ) if( !c.isEnabled() )
return disabledText; return disabledText;
// use component foreground if explicitly set
Color fg = c.getForeground();
if( isCustomForeground( fg ) )
return fg;
boolean def = isDefaultButton( c ); boolean def = isDefaultButton( c );
return def ? defaultForeground : c.getForeground(); return def ? defaultForeground : fg;
}
protected boolean isCustomForeground( Color fg ) {
return fg != foreground;
} }
@Override @Override