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.borderColor Color
* @uiDefault Component.disabledBorderColor Color * @uiDefault Component.disabledBorderColor Color
* @uiDefault ComboBox.editableBackground Color optional; defaults to ComboBox.background * @uiDefault ComboBox.editableBackground Color optional; defaults to ComboBox.background
* @uiDefault ComboBox.focusedBackground Color optional
* @uiDefault ComboBox.disabledBackground Color * @uiDefault ComboBox.disabledBackground Color
* @uiDefault ComboBox.disabledForeground Color * @uiDefault ComboBox.disabledForeground Color
* @uiDefault ComboBox.buttonBackground Color * @uiDefault ComboBox.buttonBackground Color
* @uiDefault ComboBox.buttonEditableBackground Color * @uiDefault ComboBox.buttonEditableBackground Color
* @uiDefault ComboBox.buttonFocusedBackground Color optional
* @uiDefault ComboBox.buttonArrowColor Color * @uiDefault ComboBox.buttonArrowColor Color
* @uiDefault ComboBox.buttonDisabledArrowColor Color * @uiDefault ComboBox.buttonDisabledArrowColor Color
* @uiDefault ComboBox.buttonHoverArrowColor Color * @uiDefault ComboBox.buttonHoverArrowColor Color
@@ -116,10 +118,12 @@ public class FlatComboBoxUI
protected Color editableBackground; protected Color editableBackground;
protected Color disabledBackground; protected Color disabledBackground;
protected Color focusedBackground;
protected Color disabledForeground; protected Color disabledForeground;
protected Color buttonBackground; protected Color buttonBackground;
protected Color buttonEditableBackground; protected Color buttonEditableBackground;
protected Color buttonFocusedBackground;
protected Color buttonArrowColor; protected Color buttonArrowColor;
protected Color buttonDisabledArrowColor; protected Color buttonDisabledArrowColor;
protected Color buttonHoverArrowColor; protected Color buttonHoverArrowColor;
@@ -196,9 +200,11 @@ public class FlatComboBoxUI
editableBackground = UIManager.getColor( "ComboBox.editableBackground" ); editableBackground = UIManager.getColor( "ComboBox.editableBackground" );
disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" ); disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" );
focusedBackground = UIManager.getColor( "ComboBox.focusedBackground" );
disabledForeground = UIManager.getColor( "ComboBox.disabledForeground" ); disabledForeground = UIManager.getColor( "ComboBox.disabledForeground" );
buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" ); buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" );
buttonFocusedBackground = UIManager.getColor( "ComboBox.buttonFocusedBackground" );
buttonEditableBackground = UIManager.getColor( "ComboBox.buttonEditableBackground" ); buttonEditableBackground = UIManager.getColor( "ComboBox.buttonEditableBackground" );
buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" ); buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" ); buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" );
@@ -225,10 +231,12 @@ public class FlatComboBoxUI
editableBackground = null; editableBackground = null;
disabledBackground = null; disabledBackground = null;
focusedBackground = null;
disabledForeground = null; disabledForeground = null;
buttonBackground = null; buttonBackground = null;
buttonEditableBackground = null; buttonEditableBackground = null;
buttonFocusedBackground = null;
buttonArrowColor = null; buttonArrowColor = null;
buttonDisabledArrowColor = null; buttonDisabledArrowColor = null;
buttonHoverArrowColor = null; buttonHoverArrowColor = null;
@@ -423,7 +431,7 @@ public class FlatComboBoxUI
// paint arrow button background // paint arrow button background
if( enabled && !isCellRenderer ) { if( enabled && !isCellRenderer ) {
g2.setColor( paintButton ? buttonEditableBackground : buttonBackground ); g2.setColor( paintButton ? buttonEditableBackground : buttonFocusedBackground != null && isFocusOwner() ? buttonFocusedBackground : buttonBackground );
Shape oldClip = g2.getClip(); Shape oldClip = g2.getClip();
if( isLeftToRight ) if( isLeftToRight )
g2.clipRect( arrowX, 0, width - arrowX, height ); g2.clipRect( arrowX, 0, width - arrowX, height );
@@ -484,10 +492,16 @@ public class FlatComboBoxUI
protected Color getBackground( boolean enabled ) { protected Color getBackground( boolean enabled ) {
return 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); : (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 ) { protected Color getForeground( boolean enabled ) {
return enabled ? comboBox.getForeground() : disabledForeground; 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.disabledBackground Color used if not enabled
* @uiDefault PasswordField.inactiveBackground Color used if not editable * @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.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.border Border
* @uiDefault PasswordField.margin Insets * @uiDefault PasswordField.margin Insets
* @uiDefault PasswordField.echoChar character * @uiDefault PasswordField.echoChar character
@@ -73,6 +74,7 @@ public class FlatPasswordFieldUI
protected int minimumWidth; protected int minimumWidth;
protected boolean isIntelliJTheme; protected boolean isIntelliJTheme;
protected Color placeholderForeground; protected Color placeholderForeground;
protected Color focusedBackground;
protected boolean showCapsLock; protected boolean showCapsLock;
protected Icon capsLockIcon; protected Icon capsLockIcon;
@@ -91,6 +93,7 @@ public class FlatPasswordFieldUI
minimumWidth = UIManager.getInt( "Component.minimumWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" ); placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" ); showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" );
capsLockIcon = UIManager.getIcon( "PasswordField.capsLockIcon" ); capsLockIcon = UIManager.getIcon( "PasswordField.capsLockIcon" );
@@ -104,6 +107,7 @@ public class FlatPasswordFieldUI
super.uninstallDefaults(); super.uninstallDefaults();
placeholderForeground = null; placeholderForeground = null;
focusedBackground = null;
capsLockIcon = null; capsLockIcon = null;
MigLayoutVisualPadding.uninstall( getComponent() ); MigLayoutVisualPadding.uninstall( getComponent() );
@@ -157,7 +161,7 @@ public class FlatPasswordFieldUI
@Override @Override
protected void paintSafely( Graphics g ) { protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme ); FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground ); FlatTextFieldUI.paintPlaceholder( g, getComponent(), placeholderForeground );
paintCapsLock( g ); paintCapsLock( g );

View File

@@ -42,6 +42,7 @@ import com.formdev.flatlaf.util.HiDPIUtils;
* @uiDefault TextArea.selectionBackground Color * @uiDefault TextArea.selectionBackground Color
* @uiDefault TextArea.selectionForeground Color * @uiDefault TextArea.selectionForeground Color
* @uiDefault TextArea.inactiveForeground Color used if not enabled (yes, this is confusing; this should be named disabledForeground) * @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.border Border
* @uiDefault TextArea.margin Insets * @uiDefault TextArea.margin Insets
* @uiDefault TextArea.caretBlinkRate int default is 500 milliseconds * @uiDefault TextArea.caretBlinkRate int default is 500 milliseconds
@@ -63,6 +64,7 @@ public class FlatTextAreaUI
protected Color background; protected Color background;
protected Color disabledBackground; protected Color disabledBackground;
protected Color inactiveBackground; protected Color inactiveBackground;
protected Color focusedBackground;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTextAreaUI(); return new FlatTextAreaUI();
@@ -84,6 +86,7 @@ public class FlatTextAreaUI
background = UIManager.getColor( "TextArea.background" ); background = UIManager.getColor( "TextArea.background" );
disabledBackground = UIManager.getColor( "TextArea.disabledBackground" ); disabledBackground = UIManager.getColor( "TextArea.disabledBackground" );
inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" ); inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" );
focusedBackground = UIManager.getColor( "TextArea.focusedBackground" );
} }
@Override @Override
@@ -93,6 +96,7 @@ public class FlatTextAreaUI
background = null; background = null;
disabledBackground = null; disabledBackground = null;
inactiveBackground = null; inactiveBackground = null;
focusedBackground = null;
} }
@Override @Override
@@ -164,6 +168,12 @@ public class FlatTextAreaUI
return; return;
} }
if( focusedBackground != null && FlatUIUtils.isPermanentFocusOwner( c ) ) {
g.setColor( focusedBackground );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
return;
}
super.paintBackground( g ); 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.disabledBackground Color used if not enabled
* @uiDefault TextField.inactiveBackground Color used if not editable * @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.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.border Border
* @uiDefault TextField.margin Insets * @uiDefault TextField.margin Insets
* @uiDefault TextField.caretBlinkRate int default is 500 milliseconds * @uiDefault TextField.caretBlinkRate int default is 500 milliseconds
@@ -75,6 +76,7 @@ public class FlatTextFieldUI
protected int minimumWidth; protected int minimumWidth;
protected boolean isIntelliJTheme; protected boolean isIntelliJTheme;
protected Color placeholderForeground; protected Color placeholderForeground;
protected Color focusedBackground;
private FocusListener focusListener; private FocusListener focusListener;
@@ -90,6 +92,7 @@ public class FlatTextFieldUI
minimumWidth = UIManager.getInt( "Component.minimumWidth" ); minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" ); isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" ); placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
LookAndFeel.installProperty( getComponent(), "opaque", false ); LookAndFeel.installProperty( getComponent(), "opaque", false );
@@ -101,6 +104,7 @@ public class FlatTextFieldUI
super.uninstallDefaults(); super.uninstallDefaults();
placeholderForeground = null; placeholderForeground = null;
focusedBackground = null;
MigLayoutVisualPadding.uninstall( getComponent() ); MigLayoutVisualPadding.uninstall( getComponent() );
} }
@@ -148,7 +152,7 @@ public class FlatTextFieldUI
@Override @Override
protected void paintSafely( Graphics g ) { protected void paintSafely( Graphics g ) {
paintBackground( g, getComponent(), isIntelliJTheme ); paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );
paintPlaceholder( g, getComponent(), placeholderForeground ); paintPlaceholder( g, getComponent(), placeholderForeground );
super.paintSafely( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ) ); super.paintSafely( HiDPIUtils.createGraphicsTextYCorrection( (Graphics2D) g ) );
@@ -159,7 +163,7 @@ public class FlatTextFieldUI
// background is painted elsewhere // 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: // do not paint background if:
// - not opaque and // - not opaque and
// - border is not a flat border and // - border is not a flat border and
@@ -180,7 +184,7 @@ public class FlatTextFieldUI
try { try {
FlatUIUtils.setRenderingHints( g2 ); FlatUIUtils.setRenderingHints( g2 );
Color background = c.getBackground(); Color background = focusedBackground != null && FlatUIUtils.isPermanentFocusOwner( c ) ? focusedBackground : c.getBackground();
g2.setColor( !(background instanceof UIResource) g2.setColor( !(background instanceof UIResource)
? background ? background
: (isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) : (isIntelliJTheme && (!c.isEnabled() || !c.isEditable())