From 7b11e291228b6f582d486fe4e75353f584826f1e Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 29 Jun 2020 10:49:07 +0200 Subject: [PATCH] Button and ToggleButton: support making buttons square (issue #118) --- CHANGELOG.md | 2 ++ .../formdev/flatlaf/FlatClientProperties.java | 8 ++++++++ .../com/formdev/flatlaf/ui/FlatButtonUI.java | 18 +++++++++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3991bea9..baace827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ FlatLaf Change Log - Button and ToggleButton: Support disabled background color (use UI values `Button.disabledBackground` and `ToggleButton.disabledBackground`). (issue #112) +- Button and ToggleButton: Support making buttons square (set client property + `JButton.squareSize` to `true`). (issue #118) - ScrollBar: Support pressed track, thumb and button colors (use UI values `ScrollBar.pressedTrackColor`, `ScrollBar.pressedThumbColor` and `ScrollBar.pressedButtonBackground`). (issue #115) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java index ae53d724..e02abedc 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java @@ -96,6 +96,14 @@ public interface FlatClientProperties */ String SELECTED_STATE_INDETERMINATE = "indeterminate"; + /** + * Specifies whether the button preferred size will be made square (quadratically). + *

+ * Components {@link javax.swing.JButton} and {@link javax.swing.JToggleButton} + * Value type {@link java.lang.Boolean} + */ + String SQUARE_SIZE = "JButton.squareSize"; + /** * Specifies minimum width of a component. *

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 d81358fb..d8aa33a5 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 @@ -213,6 +213,7 @@ public class FlatButtonUI protected void propertyChange( AbstractButton b, PropertyChangeEvent e ) { switch( e.getPropertyName() ) { + case SQUARE_SIZE: case MINIMUM_WIDTH: case MINIMUM_HEIGHT: b.revalidate(); @@ -457,13 +458,16 @@ public class FlatButtonUI if( prefSize == null ) return null; - // make button square if it is a single-character button - // or apply minimum width, if not in toolbar and not a icon-only or single-character button - if( isIconOnlyOrSingleCharacterButton( c ) ) { - // make only single-character buttons square to allow non-square icon-only buttons - if( ((AbstractButton)c).getIcon() == null ) - prefSize.width = Math.max( prefSize.width, prefSize.height ); - } else if( !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) { + // make square or apply minimum width/height + boolean isIconOnlyOrSingleCharacter = isIconOnlyOrSingleCharacterButton( c ); + if( clientPropertyBoolean( c, SQUARE_SIZE, false ) ) { + // make button square (increase width or height so that they are equal) + prefSize.width = prefSize.height = Math.max( prefSize.width, prefSize.height ); + } else if( isIconOnlyOrSingleCharacter && ((AbstractButton)c).getIcon() == null ) { + // make single-character-no-icon button square (increase width) + prefSize.width = Math.max( prefSize.width, prefSize.height ); + } else if( !isIconOnlyOrSingleCharacter && !isToolBarButton( c ) && c.getBorder() instanceof FlatButtonBorder ) { + // apply minimum width/height float focusWidth = FlatUIUtils.getBorderFocusWidth( c ); prefSize.width = Math.max( prefSize.width, scale( FlatUIUtils.minimumWidth( c, minimumWidth ) ) + Math.round( focusWidth * 2 ) ); prefSize.height = Math.max( prefSize.height, scale( FlatUIUtils.minimumHeight( c, 0 ) ) + Math.round( focusWidth * 2 ) );