Spinner: support Spinner.focusedBackground

ComboBox:
- prefer explicit set background color over focusedBackground
- if ComboBox.buttonFocusedBackground is not specified use ComboBox.focusedBackground
- added ComboBox.popupFocusedBackground

(issue #335)
This commit is contained in:
Karl Tauber
2021-06-15 11:50:30 +02:00
parent 7e0915cb9c
commit ed9cb0f918
4 changed files with 76 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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