From e55b2afd60e76909102d461ba71def515ec55c42 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 25 Aug 2020 16:41:40 +0200 Subject: [PATCH] Button: show "selected" state (issue #161) --- CHANGELOG.md | 4 ++ .../com/formdev/flatlaf/ui/FlatButtonUI.java | 26 ++++++++++ .../flatlaf/ui/FlatToggleButtonUI.java | 52 +++---------------- .../formdev/flatlaf/FlatDarkLaf.properties | 4 ++ .../formdev/flatlaf/FlatLightLaf.properties | 4 ++ .../uidefaults/FlatDarkLaf_1.8.0_202.txt | 4 ++ .../uidefaults/FlatLightLaf_1.8.0_202.txt | 4 ++ .../flatlaf/themeeditor/FlatLafUIKeys.txt | 4 ++ 8 files changed, 56 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46cf81a0..be6abd7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ FlatLaf Change Log - Extras: `FlatSVGIcon` now allows specifying `ClassLoader` that is used to load SVG file. (issue #163) +#### Fixed bugs + +- Button: "selected" state was not shown. (issue #161) + #### Other Changes - Extras: Updated dependency 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 c43a25a1..ef3e0d7a 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 @@ -67,8 +67,11 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault Button.focusedBackground Color optional * @uiDefault Button.hoverBackground Color optional * @uiDefault Button.pressedBackground Color optional + * @uiDefault Button.selectedBackground Color + * @uiDefault Button.selectedForeground Color * @uiDefault Button.disabledBackground Color optional * @uiDefault Button.disabledText Color + * @uiDefault Button.disabledSelectedBackground Color * @uiDefault Button.default.background Color * @uiDefault Button.default.startBackground Color optional; if set, a gradient paint is used and Button.default.background is ignored * @uiDefault Button.default.endBackground Color optional; if set, a gradient paint is used @@ -84,6 +87,7 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault Button.toolbar.spacingInsets Insets * @uiDefault Button.toolbar.hoverBackground Color * @uiDefault Button.toolbar.pressedBackground Color + * @uiDefault Button.toolbar.selectedBackground Color * * @author Karl Tauber */ @@ -101,8 +105,11 @@ public class FlatButtonUI protected Color focusedBackground; protected Color hoverBackground; protected Color pressedBackground; + protected Color selectedBackground; + protected Color selectedForeground; protected Color disabledBackground; protected Color disabledText; + protected Color disabledSelectedBackground; protected Color defaultBackground; protected Color defaultEndBackground; @@ -119,6 +126,7 @@ public class FlatButtonUI protected Insets toolbarSpacingInsets; protected Color toolbarHoverBackground; protected Color toolbarPressedBackground; + protected Color toolbarSelectedBackground; private Icon helpButtonIcon; @@ -150,8 +158,11 @@ public class FlatButtonUI focusedBackground = UIManager.getColor( prefix + "focusedBackground" ); hoverBackground = UIManager.getColor( prefix + "hoverBackground" ); pressedBackground = UIManager.getColor( prefix + "pressedBackground" ); + selectedBackground = UIManager.getColor( prefix + "selectedBackground" ); + selectedForeground = UIManager.getColor( prefix + "selectedForeground" ); disabledBackground = UIManager.getColor( prefix + "disabledBackground" ); disabledText = UIManager.getColor( prefix + "disabledText" ); + disabledSelectedBackground = UIManager.getColor( prefix + "disabledSelectedBackground" ); if( UIManager.getBoolean( "Button.paintShadow" ) ) { shadowWidth = FlatUIUtils.getUIInt( "Button.shadowWidth", 2 ); @@ -174,6 +185,7 @@ public class FlatButtonUI toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" ); toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" ); toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" ); + toolbarSelectedBackground = UIManager.getColor( prefix + "toolbar.selectedBackground" ); helpButtonIcon = UIManager.getIcon( "HelpButton.icon" ); @@ -369,6 +381,17 @@ public class FlatButtonUI } protected Color getBackground( JComponent c ) { + if( ((AbstractButton)c).isSelected() ) { + // in toolbar use same colors for disabled and enabled because + // we assume that toolbar icon is shown disabled + boolean toolBarButton = isToolBarButton( c ); + return buttonStateColor( c, + toolBarButton ? toolbarSelectedBackground : selectedBackground, + toolBarButton ? toolbarSelectedBackground : disabledSelectedBackground, + null, null, + toolBarButton ? toolbarPressedBackground : pressedBackground ); + } + if( !c.isEnabled() ) return disabledBackground; @@ -430,6 +453,9 @@ public class FlatButtonUI if( !c.isEnabled() ) return disabledText; + if( ((AbstractButton)c).isSelected() && !isToolBarButton( c ) ) + return selectedForeground; + // use component foreground if explicitly set Color fg = c.getForeground(); if( isCustomForeground( fg ) ) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java index a4f25112..37a2d551 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToggleButtonUI.java @@ -22,7 +22,6 @@ import java.awt.Component; import java.awt.Graphics; import java.beans.PropertyChangeEvent; import javax.swing.AbstractButton; -import javax.swing.ButtonModel; import javax.swing.JComponent; import javax.swing.JToggleButton; import javax.swing.UIManager; @@ -50,18 +49,17 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault ToggleButton.startBackground Color optional; if set, a gradient paint is used and ToggleButton.background is ignored * @uiDefault ToggleButton.endBackground Color optional; if set, a gradient paint is used * @uiDefault ToggleButton.pressedBackground Color - * @uiDefault ToggleButton.disabledBackground Color optional - * @uiDefault ToggleButton.disabledText Color - * @uiDefault ToggleButton.toolbar.hoverBackground Color - * @uiDefault ToggleButton.toolbar.pressedBackground Color - * - * - * * @uiDefault ToggleButton.selectedBackground Color * @uiDefault ToggleButton.selectedForeground Color + * @uiDefault ToggleButton.disabledBackground Color optional + * @uiDefault ToggleButton.disabledText Color * @uiDefault ToggleButton.disabledSelectedBackground Color + * @uiDefault ToggleButton.toolbar.hoverBackground Color + * @uiDefault ToggleButton.toolbar.pressedBackground Color * @uiDefault ToggleButton.toolbar.selectedBackground Color * + * + * * @uiDefault ToggleButton.tab.underlineHeight int * @uiDefault ToggleButton.tab.underlineColor Color * @uiDefault ToggleButton.tab.disabledUnderlineColor Color @@ -75,12 +73,6 @@ import com.formdev.flatlaf.util.UIScale; public class FlatToggleButtonUI extends FlatButtonUI { - protected Color selectedBackground; - protected Color selectedForeground; - protected Color disabledSelectedBackground; - - protected Color toolbarSelectedBackground; - protected int tabUnderlineHeight; protected Color tabUnderlineColor; protected Color tabDisabledUnderlineColor; @@ -108,12 +100,6 @@ public class FlatToggleButtonUI super.installDefaults( b ); if( !defaults_initialized ) { - selectedBackground = UIManager.getColor( "ToggleButton.selectedBackground" ); - selectedForeground = UIManager.getColor( "ToggleButton.selectedForeground" ); - disabledSelectedBackground = UIManager.getColor( "ToggleButton.disabledSelectedBackground" ); - - toolbarSelectedBackground = UIManager.getColor( "ToggleButton.toolbar.selectedBackground" ); - tabUnderlineHeight = UIManager.getInt( "ToggleButton.tab.underlineHeight" ); tabUnderlineColor = UIManager.getColor( "ToggleButton.tab.underlineColor" ); tabDisabledUnderlineColor = UIManager.getColor( "ToggleButton.tab.disabledUnderlineColor" ); @@ -185,30 +171,4 @@ public class FlatToggleButtonUI } else super.paintBackground( g, c ); } - - @Override - protected Color getBackground( JComponent c ) { - ButtonModel model = ((AbstractButton)c).getModel(); - - if( model.isSelected() ) { - // in toolbar use same colors for disabled and enabled because - // we assume that toolbar icon is shown disabled - boolean toolBarButton = isToolBarButton( c ); - return buttonStateColor( c, - toolBarButton ? toolbarSelectedBackground : selectedBackground, - toolBarButton ? toolbarSelectedBackground : disabledSelectedBackground, - null, null, - toolBarButton ? toolbarPressedBackground : pressedBackground ); - } - - return super.getBackground( c ); - } - - @Override - protected Color getForeground( JComponent c ) { - if( c.isEnabled() && ((AbstractButton)c).isSelected() && !isToolBarButton( c ) ) - return selectedForeground; - - return super.getForeground( c ); - } } 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 698901a1..fd49e4f0 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -74,6 +74,9 @@ controlDkShadow=lighten($controlShadow,10%) Button.background=#4c5052 Button.hoverBackground=lighten($Button.background,3%,derived) Button.pressedBackground=lighten($Button.background,6%,derived) +Button.selectedBackground=lighten($Button.background,10%,derived) +Button.selectedForeground=@foreground +Button.disabledSelectedBackground=lighten($Button.background,3%,derived) Button.borderColor=#5e6060 Button.disabledBorderColor=#5e6060 @@ -92,6 +95,7 @@ Button.default.boldText=true Button.toolbar.hoverBackground=lighten($Button.background,1%,derived) Button.toolbar.pressedBackground=lighten($Button.background,4%,derived) +Button.toolbar.selectedBackground=lighten($Button.background,7%,derived) #---- CheckBox ---- 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 996d652e..3ce068d5 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -75,6 +75,9 @@ Button.background=#ffffff Button.focusedBackground=#e3f1fa Button.hoverBackground=darken($Button.background,3%,derived) Button.pressedBackground=darken($Button.background,10%,derived) +Button.selectedBackground=darken($Button.background,20%,derived) +Button.selectedForeground=@foreground +Button.disabledSelectedBackground=darken($Button.background,13%,derived) Button.borderColor=$Component.borderColor Button.disabledBorderColor=$Component.disabledBorderColor @@ -94,6 +97,7 @@ Button.default.borderWidth=2 Button.toolbar.hoverBackground=darken($Button.background,12%,derived) Button.toolbar.pressedBackground=darken($Button.background,15%,derived) +Button.toolbar.selectedBackground=$Button.selectedBackground #---- CheckBox ---- diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt index 4e0ea56d..05c66959 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt @@ -80,6 +80,7 @@ Button.default.pressedBackground #3f6796 com.formdev.flatlaf.util.DerivedColo Button.defaultButtonFollowsFocus true Button.disabledBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] Button.disabledBorderColor #5e6060 javax.swing.plaf.ColorUIResource [UI] +Button.disabledSelectedBackground #53585a com.formdev.flatlaf.util.DerivedColor [UI] lighten(3% autoInverse) Button.disabledText #777777 javax.swing.plaf.ColorUIResource [UI] Button.focusedBorderColor #466d94 javax.swing.plaf.ColorUIResource [UI] Button.font [active] $defaultFont [UI] @@ -93,12 +94,15 @@ Button.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [U Button.minimumWidth 72 Button.pressedBackground #5b5f62 com.formdev.flatlaf.util.DerivedColor [UI] lighten(6% autoInverse) Button.rollover true +Button.selectedBackground #656a6c com.formdev.flatlaf.util.DerivedColor [UI] lighten(10% autoInverse) +Button.selectedForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI] Button.shadow #646464 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 Button.toolbar.hoverBackground #4e5355 com.formdev.flatlaf.util.DerivedColor [UI] lighten(1% autoInverse) Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] Button.toolbar.pressedBackground #565a5d com.formdev.flatlaf.util.DerivedColor [UI] lighten(4% autoInverse) +Button.toolbar.selectedBackground #5d6265 com.formdev.flatlaf.util.DerivedColor [UI] lighten(7% autoInverse) Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt index 6e90d26e..c7dade83 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt @@ -80,6 +80,7 @@ Button.default.pressedBackground #e6e6e6 com.formdev.flatlaf.util.DerivedColo Button.defaultButtonFollowsFocus true Button.disabledBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] Button.disabledBorderColor #cfcfcf javax.swing.plaf.ColorUIResource [UI] +Button.disabledSelectedBackground #dedede com.formdev.flatlaf.util.DerivedColor [UI] darken(13% autoInverse) Button.disabledText #8c8c8c javax.swing.plaf.ColorUIResource [UI] Button.focusedBackground #e3f1fa javax.swing.plaf.ColorUIResource [UI] Button.focusedBorderColor #87afda javax.swing.plaf.ColorUIResource [UI] @@ -94,12 +95,15 @@ Button.margin 2,14,2,14 javax.swing.plaf.InsetsUIResource [U Button.minimumWidth 72 Button.pressedBackground #e6e6e6 com.formdev.flatlaf.util.DerivedColor [UI] darken(10% autoInverse) Button.rollover true +Button.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] darken(20% autoInverse) +Button.selectedForeground #000000 javax.swing.plaf.ColorUIResource [UI] Button.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI] Button.textIconGap 4 Button.textShiftOffset 0 Button.toolbar.hoverBackground #e0e0e0 com.formdev.flatlaf.util.DerivedColor [UI] darken(12% autoInverse) Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] Button.toolbar.pressedBackground #d9d9d9 com.formdev.flatlaf.util.DerivedColor [UI] darken(15% autoInverse) +Button.toolbar.selectedBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] darken(20% autoInverse) Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI 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 c5ff3e84..dcf47d78 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 @@ -28,6 +28,7 @@ Button.default.pressedBackground Button.defaultButtonFollowsFocus Button.disabledBackground Button.disabledBorderColor +Button.disabledSelectedBackground Button.disabledText Button.focusInputMap Button.focusedBackground @@ -43,12 +44,15 @@ Button.margin Button.minimumWidth Button.pressedBackground Button.rollover +Button.selectedBackground +Button.selectedForeground Button.shadow Button.textIconGap Button.textShiftOffset Button.toolbar.hoverBackground Button.toolbar.margin Button.toolbar.pressedBackground +Button.toolbar.selectedBackground Button.toolbar.spacingInsets ButtonUI Caret.width