From ce4e488529c6d6c4026ceac2c2fb2311fbff1a1e Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 12 Sep 2019 12:19:19 +0200 Subject: [PATCH] Button: support hover, pressed and focused backgrounds --- .../formdev/flatlaf/ui/FlatButtonBorder.java | 12 +++++ .../com/formdev/flatlaf/ui/FlatButtonUI.java | 45 ++++++++++++++++++- .../formdev/flatlaf/FlatDarkLaf.properties | 6 +++ .../flatlaf/FlatIntelliJLaf.properties | 5 +++ .../com/formdev/flatlaf/FlatLaf.properties | 1 + .../formdev/flatlaf/FlatLightLaf.properties | 7 +++ .../formdev/flatlaf/FlatTestLaf.properties | 13 +++++- 7 files changed, 86 insertions(+), 3 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java index 322c4300..9d5146d9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonBorder.java @@ -21,6 +21,7 @@ import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Paint; +import javax.swing.AbstractButton; import javax.swing.UIManager; /** @@ -29,7 +30,9 @@ import javax.swing.UIManager; * @uiDefault Button.borderColor Color * @uiDefault Button.disabledBorderColor Color * @uiDefault Button.focusedBorderColor Color + * @uiDefault Button.hoverBorderColor Color optional * @uiDefault Button.default.borderColor Color + * @uiDefault Button.default.hoverBorderColor Color optional * @uiDefault Button.default.focusedBorderColor Color * @uiDefault Button.default.focusColor Color * @uiDefault Button.arc int @@ -42,7 +45,9 @@ public class FlatButtonBorder protected final Color borderColor = UIManager.getColor( "Button.borderColor" ); protected final Color disabledBorderColor = UIManager.getColor( "Button.disabledBorderColor" ); 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 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 arc = UIManager.getInt( "Button.arc" ); @@ -62,6 +67,13 @@ public class FlatButtonBorder protected Paint getBorderColor( Component c ) { if( c.isEnabled() ) { 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() ) return def ? defaultFocusedBorderColor : focusedBorderColor; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java index 801827d6..9f6f74e5 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatButtonUI.java @@ -44,9 +44,15 @@ import javax.swing.plaf.basic.BasicButtonUI; * @uiDefault Component.focusWidth int * @uiDefault Button.arc 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.default.background 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.pressedBackground Color * @@ -59,9 +65,17 @@ public class FlatButtonUI protected int arc; protected int minimumWidth; + protected Color focusedBackground; + protected Color hoverBackground; + protected Color pressedBackground; protected Color disabledText; + protected Color defaultBackground; protected Color defaultForeground; + protected Color defaultFocusedBackground; + protected Color defaultHoverBackground; + protected Color defaultPressedBackground; + protected Color toolbarHoverBackground; protected Color toolbarPressedBackground; @@ -85,9 +99,17 @@ public class FlatButtonUI arc = UIManager.getInt( prefix + "arc" ); 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" ); - 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" ); toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" ); @@ -175,6 +197,25 @@ public class FlatButtonUI } 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(); } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index cb14f212..d847b06f 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -61,14 +61,20 @@ window=@background #---- Button ---- Button.background=4c5052 +Button.hoverBackground=525658 +Button.pressedBackground=5c6164 Button.borderColor=5e6060 Button.disabledBorderColor=5e6060 Button.focusedBorderColor=466d94 +Button.hoverBorderColor=466d94 Button.default.background=365880 Button.default.foreground=bbbbbb +Button.default.hoverBackground=3d6185 +Button.default.pressedBackground=43688c Button.default.borderColor=4c708c +Button.default.hoverBorderColor=537699 Button.default.focusedBorderColor=537699 Button.default.focusColor=43688c diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatIntelliJLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatIntelliJLaf.properties index 069dc434..b913ed3f 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatIntelliJLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatIntelliJLaf.properties @@ -18,6 +18,11 @@ # which is licensed under the Apache 2.0 license. Copyright 2000-2019 JetBrains s.r.o. # See: https://github.com/JetBrains/intellij-community/ +#---- Button ---- + +Button.focusedBackground=null + + #---- CheckBox ---- CheckBox.icon.selectedBorderColor=4982CC diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 64cda371..a46ce080 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -351,6 +351,7 @@ TitledBorder.border=1,1,1,1,@@Separator.foreground ToggleButton.border=com.formdev.flatlaf.ui.FlatButtonBorder ToggleButton.arc=6 +ToggleButton.rollover=true ToggleButton.background=@@Button.background diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index 4e93dadb..0dc773d1 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -61,14 +61,21 @@ window=@background #---- Button ---- Button.background=ffffff +Button.focusedBackground=e3f1fa +Button.hoverBackground=f8f8f8 +Button.pressedBackground=dfdfdf Button.borderColor=bfbfbf Button.disabledBorderColor=cfcfcf Button.focusedBorderColor=87afda +Button.hoverBorderColor=87afda Button.default.background=4A86C7 Button.default.foreground=f0f0f0 +Button.default.hoverBackground=5B91CC +Button.default.pressedBackground=6E9ED2 Button.default.borderColor=3167ad +Button.default.hoverBorderColor=a8cef6 Button.default.focusedBorderColor=a8cef6 Button.default.focusColor=97c3f3 diff --git a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties index 591f0425..d6126879 100644 --- a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties +++ b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties @@ -44,15 +44,22 @@ #---- Button ---- Button.background=ffffff +Button.focusedBackground=00ffff +Button.hoverBackground=ffff00 +Button.pressedBackground=FFC800 Button.borderColor=0000ff Button.disabledBorderColor=000088 Button.focusedBorderColor=466d94 -#Button.arc=10 +Button.hoverBorderColor=ff0000 Button.default.background=dddddd Button.default.foreground=880000 +Button.default.focusedBackground=00ffff +Button.default.hoverBackground=ffff00 +Button.default.pressedBackground=FFC800 Button.default.borderColor=ff0000 +Button.default.hoverBorderColor=ff0000 Button.default.focusedBorderColor=537699 Button.default.focusColor=ff0000 @@ -232,6 +239,10 @@ ToggleButton.selectedBackground=44ff44 ToggleButton.selectedForeground=000000 ToggleButton.disabledSelectedBackground=44dd44 +ToggleButton.focusedBackground=00ffff +ToggleButton.hoverBackground=ffff00 +ToggleButton.pressedBackground=FFC800 + #---- ToolTip ----