Button: support hover, pressed and focused backgrounds

This commit is contained in:
Karl Tauber
2019-09-12 12:19:19 +02:00
parent 02de416830
commit ce4e488529
7 changed files with 86 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Paint; import java.awt.Paint;
import javax.swing.AbstractButton;
import javax.swing.UIManager; import javax.swing.UIManager;
/** /**
@@ -29,7 +30,9 @@ import javax.swing.UIManager;
* @uiDefault Button.borderColor Color * @uiDefault Button.borderColor Color
* @uiDefault Button.disabledBorderColor Color * @uiDefault Button.disabledBorderColor Color
* @uiDefault Button.focusedBorderColor Color * @uiDefault Button.focusedBorderColor Color
* @uiDefault Button.hoverBorderColor Color optional
* @uiDefault Button.default.borderColor Color * @uiDefault Button.default.borderColor Color
* @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.arc int * @uiDefault Button.arc int
@@ -42,7 +45,9 @@ public class FlatButtonBorder
protected final Color borderColor = UIManager.getColor( "Button.borderColor" ); protected final Color borderColor = UIManager.getColor( "Button.borderColor" );
protected final Color disabledBorderColor = UIManager.getColor( "Button.disabledBorderColor" ); protected final Color disabledBorderColor = UIManager.getColor( "Button.disabledBorderColor" );
protected final Color focusedBorderColor = UIManager.getColor( "Button.focusedBorderColor" ); protected final Color focusedBorderColor = UIManager.getColor( "Button.focusedBorderColor" );
protected final Color hoverBorderColor = UIManager.getColor( "Button.hoverBorderColor" );
protected final Color defaultBorderColor = UIManager.getColor( "Button.default.borderColor" ); protected final Color defaultBorderColor = UIManager.getColor( "Button.default.borderColor" );
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 arc = UIManager.getInt( "Button.arc" ); protected final int arc = UIManager.getInt( "Button.arc" );
@@ -62,6 +67,13 @@ public class FlatButtonBorder
protected Paint getBorderColor( Component c ) { protected Paint getBorderColor( Component c ) {
if( c.isEnabled() ) { if( c.isEnabled() ) {
boolean def = FlatButtonUI.isDefaultButton( c ); boolean def = FlatButtonUI.isDefaultButton( c );
if( c instanceof AbstractButton && ((AbstractButton)c).getModel().isRollover() ) {
Color color = def ? defaultHoverBorderColor : hoverBorderColor;
if( color != null )
return color;
}
if( c.hasFocus() ) if( c.hasFocus() )
return def ? defaultFocusedBorderColor : focusedBorderColor; return def ? defaultFocusedBorderColor : focusedBorderColor;

View File

@@ -44,9 +44,15 @@ import javax.swing.plaf.basic.BasicButtonUI;
* @uiDefault Component.focusWidth int * @uiDefault Component.focusWidth int
* @uiDefault Button.arc int * @uiDefault Button.arc int
* @uiDefault Button.minimumWidth int * @uiDefault Button.minimumWidth int
* @uiDefault Button.focusedBackground Color optional
* @uiDefault Button.hoverBackground Color optional
* @uiDefault Button.pressedBackground Color optional
* @uiDefault Button.disabledText Color * @uiDefault Button.disabledText Color
* @uiDefault Button.default.background Color * @uiDefault Button.default.background Color
* @uiDefault Button.default.foreground Color * @uiDefault Button.default.foreground Color
* @uiDefault Button.default.focusedBackground Color optional
* @uiDefault Button.default.hoverBackground Color optional
* @uiDefault Button.default.pressedBackground Color optional
* @uiDefault Button.toolbar.hoverBackground Color * @uiDefault Button.toolbar.hoverBackground Color
* @uiDefault Button.toolbar.pressedBackground Color * @uiDefault Button.toolbar.pressedBackground Color
* *
@@ -59,9 +65,17 @@ public class FlatButtonUI
protected int arc; protected int arc;
protected int minimumWidth; protected int minimumWidth;
protected Color focusedBackground;
protected Color hoverBackground;
protected Color pressedBackground;
protected Color disabledText; protected Color disabledText;
protected Color defaultBackground; protected Color defaultBackground;
protected Color defaultForeground; protected Color defaultForeground;
protected Color defaultFocusedBackground;
protected Color defaultHoverBackground;
protected Color defaultPressedBackground;
protected Color toolbarHoverBackground; protected Color toolbarHoverBackground;
protected Color toolbarPressedBackground; protected Color toolbarPressedBackground;
@@ -85,9 +99,17 @@ public class FlatButtonUI
arc = UIManager.getInt( prefix + "arc" ); arc = UIManager.getInt( prefix + "arc" );
minimumWidth = UIManager.getInt( prefix + "minimumWidth" ); minimumWidth = UIManager.getInt( prefix + "minimumWidth" );
focusedBackground = UIManager.getColor( prefix + "focusedBackground" );
hoverBackground = UIManager.getColor( prefix + "hoverBackground" );
pressedBackground = UIManager.getColor( prefix + "pressedBackground" );
disabledText = UIManager.getColor( prefix + "disabledText" ); disabledText = UIManager.getColor( prefix + "disabledText" );
defaultBackground = UIManager.getColor( prefix + "default.background" );
defaultForeground = UIManager.getColor( prefix + "default.foreground" ); defaultBackground = UIManager.getColor( "Button.default.background" );
defaultForeground = UIManager.getColor( "Button.default.foreground" );
defaultFocusedBackground = UIManager.getColor( "Button.default.focusedBackground" );
defaultHoverBackground = UIManager.getColor( "Button.default.hoverBackground" );
defaultPressedBackground = UIManager.getColor( "Button.default.pressedBackground" );
toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" ); toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" );
toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" ); toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" );
@@ -175,6 +197,25 @@ public class FlatButtonUI
} }
boolean def = isDefaultButton( c ); boolean def = isDefaultButton( c );
if( model.isPressed() ) {
Color color = def ? defaultPressedBackground : pressedBackground;
if( color != null )
return color;
}
if( model.isRollover() ) {
Color color = def ? defaultHoverBackground : hoverBackground;
if( color != null )
return color;
}
if( c.hasFocus() ) {
Color color = def ? defaultFocusedBackground : focusedBackground;
if( color != null )
return color;
}
return def ? defaultBackground : c.getBackground(); return def ? defaultBackground : c.getBackground();
} }

View File

@@ -61,14 +61,20 @@ window=@background
#---- Button ---- #---- Button ----
Button.background=4c5052 Button.background=4c5052
Button.hoverBackground=525658
Button.pressedBackground=5c6164
Button.borderColor=5e6060 Button.borderColor=5e6060
Button.disabledBorderColor=5e6060 Button.disabledBorderColor=5e6060
Button.focusedBorderColor=466d94 Button.focusedBorderColor=466d94
Button.hoverBorderColor=466d94
Button.default.background=365880 Button.default.background=365880
Button.default.foreground=bbbbbb Button.default.foreground=bbbbbb
Button.default.hoverBackground=3d6185
Button.default.pressedBackground=43688c
Button.default.borderColor=4c708c Button.default.borderColor=4c708c
Button.default.hoverBorderColor=537699
Button.default.focusedBorderColor=537699 Button.default.focusedBorderColor=537699
Button.default.focusColor=43688c Button.default.focusColor=43688c

View File

@@ -18,6 +18,11 @@
# which is licensed under the Apache 2.0 license. Copyright 2000-2019 JetBrains s.r.o. # which is licensed under the Apache 2.0 license. Copyright 2000-2019 JetBrains s.r.o.
# See: https://github.com/JetBrains/intellij-community/ # See: https://github.com/JetBrains/intellij-community/
#---- Button ----
Button.focusedBackground=null
#---- CheckBox ---- #---- CheckBox ----
CheckBox.icon.selectedBorderColor=4982CC CheckBox.icon.selectedBorderColor=4982CC

View File

@@ -351,6 +351,7 @@ TitledBorder.border=1,1,1,1,@@Separator.foreground
ToggleButton.border=com.formdev.flatlaf.ui.FlatButtonBorder ToggleButton.border=com.formdev.flatlaf.ui.FlatButtonBorder
ToggleButton.arc=6 ToggleButton.arc=6
ToggleButton.rollover=true
ToggleButton.background=@@Button.background ToggleButton.background=@@Button.background

View File

@@ -61,14 +61,21 @@ window=@background
#---- Button ---- #---- Button ----
Button.background=ffffff Button.background=ffffff
Button.focusedBackground=e3f1fa
Button.hoverBackground=f8f8f8
Button.pressedBackground=dfdfdf
Button.borderColor=bfbfbf Button.borderColor=bfbfbf
Button.disabledBorderColor=cfcfcf Button.disabledBorderColor=cfcfcf
Button.focusedBorderColor=87afda Button.focusedBorderColor=87afda
Button.hoverBorderColor=87afda
Button.default.background=4A86C7 Button.default.background=4A86C7
Button.default.foreground=f0f0f0 Button.default.foreground=f0f0f0
Button.default.hoverBackground=5B91CC
Button.default.pressedBackground=6E9ED2
Button.default.borderColor=3167ad Button.default.borderColor=3167ad
Button.default.hoverBorderColor=a8cef6
Button.default.focusedBorderColor=a8cef6 Button.default.focusedBorderColor=a8cef6
Button.default.focusColor=97c3f3 Button.default.focusColor=97c3f3

View File

@@ -44,15 +44,22 @@
#---- Button ---- #---- Button ----
Button.background=ffffff Button.background=ffffff
Button.focusedBackground=00ffff
Button.hoverBackground=ffff00
Button.pressedBackground=FFC800
Button.borderColor=0000ff Button.borderColor=0000ff
Button.disabledBorderColor=000088 Button.disabledBorderColor=000088
Button.focusedBorderColor=466d94 Button.focusedBorderColor=466d94
#Button.arc=10 Button.hoverBorderColor=ff0000
Button.default.background=dddddd Button.default.background=dddddd
Button.default.foreground=880000 Button.default.foreground=880000
Button.default.focusedBackground=00ffff
Button.default.hoverBackground=ffff00
Button.default.pressedBackground=FFC800
Button.default.borderColor=ff0000 Button.default.borderColor=ff0000
Button.default.hoverBorderColor=ff0000
Button.default.focusedBorderColor=537699 Button.default.focusedBorderColor=537699
Button.default.focusColor=ff0000 Button.default.focusColor=ff0000
@@ -232,6 +239,10 @@ ToggleButton.selectedBackground=44ff44
ToggleButton.selectedForeground=000000 ToggleButton.selectedForeground=000000
ToggleButton.disabledSelectedBackground=44dd44 ToggleButton.disabledSelectedBackground=44dd44
ToggleButton.focusedBackground=00ffff
ToggleButton.hoverBackground=ffff00
ToggleButton.pressedBackground=FFC800
#---- ToolTip ---- #---- ToolTip ----