From cd8f9692914c80d87be20382ee6d6027f741e8e7 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 4 Sep 2019 23:44:07 +0200 Subject: [PATCH] Button: support for buttons in toolbars --- .../com/formdev/flatlaf/ui/FlatButtonUI.java | 28 ++++++++- .../com/formdev/flatlaf/ui/FlatToolBarUI.java | 57 ------------------- .../com/formdev/flatlaf/ui/FlatUIUtils.java | 5 ++ .../formdev/flatlaf/FlatDarkLaf.properties | 3 + .../formdev/flatlaf/FlatLightLaf.properties | 3 + .../formdev/flatlaf/FlatTestLaf.properties | 3 + 6 files changed, 39 insertions(+), 60 deletions(-) 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 41a5fb22..70b08af9 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 @@ -24,9 +24,11 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.AbstractButton; +import javax.swing.ButtonModel; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.UIManager; +import javax.swing.border.Border; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicButtonUI; @@ -40,6 +42,8 @@ import javax.swing.plaf.basic.BasicButtonUI; * @uiDefault Button.disabledText Color * @uiDefault Button.default.background Color * @uiDefault Button.default.foreground Color + * @uiDefault Button.toolbar.hoverBackground Color + * @uiDefault Button.toolbar.pressedBackground Color * * @author Karl Tauber */ @@ -52,6 +56,8 @@ public class FlatButtonUI protected Color disabledText; protected Color defaultBackground; protected Color defaultForeground; + protected Color toolbarHoverBackground; + protected Color toolbarPressedBackground; private static ComponentUI instance; @@ -71,6 +77,8 @@ public class FlatButtonUI disabledText = UIManager.getColor( "Button.disabledText" ); defaultBackground = UIManager.getColor( "Button.default.background" ); defaultForeground = UIManager.getColor( "Button.default.foreground" ); + toolbarHoverBackground = UIManager.getColor( "Button.toolbar.hoverBackground" ); + toolbarPressedBackground = UIManager.getColor( "Button.toolbar.pressedBackground" ); } static boolean isContentAreaFilled( Component c ) { @@ -91,8 +99,9 @@ public class FlatButtonUI try { FlatUIUtils.setRenderingHints( g2 ); - float focusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; - float arc = (c.getBorder() instanceof FlatButtonBorder) ? scale( (float) this.arc ) : 0; + Border border = c.getBorder(); + float focusWidth = (border instanceof FlatBorder) ? scale( (float) this.focusWidth ) : 0; + float arc = (border instanceof FlatButtonBorder || FlatUIUtils.isToolBarButton( c )) ? scale( (float) this.arc ) : 0; g2.setColor( getBackground( c ) ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc ); @@ -117,7 +126,20 @@ public class FlatButtonUI textRect.y + fm.getAscent() + getTextShiftOffset() ); } - private Color getBackground( Component c ) { + private Color getBackground( JComponent c ) { + ButtonModel model = ((AbstractButton)c).getModel(); + + // toolbar button + if( FlatUIUtils.isToolBarButton( c ) ) { + if( model.isPressed() ) + return toolbarPressedBackground; + if( model.isRollover() ) + return toolbarHoverBackground; + + // use background of toolbar + return c.getParent().getBackground(); + } + boolean def = isDefaultButton( c ); return def ? defaultBackground : c.getBackground(); } diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java index c35916e8..f68f50ce 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatToolBarUI.java @@ -17,11 +17,8 @@ package com.formdev.flatlaf.ui; import static com.formdev.flatlaf.util.UIScale.scale; -import java.awt.Color; import java.awt.Component; import java.awt.Insets; -import java.util.HashMap; -import java.util.Objects; import javax.swing.AbstractButton; import javax.swing.JComponent; import javax.swing.UIManager; @@ -44,10 +41,6 @@ public class FlatToolBarUI extends BasicToolBarUI { private Border rolloverBorder; - private final HashMap backgroundTable = new HashMap<>(); - - /** Cache non-UIResource button color. */ - private Color buttonBackground; public static ComponentUI createUI( JComponent c ) { return new FlatToolBarUI(); @@ -58,7 +51,6 @@ public class FlatToolBarUI super.uninstallUI( c ); rolloverBorder = null; - buttonBackground = null; } @Override @@ -82,55 +74,6 @@ public class FlatToolBarUI return rolloverBorder; } - @Override - protected void setBorderToRollover( Component c ) { - super.setBorderToRollover( c ); - setToRollover( c ); - } - - @Override - protected void setBorderToNonRollover( Component c ) { - super.setBorderToNonRollover( c ); - setToRollover( c ); - } - - @Override - protected void setBorderToNormal( Component c ) { - super.setBorderToNormal( c ); - setToNormal( c ); - } - - private void setToRollover( Component c ) { - if( c instanceof AbstractButton ) { - AbstractButton b = (AbstractButton) c; - - Color background = backgroundTable.get( b ); - if( background == null || background instanceof UIResource ) - backgroundTable.put( b, b.getBackground() ); - - if( b.getBackground() instanceof UIResource ) - b.setBackground( getButtonBackground() ); - } - } - - private void setToNormal( Component c ) { - if( c instanceof AbstractButton ) { - AbstractButton b = (AbstractButton) c; - - Color background = backgroundTable.remove( b ); - b.setBackground( background ); - } - } - - private Color getButtonBackground() { - // use toolbar background as button background - // must be not an instance of UIResource - Color toolBarBackground = toolBar.getBackground(); - if( !Objects.equals( buttonBackground, toolBarBackground ) ) - buttonBackground = FlatUIUtils.nonUIResource( toolBarBackground ); - return buttonBackground; - } - //---- class FlatRolloverMarginBorder ------------------------------------- /** diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index c65606e5..5fae4ccb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -26,6 +26,7 @@ import java.awt.RenderingHints; import java.awt.geom.Path2D; import java.awt.geom.RoundRectangle2D; import javax.swing.JComponent; +import javax.swing.JToolBar; import javax.swing.UIManager; import javax.swing.plaf.ColorUIResource; import com.formdev.flatlaf.util.JavaCompatibility; @@ -62,6 +63,10 @@ public class FlatUIUtils return (c instanceof ColorUIResource) ? new Color( c.getRGB(), true ) : c; } + public static boolean isToolBarButton( JComponent c ) { + return c.getParent() instanceof JToolBar; + } + /** * Sets rendering hints used for painting. */ 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 7abfa360..e7fcd0d1 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 @@ Button.default.endBorderColor=4c708c Button.default.focusedBorderColor=537699 Button.default.focusColor=43688c +Button.toolbar.hoverBackground=4c5052 +Button.toolbar.pressedBackground=5c6164 + #---- 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 1ec439d6..b7419d03 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -74,6 +74,9 @@ Button.default.endBorderColor=3167ad Button.default.focusedBorderColor=a8cef6 Button.default.focusColor=97c3f3 +Button.toolbar.hoverBackground=dfdfdf +Button.toolbar.pressedBackground=cfcfcf + #---- CheckBox ---- 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 21cec0b6..148f8d9e 100644 --- a/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties +++ b/flatlaf-core/src/test/resources/com/formdev/flatlaf/FlatTestLaf.properties @@ -58,6 +58,9 @@ Button.default.endBorderColor=ff0000 Button.default.focusedBorderColor=537699 Button.default.focusColor=ff0000 +Button.toolbar.hoverBackground=ffffff +Button.toolbar.pressedBackground=eeeeee + #---- CheckBox ----