diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cea8f65..2ed6b45d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ FlatLaf Change Log ================== +## 1.3-SNAPSHOT + +#### New features and improvements + +- TextComponents, ComboBox and Spinner: Support different background color when + component is focused (use UI values `TextField.focusedBackground`, + `PasswordField.focusedBackground`, `FormattedTextField.focusedBackground`, + `TextArea.focusedBackground`, `TextPane.focusedBackground`, + `EditorPane.focusedBackground`, `ComboBox.focusedBackground`, + `ComboBox.buttonFocusedBackground`, `ComboBox.popupFocusedBackground` and + `Spinner.focusedBackground`). (issue #335) + + ## 1.2 #### New features and improvements diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index 32de8ea0..1f585521 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -61,6 +61,7 @@ import javax.swing.UIManager; import javax.swing.border.AbstractBorder; import javax.swing.border.Border; import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicComboBoxUI; import javax.swing.plaf.basic.BasicComboPopup; import javax.swing.plaf.basic.ComboPopup; @@ -97,11 +98,12 @@ import com.formdev.flatlaf.util.UIScale; * @uiDefault ComboBox.disabledForeground Color * @uiDefault ComboBox.buttonBackground Color * @uiDefault ComboBox.buttonEditableBackground Color - * @uiDefault ComboBox.buttonFocusedBackground Color optional + * @uiDefault ComboBox.buttonFocusedBackground Color optional; defaults to ComboBox.focusedBackground * @uiDefault ComboBox.buttonArrowColor Color * @uiDefault ComboBox.buttonDisabledArrowColor Color * @uiDefault ComboBox.buttonHoverArrowColor Color * @uiDefault ComboBox.buttonPressedArrowColor Color + * @uiDefault ComboBox.popupFocusedBackground Color optional * * @author Karl Tauber */ @@ -117,8 +119,8 @@ public class FlatComboBoxUI protected Color disabledBorderColor; protected Color editableBackground; - protected Color disabledBackground; protected Color focusedBackground; + protected Color disabledBackground; protected Color disabledForeground; protected Color buttonBackground; @@ -129,6 +131,8 @@ public class FlatComboBoxUI protected Color buttonHoverArrowColor; protected Color buttonPressedArrowColor; + protected Color popupFocusedBackground; + private MouseListener hoverListener; protected boolean hover; protected boolean pressed; @@ -199,8 +203,8 @@ public class FlatComboBoxUI disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" ); editableBackground = UIManager.getColor( "ComboBox.editableBackground" ); - disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" ); focusedBackground = UIManager.getColor( "ComboBox.focusedBackground" ); + disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" ); disabledForeground = UIManager.getColor( "ComboBox.disabledForeground" ); buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" ); @@ -211,6 +215,8 @@ public class FlatComboBoxUI buttonHoverArrowColor = UIManager.getColor( "ComboBox.buttonHoverArrowColor" ); buttonPressedArrowColor = UIManager.getColor( "ComboBox.buttonPressedArrowColor" ); + popupFocusedBackground = UIManager.getColor( "ComboBox.popupFocusedBackground" ); + // set maximumRowCount int maximumRowCount = UIManager.getInt( "ComboBox.maximumRowCount" ); if( maximumRowCount > 0 && maximumRowCount != 8 && comboBox.getMaximumRowCount() == 8 ) @@ -230,8 +236,8 @@ public class FlatComboBoxUI disabledBorderColor = null; editableBackground = null; - disabledBackground = null; focusedBackground = null; + disabledBackground = null; disabledForeground = null; buttonBackground = null; @@ -242,6 +248,8 @@ public class FlatComboBoxUI buttonHoverArrowColor = null; buttonPressedArrowColor = null; + popupFocusedBackground = null; + MigLayoutVisualPadding.uninstall( comboBox ); } @@ -431,7 +439,11 @@ public class FlatComboBoxUI // paint arrow button background if( enabled && !isCellRenderer ) { - g2.setColor( paintButton ? buttonEditableBackground : buttonFocusedBackground != null && isFocusOwner() ? buttonFocusedBackground : buttonBackground ); + g2.setColor( paintButton + ? buttonEditableBackground + : (buttonFocusedBackground != null || focusedBackground != null) && isPermanentFocusOwner( comboBox ) + ? (buttonFocusedBackground != null ? buttonFocusedBackground : focusedBackground) + : buttonBackground ); Shape oldClip = g2.getClip(); if( isLeftToRight ) g2.clipRect( arrowX, 0, width - arrowX, height ); @@ -491,15 +503,20 @@ public class FlatComboBoxUI } protected Color getBackground( boolean enabled ) { - return enabled - ? (focusedBackground != null && isFocusOwner() - ? focusedBackground - : (editableBackground != null && comboBox.isEditable() ? editableBackground : comboBox.getBackground()) ) - : (isIntelliJTheme ? FlatUIUtils.getParentBackground( comboBox ) : disabledBackground); - } - - protected boolean isFocusOwner() { - return FlatUIUtils.isPermanentFocusOwner( comboBox ) || comboBox.getEditor() != null && comboBox.getEditor().getEditorComponent() != null && FlatUIUtils.isPermanentFocusOwner( comboBox.getEditor().getEditorComponent() ); + if( enabled ) { + Color background = comboBox.getBackground(); + + // always use explicitly set color + if( !(background instanceof UIResource) ) + return background; + + // focused + if( focusedBackground != null && isPermanentFocusOwner( comboBox ) ) + return focusedBackground; + + return (editableBackground != null && comboBox.isEditable()) ? editableBackground : background; + } else + return isIntelliJTheme ? FlatUIUtils.getParentBackground( comboBox ) : disabledBackground; } protected Color getForeground( boolean enabled ) { @@ -710,9 +727,8 @@ public class FlatComboBoxUI super.configureList(); list.setCellRenderer( new PopupListCellRenderer() ); - if( focusedBackground != null ) { - list.setBackground( focusedBackground ); - } + if( popupFocusedBackground != null ) + list.setBackground( popupFocusedBackground ); } @Override diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java index b7d8e4c8..a341d0a9 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatSpinnerUI.java @@ -39,6 +39,7 @@ import javax.swing.LookAndFeel; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; +import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicSpinnerUI; import com.formdev.flatlaf.FlatClientProperties; @@ -65,6 +66,7 @@ import com.formdev.flatlaf.FlatClientProperties; * @uiDefault Component.disabledBorderColor Color * @uiDefault Spinner.disabledBackground Color * @uiDefault Spinner.disabledForeground Color + * @uiDefault Spinner.focusedBackground Color optional * @uiDefault Spinner.buttonBackground Color * @uiDefault Spinner.buttonArrowColor Color * @uiDefault Spinner.buttonDisabledArrowColor Color @@ -87,6 +89,7 @@ public class FlatSpinnerUI protected Color disabledBorderColor; protected Color disabledBackground; protected Color disabledForeground; + protected Color focusedBackground; protected Color buttonBackground; protected Color buttonArrowColor; protected Color buttonDisabledArrowColor; @@ -112,6 +115,7 @@ public class FlatSpinnerUI disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" ); disabledBackground = UIManager.getColor( "Spinner.disabledBackground" ); disabledForeground = UIManager.getColor( "Spinner.disabledForeground" ); + focusedBackground = UIManager.getColor( "Spinner.focusedBackground" ); buttonBackground = UIManager.getColor( "Spinner.buttonBackground" ); buttonArrowColor = UIManager.getColor( "Spinner.buttonArrowColor" ); buttonDisabledArrowColor = UIManager.getColor( "Spinner.buttonDisabledArrowColor" ); @@ -133,6 +137,7 @@ public class FlatSpinnerUI disabledBorderColor = null; disabledBackground = null; disabledForeground = null; + focusedBackground = null; buttonBackground = null; buttonArrowColor = null; buttonDisabledArrowColor = null; @@ -232,9 +237,20 @@ public class FlatSpinnerUI } protected Color getBackground( boolean enabled ) { - return enabled - ? spinner.getBackground() - : (isIntelliJTheme ? FlatUIUtils.getParentBackground( spinner ) : disabledBackground); + if( enabled ) { + Color background = spinner.getBackground(); + + // always use explicitly set color + if( !(background instanceof UIResource) ) + return background; + + // focused + if( focusedBackground != null && isPermanentFocusOwner( spinner ) ) + return focusedBackground; + + return background; + } else + return isIntelliJTheme ? FlatUIUtils.getParentBackground( spinner ) : disabledBackground; } protected Color getForeground( boolean enabled ) { @@ -415,6 +431,7 @@ public class FlatSpinnerUI @Override public void focusGained( FocusEvent e ) { + // necessary to update focus border spinner.repaint(); // if spinner gained focus, transfer it to the editor text field @@ -427,6 +444,7 @@ public class FlatSpinnerUI @Override public void focusLost( FocusEvent e ) { + // necessary to update focus border spinner.repaint(); } diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index 6af9434a..cc4d76a7 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -130,6 +130,10 @@ ComboBox.background = #fff ComboBox.buttonBackground = #f0f0f0 ComboBox.buttonEditableBackground = #ccc +ComboBox.focusedBackground = #ff8 +ComboBox.buttonFocusedBackground = #ff0 +ComboBox.popupFocusedBackground = #ffc + #---- Component ---- @@ -295,6 +299,11 @@ Slider.disabledTrackColor = #ff8 Slider.disabledThumbColor = #880 +#---- Spinner ---- + +Spinner.focusedBackground = #ff8 + + #---- SplitPane ---- SplitPaneDivider.draggingColor = #800