From 1ecafa5f2d6a2ebdcc2830260839d4fc817697ac Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 11 Sep 2019 10:09:05 +0200 Subject: [PATCH] Button: apply minimum button width of 72px --- .../com/formdev/flatlaf/ui/FlatButtonUI.java | 17 ++++++++++++++--- .../formdev/flatlaf/ui/FlatToggleButtonUI.java | 4 ++-- .../com/formdev/flatlaf/ui/FlatUIUtils.java | 5 ----- .../com/formdev/flatlaf/FlatLaf.properties | 3 ++- 4 files changed, 18 insertions(+), 11 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 7e2d2c89..801827d6 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 @@ -30,6 +30,7 @@ import javax.swing.ButtonModel; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JComponent; +import javax.swing.JToolBar; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.plaf.ComponentUI; @@ -42,6 +43,7 @@ import javax.swing.plaf.basic.BasicButtonUI; * * @uiDefault Component.focusWidth int * @uiDefault Button.arc int + * @uiDefault Button.minimumWidth int * @uiDefault Button.disabledText Color * @uiDefault Button.default.background Color * @uiDefault Button.default.foreground Color @@ -55,6 +57,7 @@ public class FlatButtonUI { protected int focusWidth; protected int arc; + protected int minimumWidth; protected Color disabledText; protected Color defaultBackground; @@ -80,6 +83,7 @@ public class FlatButtonUI focusWidth = UIManager.getInt( "Component.focusWidth" ); arc = UIManager.getInt( prefix + "arc" ); + minimumWidth = UIManager.getInt( prefix + "minimumWidth" ); disabledText = UIManager.getColor( prefix + "disabledText" ); defaultBackground = UIManager.getColor( prefix + "default.background" ); @@ -102,6 +106,10 @@ public class FlatButtonUI return c instanceof JButton && clientPropertyEquals( (JButton) c, BUTTON_TYPE, BUTTON_TYPE_HELP ); } + static boolean isToolBarButton( JComponent c ) { + return c.getParent() instanceof JToolBar; + } + @Override public void update( Graphics g, JComponent c ) { if( isHelpButton( c ) ) { @@ -121,7 +129,7 @@ public class FlatButtonUI 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; + float arc = (border instanceof FlatButtonBorder || isToolBarButton( c )) ? scale( (float) this.arc ) : 0; g2.setColor( background ); FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), focusWidth, arc ); @@ -156,7 +164,7 @@ public class FlatButtonUI ButtonModel model = ((AbstractButton)c).getModel(); // toolbar button - if( FlatUIUtils.isToolBarButton( c ) ) { + if( isToolBarButton( c ) ) { if( model.isPressed() ) return toolbarPressedBackground; if( model.isRollover() ) @@ -180,6 +188,9 @@ public class FlatButtonUI if( isHelpButton( c ) ) return new Dimension( helpButtonIcon.getIconWidth(), helpButtonIcon.getIconHeight() ); - return super.getPreferredSize( c ); + Dimension prefSize = super.getPreferredSize( c ); + if( !isToolBarButton( c ) ) + prefSize.width = Math.max( prefSize.width, scale( minimumWidth + (focusWidth * 2) ) ); + return prefSize; } } 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 0fb3c09a..1c493001 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 @@ -72,7 +72,7 @@ public class FlatToggleButtonUI ButtonModel model = ((AbstractButton)c).getModel(); if( model.isSelected() ) { - return FlatUIUtils.isToolBarButton( c ) + return isToolBarButton( c ) ? toolbarPressedBackground : (c.isEnabled() ? selectedBackground : disabledSelectedBackground); } @@ -84,7 +84,7 @@ public class FlatToggleButtonUI protected Color getForeground( JComponent c ) { ButtonModel model = ((AbstractButton)c).getModel(); - if( model.isSelected() && !FlatUIUtils.isToolBarButton( c ) ) + if( model.isSelected() && !isToolBarButton( c ) ) return selectedForeground; return super.getForeground( c ); 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 06ee969f..4c555745 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,7 +26,6 @@ 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; @@ -63,10 +62,6 @@ 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/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 5d1e4606..21d2aa00 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -64,6 +64,7 @@ ViewportUI=com.formdev.flatlaf.ui.FlatViewportUI Button.border=com.formdev.flatlaf.ui.FlatButtonBorder Button.arc=6 +Button.minimumWidth=72 #---- CheckBox ---- @@ -189,7 +190,7 @@ OptionPane.maxCharactersPerLine=80 OptionPane.iconMessageGap=16 OptionPane.messagePadding=3 OptionPane.buttonPadding=8 -OptionPane.buttonMinimumWidth=80 +OptionPane.buttonMinimumWidth=72 OptionPane.sameSizeButtons=true OptionPane.setButtonMargin=false OptionPane.buttonOrientation=4