From 27d4b5eba717d535a8c464e622122b9886c9af8f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 15 Jan 2020 11:05:16 +0100 Subject: [PATCH] ToggleButton: Support per component styling for tab-style toggle buttons with client properties `JToggleButton.tab.underlineHeight` (integer), `JToggleButton.tab.underlineColor` (Color) and `JToggleButton.tab.selectedBackground` (Color) (issue #45) --- CHANGELOG.md | 4 +++ .../formdev/flatlaf/FlatClientProperties.java | 34 +++++++++++++++++++ .../flatlaf/ui/FlatToggleButtonUI.java | 14 ++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9bef8e1..41dcd972 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ FlatLaf Change Log changed (is no longer an instance of `FlatButtonBorder`). - ToggleButton: Renamed toggle button type "underline" to "tab" (value of client property `JButton.buttonType` is now `tab`). +- ToggleButton: Support per component styling for tab-style toggle buttons with + client properties `JToggleButton.tab.underlineHeight` (integer), + `JToggleButton.tab.underlineColor` (Color) and + `JToggleButton.tab.selectedBackground` (Color). (issue #45) - ToggleButton: No longer use focus width for tab-style toggle buttons to compute component size, which reduces/fixes component size in "Flat IntelliJ" and "Flat Darcula" themes. 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 be2707d6..016b048b 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java @@ -16,6 +16,7 @@ package com.formdev.flatlaf; +import java.awt.Color; import java.util.Objects; import javax.swing.JComponent; @@ -124,6 +125,30 @@ public interface FlatClientProperties */ String PLACEHOLDER_TEXT = "JTextField.placeholderText"; + /** + * Height of underline if toggle button type is {@link #BUTTON_TYPE_TAB}. + *

+ * Component {@link javax.swing.JToggleButton}
+ * Value type {@link java.lang.Integer} + */ + String TAB_BUTTON_UNDERLINE_HEIGHT = "JToggleButton.tab.underlineHeight"; + + /** + * Color of underline if toggle button type is {@link #BUTTON_TYPE_TAB}. + *

+ * Component {@link javax.swing.JToggleButton}
+ * Value type {@link java.awt.Color} + */ + String TAB_BUTTON_UNDERLINE_COLOR = "JToggleButton.tab.underlineColor"; + + /** + * Background color if selected and toggle button type is {@link #BUTTON_TYPE_TAB}. + *

+ * Component {@link javax.swing.JToggleButton}
+ * Value type {@link java.awt.Color} + */ + String TAB_BUTTON_SELECTED_BACKGROUND = "JToggleButton.tab.selectedBackground"; + /** * Checks whether a client property of a component has the given value. */ @@ -148,4 +173,13 @@ public interface FlatClientProperties Object value = c.getClientProperty( key ); return (value instanceof Integer) ? (int) value : defaultValue; } + + /** + * Checks whether a client property of a component is a color and returns its value. + * If the client property is not set, or not a color, defaultValue is returned. + */ + static Color clientPropertyColor( JComponent c, String key, Color defaultValue ) { + Object value = c.getClientProperty( key ); + return (value instanceof Color) ? (Color) value : defaultValue; + } } 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 2c232de3..c664a7dd 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 @@ -144,6 +144,12 @@ public class FlatToggleButtonUI b.repaint(); break; + + case TAB_BUTTON_UNDERLINE_HEIGHT: + case TAB_BUTTON_UNDERLINE_COLOR: + case TAB_BUTTON_SELECTED_BACKGROUND: + b.repaint(); + break; } } @@ -160,7 +166,7 @@ public class FlatToggleButtonUI // paint background Color background = buttonStateColor( c, - selected ? tabSelectedBackground : null, + selected ? clientPropertyColor( c, TAB_BUTTON_SELECTED_BACKGROUND, tabSelectedBackground ) : null, null, tabFocusBackground, tabHoverBackground, null ); if( background != null ) { g.setColor( background ); @@ -169,8 +175,10 @@ public class FlatToggleButtonUI // paint underline if selected if( selected ) { - int underlineHeight = UIScale.scale( tabUnderlineHeight ); - g.setColor( c.isEnabled() ? tabUnderlineColor : tabDisabledUnderlineColor ); + int underlineHeight = UIScale.scale( clientPropertyInt( c, TAB_BUTTON_UNDERLINE_HEIGHT, tabUnderlineHeight ) ); + g.setColor( c.isEnabled() + ? clientPropertyColor( c, TAB_BUTTON_UNDERLINE_COLOR, tabUnderlineColor ) + : tabDisabledUnderlineColor ); g.fillRect( 0, height - underlineHeight, width, underlineHeight ); } } else