Issue #335: allow a different background on focus.

This commit is contained in:
Christopher Deckers
2021-06-08 10:12:59 +02:00
parent b26dbe81f4
commit 26250e790f
4 changed files with 38 additions and 6 deletions

View File

@@ -92,10 +92,12 @@ import com.formdev.flatlaf.util.UIScale;
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault ComboBox.editableBackground Color optional; defaults to ComboBox.background
* @uiDefault ComboBox.focusedBackground Color optional
* @uiDefault ComboBox.disabledBackground Color
* @uiDefault ComboBox.disabledForeground Color
* @uiDefault ComboBox.buttonBackground Color
* @uiDefault ComboBox.buttonEditableBackground Color
* @uiDefault ComboBox.buttonFocusedBackground Color optional
* @uiDefault ComboBox.buttonArrowColor Color
* @uiDefault ComboBox.buttonDisabledArrowColor Color
* @uiDefault ComboBox.buttonHoverArrowColor Color
@@ -116,10 +118,12 @@ public class FlatComboBoxUI
protected Color editableBackground;
protected Color disabledBackground;
protected Color focusedBackground;
protected Color disabledForeground;
protected Color buttonBackground;
protected Color buttonEditableBackground;
protected Color buttonFocusedBackground;
protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor;
@@ -196,9 +200,11 @@ public class FlatComboBoxUI
editableBackground = UIManager.getColor( "ComboBox.editableBackground" );
disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" );
focusedBackground = UIManager.getColor( "ComboBox.focusedBackground" );
disabledForeground = UIManager.getColor( "ComboBox.disabledForeground" );
buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" );
buttonFocusedBackground = UIManager.getColor( "ComboBox.buttonFocusedBackground" );
buttonEditableBackground = UIManager.getColor( "ComboBox.buttonEditableBackground" );
buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" );
@@ -225,10 +231,12 @@ public class FlatComboBoxUI
editableBackground = null;
disabledBackground = null;
focusedBackground = null;
disabledForeground = null;
buttonBackground = null;
buttonEditableBackground = null;
buttonFocusedBackground = null;
buttonArrowColor = null;
buttonDisabledArrowColor = null;
buttonHoverArrowColor = null;
@@ -423,7 +431,7 @@ public class FlatComboBoxUI
// paint arrow button background
if( enabled && !isCellRenderer ) {
g2.setColor( paintButton ? buttonEditableBackground : buttonBackground );
g2.setColor( paintButton ? buttonEditableBackground : buttonFocusedBackground != null && isFocusOwner() ? buttonFocusedBackground : buttonBackground );
Shape oldClip = g2.getClip();
if( isLeftToRight )
g2.clipRect( arrowX, 0, width - arrowX, height );
@@ -484,9 +492,15 @@ public class FlatComboBoxUI
protected Color getBackground( boolean enabled ) {
return enabled
? (editableBackground != null && comboBox.isEditable() ? editableBackground : comboBox.getBackground())
? (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() );
}
protected Color getForeground( boolean enabled ) {
return enabled ? comboBox.getForeground() : disabledForeground;

View File

@@ -50,6 +50,7 @@ import com.formdev.flatlaf.util.HiDPIUtils;
* @uiDefault PasswordField.disabledBackground Color used if not enabled
* @uiDefault PasswordField.inactiveBackground Color used if not editable
* @uiDefault PasswordField.inactiveForeground Color used if not enabled (yes, this is confusing; this should be named disabledForeground)
* @uiDefault PasswordField.focusedBackground Color optional
* @uiDefault PasswordField.border Border
* @uiDefault PasswordField.margin Insets
* @uiDefault PasswordField.echoChar character
@@ -73,6 +74,7 @@ public class FlatPasswordFieldUI
protected int minimumWidth;
protected boolean isIntelliJTheme;
protected Color placeholderForeground;
protected Color focusedBackground;
protected boolean showCapsLock;
protected Icon capsLockIcon;
@@ -91,6 +93,7 @@ public class FlatPasswordFieldUI
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" );
capsLockIcon = UIManager.getIcon( "PasswordField.capsLockIcon" );
@@ -104,6 +107,7 @@ public class FlatPasswordFieldUI
super.uninstallDefaults();
placeholderForeground = null;
focusedBackground = null;
capsLockIcon = null;
MigLayoutVisualPadding.uninstall( getComponent() );
@@ -157,7 +161,7 @@ public class FlatPasswordFieldUI
@Override
protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme );
FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground );
paintCapsLock( g );

View File

@@ -42,6 +42,7 @@ import com.formdev.flatlaf.util.HiDPIUtils;
* @uiDefault TextArea.selectionBackground Color
* @uiDefault TextArea.selectionForeground Color
* @uiDefault TextArea.inactiveForeground Color used if not enabled (yes, this is confusing; this should be named disabledForeground)
* @uiDefault TextArea.focusedBackground Color optional
* @uiDefault TextArea.border Border
* @uiDefault TextArea.margin Insets
* @uiDefault TextArea.caretBlinkRate int default is 500 milliseconds
@@ -63,6 +64,7 @@ public class FlatTextAreaUI
protected Color background;
protected Color disabledBackground;
protected Color inactiveBackground;
protected Color focusedBackground;
public static ComponentUI createUI( JComponent c ) {
return new FlatTextAreaUI();
@@ -84,6 +86,7 @@ public class FlatTextAreaUI
background = UIManager.getColor( "TextArea.background" );
disabledBackground = UIManager.getColor( "TextArea.disabledBackground" );
inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" );
focusedBackground = UIManager.getColor( "TextArea.focusedBackground" );
}
@Override
@@ -93,6 +96,7 @@ public class FlatTextAreaUI
background = null;
disabledBackground = null;
inactiveBackground = null;
focusedBackground = null;
}
@Override
@@ -164,6 +168,12 @@ public class FlatTextAreaUI
return;
}
if( focusedBackground != null && FlatUIUtils.isPermanentFocusOwner( c ) ) {
g.setColor( focusedBackground );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
return;
}
super.paintBackground( g );
}
}

View File

@@ -55,6 +55,7 @@ import com.formdev.flatlaf.util.JavaCompatibility;
* @uiDefault TextField.disabledBackground Color used if not enabled
* @uiDefault TextField.inactiveBackground Color used if not editable
* @uiDefault TextField.inactiveForeground Color used if not enabled (yes, this is confusing; this should be named disabledForeground)
* @uiDefault TextField.focusedBackground Color optional
* @uiDefault TextField.border Border
* @uiDefault TextField.margin Insets
* @uiDefault TextField.caretBlinkRate int default is 500 milliseconds
@@ -75,6 +76,7 @@ public class FlatTextFieldUI
protected int minimumWidth;
protected boolean isIntelliJTheme;
protected Color placeholderForeground;
protected Color focusedBackground;
private FocusListener focusListener;
@@ -90,6 +92,7 @@ public class FlatTextFieldUI
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
LookAndFeel.installProperty( getComponent(), "opaque", false );
@@ -101,6 +104,7 @@ public class FlatTextFieldUI
super.uninstallDefaults();
placeholderForeground = null;
focusedBackground = null;
MigLayoutVisualPadding.uninstall( getComponent() );
}
@@ -148,7 +152,7 @@ public class FlatTextFieldUI
@Override
protected void paintSafely( Graphics g ) {
paintBackground( g, getComponent(), isIntelliJTheme );
paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
paintPlaceholder( g, getComponent(), placeholderForeground );
super.paintSafely( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ) );
@@ -159,7 +163,7 @@ public class FlatTextFieldUI
// background is painted elsewhere
}
static void paintBackground( Graphics g, JTextComponent c, boolean isIntelliJTheme ) {
static void paintBackground( Graphics g, JTextComponent c, boolean isIntelliJTheme, Color focusedBackground ) {
// do not paint background if:
// - not opaque and
// - border is not a flat border and
@@ -180,7 +184,7 @@ public class FlatTextFieldUI
try {
FlatUIUtils.setRenderingHints( g2 );
Color background = c.getBackground();
Color background = focusedBackground != null && FlatUIUtils.isPermanentFocusOwner( c ) ? focusedBackground : c.getBackground();
g2.setColor( !(background instanceof UIResource)
? background
: (isIntelliJTheme && (!c.isEnabled() || !c.isEditable())