ToolBar: added empty space around buttons in toolbar (issue #56)

This commit is contained in:
Karl Tauber
2020-02-14 12:53:30 +01:00
parent 7f9f22df3e
commit c83b4093f0
6 changed files with 41 additions and 10 deletions

View File

@@ -17,6 +17,8 @@ FlatLaf Change Log
is "..." or a single character. is "..." or a single character.
- ToolBar: No longer use special rollover border for buttons in toolbar. (issue - ToolBar: No longer use special rollover border for buttons in toolbar. (issue
#36) #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 - Fixed "illegal reflective access operation" warning on macOS when using Java
12 or later. (issue #60, PR #61) 12 or later. (issue #60, PR #61)

View File

@@ -44,6 +44,8 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Button.default.focusedBorderColor Color * @uiDefault Button.default.focusedBorderColor Color
* @uiDefault Button.default.focusColor Color * @uiDefault Button.default.focusColor Color
* @uiDefault Button.default.borderWidth int * @uiDefault Button.default.borderWidth int
* @uiDefault Button.toolbar.margin Insets
* @uiDefault Button.toolbar.spacingInsets Insets
* @uiDefault Button.arc int * @uiDefault Button.arc int
* *
* @author Karl Tauber * @author Karl Tauber
@@ -63,6 +65,7 @@ public class FlatButtonBorder
protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" ); protected final Color defaultFocusColor = UIManager.getColor( "Button.default.focusColor" );
protected final int defaultBorderWidth = UIManager.getInt( "Button.default.borderWidth" ); protected final int defaultBorderWidth = UIManager.getInt( "Button.default.borderWidth" );
protected final Insets toolbarMargin = UIManager.getInsets( "Button.toolbar.margin" ); 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" ); protected final int arc = UIManager.getInt( "Button.arc" );
@Override @Override
@@ -107,8 +110,8 @@ public class FlatButtonBorder
? ((AbstractButton)c).getMargin() ? ((AbstractButton)c).getMargin()
: null; : null;
FlatUIUtils.setInsets( insets, UIScale.scale( FlatUIUtils.setInsets( insets, UIScale.scale( FlatUIUtils.addInsets( toolbarSpacingInsets,
(margin != null && !(margin instanceof UIResource)) ? margin : toolbarMargin ) ); (margin != null && !(margin instanceof UIResource)) ? margin : toolbarMargin ) ) );
} else { } else {
insets = super.getBorderInsets( c, insets ); insets = super.getBorderInsets( c, insets );

View File

@@ -26,6 +26,7 @@ import java.awt.FontMetrics;
import java.awt.GradientPaint; import java.awt.GradientPaint;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
@@ -82,6 +83,7 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Button.shadowWidth int default is 2 * @uiDefault Button.shadowWidth int default is 2
* @uiDefault Button.shadowColor Color optional * @uiDefault Button.shadowColor Color optional
* @uiDefault Button.default.shadowColor Color optional * @uiDefault Button.default.shadowColor Color optional
* @uiDefault Button.toolbar.spacingInsets Insets
* @uiDefault Button.toolbar.hoverBackground Color * @uiDefault Button.toolbar.hoverBackground Color
* @uiDefault Button.toolbar.pressedBackground Color * @uiDefault Button.toolbar.pressedBackground Color
* *
@@ -114,6 +116,7 @@ public class FlatButtonUI
protected Color shadowColor; protected Color shadowColor;
protected Color defaultShadowColor; protected Color defaultShadowColor;
protected Insets toolbarSpacingInsets;
protected Color toolbarHoverBackground; protected Color toolbarHoverBackground;
protected Color toolbarPressedBackground; protected Color toolbarPressedBackground;
@@ -166,6 +169,7 @@ public class FlatButtonUI
defaultPressedBackground = UIManager.getColor( "Button.default.pressedBackground" ); defaultPressedBackground = UIManager.getColor( "Button.default.pressedBackground" );
defaultBoldText = UIManager.getBoolean( "Button.default.boldText" ); defaultBoldText = UIManager.getBoolean( "Button.default.boldText" );
toolbarSpacingInsets = UIManager.getInsets( "Button.toolbar.spacingInsets" );
toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" ); toolbarHoverBackground = UIManager.getColor( prefix + "toolbar.hoverBackground" );
toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" ); toolbarPressedBackground = UIManager.getColor( prefix + "toolbar.pressedBackground" );
@@ -269,27 +273,42 @@ public class FlatButtonUI
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
Border border = c.getBorder(); Border border = c.getBorder();
float focusWidth = (border instanceof FlatBorder && !isToolBarButton( c )) ? scale( (float) getFocusWidth( c ) ) : 0; boolean isToolBarButton = isToolBarButton( c );
float arc = ((border instanceof FlatButtonBorder && !isSquareButton( c )) || 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; ? scale( (float) this.arc ) : 0;
boolean def = isDefaultButton( c ); 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 // paint shadow
Color shadowColor = def ? defaultShadowColor : this.shadowColor; 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.setColor( shadowColor );
g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ), 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 // paint background
Color startBg = def ? defaultBackground : startBackground; Color startBg = def ? defaultBackground : startBackground;
Color endBg = def ? defaultEndBackground : endBackground; Color endBg = def ? defaultEndBackground : endBackground;
if( background == startBg && endBg != null && !startBg.equals( endBg ) ) 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 else
FlatUIUtils.setColor( g2, background, def ? defaultBackground : c.getBackground() ); 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 { } finally {
g2.dispose(); g2.dispose();
} }

View File

@@ -104,6 +104,7 @@ Button.defaultButtonFollowsFocus=false
Button.default.borderWidth=1 Button.default.borderWidth=1
Button.toolbar.margin=3,3,3,3 Button.toolbar.margin=3,3,3,3
Button.toolbar.spacingInsets=1,2,1,2
#---- Caret ---- #---- Caret ----
@@ -513,6 +514,8 @@ ToolBar.separatorSize=null
ToolBar.separatorWidth=7 ToolBar.separatorWidth=7
ToolBar.separatorColor=$Separator.foreground ToolBar.separatorColor=$Separator.foreground
ToolBar.spacingBorder=$Button.toolbar.spacingInsets
#---- ToolTip ---- #---- ToolTip ----

View File

@@ -114,7 +114,9 @@ Button.textIconGap 4
Button.textShiftOffset 0 Button.textShiftOffset 0
Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI] Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI]
Button.toolbar.hoverBackground #4c5052 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.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 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.background #3c3f41 javax.swing.plaf.ColorUIResource [UI]
ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI] ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI]
ToolBar.borderColor #cccccc javax.swing.plaf.ColorUIResource [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.darkShadow #7a8a99 javax.swing.plaf.ColorUIResource [UI]
ToolBar.dockingBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingBackground #3c3f41 javax.swing.plaf.ColorUIResource [UI]
ToolBar.dockingForeground #6382bf 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.separatorColor #515151 javax.swing.plaf.ColorUIResource [UI]
ToolBar.separatorWidth 7 ToolBar.separatorWidth 7
ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI] ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI]
ToolBar.spacingBorder [lazy] 1,2,1,2 false com.formdev.flatlaf.ui.FlatEmptyBorder [UI]
#---- ToolBarSeparator ---- #---- ToolBarSeparator ----

View File

@@ -115,7 +115,9 @@ Button.textIconGap 4
Button.textShiftOffset 0 Button.textShiftOffset 0
Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI] Button.toolBarBorderBackground #999999 javax.swing.plaf.ColorUIResource [UI]
Button.toolbar.hoverBackground #dfdfdf 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.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 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.background #f2f2f2 javax.swing.plaf.ColorUIResource [UI]
ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI] ToolBar.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatToolBarBorder [UI]
ToolBar.borderColor #cccccc javax.swing.plaf.ColorUIResource [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.darkShadow #7a8a99 javax.swing.plaf.ColorUIResource [UI]
ToolBar.dockingBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI] ToolBar.dockingBackground #f2f2f2 javax.swing.plaf.ColorUIResource [UI]
ToolBar.dockingForeground #6382bf 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.separatorColor #d1d1d1 javax.swing.plaf.ColorUIResource [UI]
ToolBar.separatorWidth 7 ToolBar.separatorWidth 7
ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI] ToolBar.shadow #b8cfe5 javax.swing.plaf.ColorUIResource [UI]
ToolBar.spacingBorder [lazy] 1,2,1,2 false com.formdev.flatlaf.ui.FlatEmptyBorder [UI]
#---- ToolBarSeparator ---- #---- ToolBarSeparator ----