From c83b4093f0e50543fa791b6e4d58be68df635e45 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Fri, 14 Feb 2020 12:53:30 +0100 Subject: [PATCH] ToolBar: added empty space around buttons in toolbar (issue #56) --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/ui/FlatButtonBorder.java | 7 +++-- .../com/formdev/flatlaf/ui/FlatButtonUI.java | 31 +++++++++++++++---- .../com/formdev/flatlaf/FlatLaf.properties | 3 ++ .../uidefaults/FlatDarkLaf_1.8.0_202.txt | 4 ++- .../uidefaults/FlatLightLaf_1.8.0_202.txt | 4 ++- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fea29e4..4e89241b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ FlatLaf Change Log is "..." or a single character. - ToolBar: No longer use special rollover border for buttons in toolbar. (issue #36) +- ToolBar: Added empty space around buttons in toolbar (see UI default value + `Button.toolbar.spacingInsets`). (issue #56) - Fixed "illegal reflective access operation" warning on macOS when using Java 12 or later. (issue #60, PR #61) 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 18339fbc..df8dac7e 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 @@ -44,6 +44,8 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault Button.default.focusedBorderColor Color * @uiDefault Button.default.focusColor Color * @uiDefault Button.default.borderWidth int + * @uiDefault Button.toolbar.margin Insets + * @uiDefault Button.toolbar.spacingInsets Insets * @uiDefault Button.arc int * * @author Karl Tauber @@ -63,6 +65,7 @@ public class FlatButtonBorder protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" ); protected final int defaultBorderWidth = UIManager.getInt( "Button.default.borderWidth" ); protected final Insets toolbarMargin = UIManager.getInsets( "Button.toolbar.margin" ); + protected final Insets toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" ); protected final int arc = UIManager.getInt( "Button.arc" ); @Override @@ -107,8 +110,8 @@ public class FlatButtonBorder ? ((AbstractButton)c).getMargin() : null; - FlatUIUtils.setInsets( insets, UIScale.scale( - (margin != null && !(margin instanceof UIResource)) ? margin : toolbarMargin ) ); + FlatUIUtils.setInsets( insets, UIScale.scale( FlatUIUtils.addInsets( toolbarSpacingInsets, + (margin != null && !(margin instanceof UIResource)) ? margin : toolbarMargin ) ) ); } else { insets = super.getBorderInsets( c, insets ); 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 d10827da..c9152d76 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 @@ -26,6 +26,7 @@ import java.awt.FontMetrics; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Insets; import java.awt.Rectangle; import java.awt.geom.RoundRectangle2D; import java.beans.PropertyChangeEvent; @@ -82,6 +83,7 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault Button.shadowWidth int default is 2 * @uiDefault Button.shadowColor Color optional * @uiDefault Button.default.shadowColor Color optional + * @uiDefault Button.toolbar.spacingInsets Insets * @uiDefault Button.toolbar.hoverBackground Color * @uiDefault Button.toolbar.pressedBackground Color * @@ -114,6 +116,7 @@ public class FlatButtonUI protected Color shadowColor; protected Color defaultShadowColor; + protected Insets toolbarSpacingInsets; protected Color toolbarHoverBackground; protected Color toolbarPressedBackground; @@ -166,6 +169,7 @@ public class FlatButtonUI defaultPressedBackground = UIManager.getColor( "Button.default.pressedBackground" ); defaultBoldText = UIManager.getBoolean( "Button.default.boldText" ); + toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" ); toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" ); toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" ); @@ -269,27 +273,42 @@ public class FlatButtonUI FlatUIUtils.setRenderingHints( g2 ); Border border = c.getBorder(); - float focusWidth = (border instanceof FlatBorder && !isToolBarButton( c )) ? scale( (float) getFocusWidth( c ) ) : 0; - float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton( c )) + boolean isToolBarButton = isToolBarButton( c ); + float focusWidth = (border instanceof FlatBorder && !isToolBarButton) ? scale( (float) getFocusWidth( c ) ) : 0; + float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || isToolBarButton) ? scale( (float) this.arc ) : 0; boolean def = isDefaultButton( c ); + int x = 0; + int y = 0; + int width = c.getWidth(); + int height = c.getHeight(); + + if( isToolBarButton ) { + Insets spacing = UIScale.scale( toolbarSpacingInsets ); + x += spacing.left; + y += spacing.top; + width -= spacing.left + spacing.right; + height -= spacing.top + spacing.bottom; + } + // paint shadow Color shadowColor = def ? defaultShadowColor : this.shadowColor; - if( shadowColor != null && shadowWidth > 0 && focusWidth > 0 && !c.hasFocus() && c.isEnabled() ) { + if( !isToolBarButton && shadowColor != null && shadowWidth > 0 && focusWidth > 0 && !c.hasFocus() && c.isEnabled() ) { g2.setColor( shadowColor ); g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ), - c.getWidth() - focusWidth * 2, c.getHeight() - focusWidth * 2, arc, arc ) ); + width - focusWidth * 2, height - focusWidth * 2, arc, arc ) ); } // paint background Color startBg = def ? defaultBackground : startBackground; Color endBg = def ? defaultEndBackground : endBackground; if( background == startBg && endBg != null && !startBg.equals( endBg ) ) - g2.setPaint( new GradientPaint( 0, 0, startBg, 0, c.getHeight(), endBg ) ); + g2.setPaint( new GradientPaint( 0, 0, startBg, 0, height, endBg ) ); else FlatUIUtils.setColor( g2, background, def ? defaultBackground : c.getBackground() ); - FlatUIUtils.paintComponentBackground( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc ); + + FlatUIUtils.paintComponentBackground( g2, x, y, width, height, focusWidth, arc ); } finally { g2.dispose(); } 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 9f13ecce..2a509599 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -104,6 +104,7 @@ Button.defaultButtonFollowsFocus=false Button.default.borderWidth=1 Button.toolbar.margin=3,3,3,3 +Button.toolbar.spacingInsets=1,2,1,2 #---- Caret ---- @@ -513,6 +514,8 @@ ToolBar.separatorSize=null ToolBar.separatorWidth=7 ToolBar.separatorColor=$Separator.foreground +ToolBar.spacingBorder=$Button.toolbar.spacingInsets + #---- ToolTip ---- 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 fc83251d..3f18ee02 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 @@ -114,7 +114,9 @@ Button.textIconGap 4 Button.textShiftOffset 0 Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI] Button.toolbar.hoverBackground #4c5052 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] Button.toolbar.pressedBackground #555a5d javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1890,7 +1892,6 @@ ToolBar.ancestorInputMap [lazy] 8 javax.swing.plaf.InputMapUIResource [ ToolBar.background #3c3f41 javax.swing.plaf.ColorUIResource [UI] ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI] ToolBar.borderColor #cccccc javax.swing.plaf.ColorUIResource [UI] -ToolBar.buttonMargins 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] ToolBar.darkShadow #7a8a99 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingForeground #6382bf javax.swing.plaf.ColorUIResource [UI] @@ -1911,6 +1912,7 @@ ToolBar.rolloverBorder [lazy] javax.swing.border.CompoundBorder ToolBar.separatorColor #515151 javax.swing.plaf.ColorUIResource [UI] ToolBar.separatorWidth 7 ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI] +ToolBar.spacingBorder [lazy] 1,2,1,2 false com.formdev.flatlaf.ui.FlatEmptyBorder [UI] #---- ToolBarSeparator ---- 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 87d004dd..ae367fdb 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 @@ -115,7 +115,9 @@ Button.textIconGap 4 Button.textShiftOffset 0 Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI] Button.toolbar.hoverBackground #dfdfdf javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.margin 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] Button.toolbar.pressedBackground #d8d8d8 javax.swing.plaf.ColorUIResource [UI] +Button.toolbar.spacingInsets 1,2,1,2 javax.swing.plaf.InsetsUIResource [UI] ButtonUI com.formdev.flatlaf.ui.FlatButtonUI @@ -1892,7 +1894,6 @@ ToolBar.ancestorInputMap [lazy] 8 javax.swing.plaf.InputMapUIResource [ ToolBar.background #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI] ToolBar.borderColor #cccccc javax.swing.plaf.ColorUIResource [UI] -ToolBar.buttonMargins 3,3,3,3 javax.swing.plaf.InsetsUIResource [UI] ToolBar.darkShadow #7a8a99 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingForeground #6382bf javax.swing.plaf.ColorUIResource [UI] @@ -1913,6 +1914,7 @@ ToolBar.rolloverBorder [lazy] javax.swing.border.CompoundBorder ToolBar.separatorColor #d1d1d1 javax.swing.plaf.ColorUIResource [UI] ToolBar.separatorWidth 7 ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI] +ToolBar.spacingBorder [lazy] 1,2,1,2 false com.formdev.flatlaf.ui.FlatEmptyBorder [UI] #---- ToolBarSeparator ----