Styling: support styling disabledBackground and inactiveBackground of text components

This commit is contained in:
Karl Tauber
2021-06-19 11:11:57 +02:00
parent 5e20d50abf
commit b4a9c9b7f5
7 changed files with 356 additions and 32 deletions

View File

@@ -66,8 +66,14 @@ public class FlatEditorPaneUI
{
@Styleable protected int minimumWidth;
protected boolean isIntelliJTheme;
private Color background;
@Styleable protected Color disabledBackground;
@Styleable protected Color inactiveBackground;
@Styleable protected Color focusedBackground;
private Color oldDisabledBackground;
private Color oldInactiveBackground;
private Object oldHonorDisplayProperties;
private FocusListener focusListener;
private Map<String, Object> oldStyleValues;
@@ -90,6 +96,9 @@ public class FlatEditorPaneUI
String prefix = getPropertyPrefix();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
background = UIManager.getColor( prefix + ".background" );
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
// use component font and foreground for HTML text
@@ -101,8 +110,14 @@ public class FlatEditorPaneUI
protected void uninstallDefaults() {
super.uninstallDefaults();
background = null;
disabledBackground = null;
inactiveBackground = null;
focusedBackground = null;
oldDisabledBackground = null;
oldInactiveBackground = null;
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
}
@@ -125,6 +140,11 @@ public class FlatEditorPaneUI
@Override
protected void propertyChange( PropertyChangeEvent e ) {
// invoke updateBackground() before super.propertyChange()
String propertyName = e.getPropertyName();
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
updateBackground();
super.propertyChange( e );
propertyChange( getComponent(), e, this::applyStyle );
}
@@ -147,7 +167,12 @@ public class FlatEditorPaneUI
* @since TODO
*/
protected void applyStyle( Object style ) {
oldDisabledBackground = disabledBackground;
oldInactiveBackground = inactiveBackground;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateBackground();
}
/**
@@ -157,6 +182,12 @@ public class FlatEditorPaneUI
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
private void updateBackground() {
FlatTextFieldUI.updateBackground( getComponent(), background,
disabledBackground, inactiveBackground,
oldDisabledBackground, oldInactiveBackground );
}
@Override
public Dimension getPreferredSize( JComponent c ) {
return applyMinimumWidth( c, super.getPreferredSize( c ), minimumWidth );

View File

@@ -78,11 +78,17 @@ public class FlatPasswordFieldUI
{
@Styleable protected int minimumWidth;
protected boolean isIntelliJTheme;
private Color background;
@Styleable protected Color disabledBackground;
@Styleable protected Color inactiveBackground;
@Styleable protected Color placeholderForeground;
@Styleable protected Color focusedBackground;
@Styleable protected boolean showCapsLock;
protected Icon capsLockIcon;
private Color oldDisabledBackground;
private Color oldInactiveBackground;
private FocusListener focusListener;
private KeyListener capsLockListener;
private Map<String, Object> oldStyleValues;
@@ -107,6 +113,9 @@ public class FlatPasswordFieldUI
String prefix = getPropertyPrefix();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
background = UIManager.getColor( prefix + ".background" );
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
showCapsLock = UIManager.getBoolean( "PasswordField.showCapsLock" );
@@ -121,10 +130,16 @@ public class FlatPasswordFieldUI
protected void uninstallDefaults() {
super.uninstallDefaults();
background = null;
disabledBackground = null;
inactiveBackground = null;
placeholderForeground = null;
focusedBackground = null;
capsLockIcon = null;
oldDisabledBackground = null;
oldInactiveBackground = null;
MigLayoutVisualPadding.uninstall( getComponent() );
}
@@ -173,7 +188,11 @@ public class FlatPasswordFieldUI
@Override
protected void propertyChange( PropertyChangeEvent e ) {
super.propertyChange( e );
String propertyName = e.getPropertyName();
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
updateBackground();
else
super.propertyChange( e );
FlatTextFieldUI.propertyChange( getComponent(), e, this::applyStyle );
}
@@ -181,7 +200,12 @@ public class FlatPasswordFieldUI
* @since TODO
*/
protected void applyStyle( Object style ) {
oldDisabledBackground = disabledBackground;
oldInactiveBackground = inactiveBackground;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateBackground();
}
/**
@@ -217,6 +241,12 @@ public class FlatPasswordFieldUI
}
}
private void updateBackground() {
FlatTextFieldUI.updateBackground( getComponent(), background,
disabledBackground, inactiveBackground,
oldDisabledBackground, oldInactiveBackground );
}
@Override
protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );

View File

@@ -27,9 +27,7 @@ import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTextAreaUI;
import javax.swing.text.JTextComponent;
import com.formdev.flatlaf.ui.FlatStyleSupport.Styleable;
import com.formdev.flatlaf.util.HiDPIUtils;
@@ -64,11 +62,14 @@ public class FlatTextAreaUI
{
@Styleable protected int minimumWidth;
protected boolean isIntelliJTheme;
protected Color background;
private Color background;
@Styleable protected Color disabledBackground;
@Styleable protected Color inactiveBackground;
@Styleable protected Color focusedBackground;
private Color oldDisabledBackground;
private Color oldInactiveBackground;
private FocusListener focusListener;
private Map<String, Object> oldStyleValues;
@@ -103,6 +104,9 @@ public class FlatTextAreaUI
disabledBackground = null;
inactiveBackground = null;
focusedBackground = null;
oldDisabledBackground = null;
oldInactiveBackground = null;
}
@Override
@@ -124,22 +128,24 @@ public class FlatTextAreaUI
@Override
protected void propertyChange( PropertyChangeEvent e ) {
// invoke updateBackground() before super.propertyChange()
String propertyName = e.getPropertyName();
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
updateBackground();
super.propertyChange( e );
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
switch( e.getPropertyName() ) {
case "editable":
case "enabled":
updateBackground();
break;
}
}
/**
* @since TODO
*/
protected void applyStyle( Object style ) {
oldDisabledBackground = disabledBackground;
oldInactiveBackground = inactiveBackground;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateBackground();
}
@@ -151,26 +157,9 @@ public class FlatTextAreaUI
}
private void updateBackground() {
JTextComponent c = getComponent();
Color background = c.getBackground();
if( !(background instanceof UIResource) )
return;
// do not update background if it currently has a unknown color (assigned from outside)
if( background != this.background &&
background != disabledBackground &&
background != inactiveBackground )
return;
Color newBackground = !c.isEnabled()
? disabledBackground
: (!c.isEditable()
? inactiveBackground
: this.background);
if( newBackground != background )
c.setBackground( newBackground );
FlatTextFieldUI.updateBackground( getComponent(), background,
disabledBackground, inactiveBackground,
oldDisabledBackground, oldInactiveBackground );
}
@Override

View File

@@ -80,9 +80,15 @@ public class FlatTextFieldUI
{
@Styleable protected int minimumWidth;
protected boolean isIntelliJTheme;
private Color background;
@Styleable protected Color disabledBackground;
@Styleable protected Color inactiveBackground;
@Styleable protected Color placeholderForeground;
@Styleable protected Color focusedBackground;
private Color oldDisabledBackground;
private Color oldInactiveBackground;
private FocusListener focusListener;
private Map<String, Object> oldStyleValues;
private boolean borderShared = true;
@@ -105,6 +111,9 @@ public class FlatTextFieldUI
String prefix = getPropertyPrefix();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
background = UIManager.getColor( prefix + ".background" );
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
placeholderForeground = UIManager.getColor( prefix + ".placeholderForeground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
@@ -117,9 +126,15 @@ public class FlatTextFieldUI
protected void uninstallDefaults() {
super.uninstallDefaults();
background = null;
disabledBackground = null;
inactiveBackground = null;
placeholderForeground = null;
focusedBackground = null;
oldDisabledBackground = null;
oldInactiveBackground = null;
MigLayoutVisualPadding.uninstall( getComponent() );
}
@@ -148,7 +163,11 @@ public class FlatTextFieldUI
@Override
protected void propertyChange( PropertyChangeEvent e ) {
super.propertyChange( e );
String propertyName = e.getPropertyName();
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
updateBackground();
else
super.propertyChange( e );
propertyChange( getComponent(), e, this::applyStyle );
}
@@ -175,7 +194,12 @@ public class FlatTextFieldUI
* @since TODO
*/
protected void applyStyle( Object style ) {
oldDisabledBackground = disabledBackground;
oldInactiveBackground = inactiveBackground;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateBackground();
}
/**
@@ -203,6 +227,39 @@ public class FlatTextFieldUI
}
}
private void updateBackground() {
updateBackground( getComponent(), background,
disabledBackground, inactiveBackground,
oldDisabledBackground, oldInactiveBackground );
}
// same functionality as BasicTextUI.updateBackground()
static void updateBackground( JTextComponent c, Color background,
Color disabledBackground, Color inactiveBackground,
Color oldDisabledBackground, Color oldInactiveBackground )
{
Color oldBackground = c.getBackground();
if( !(oldBackground instanceof UIResource) )
return;
// do not update background if it currently has a unknown color (assigned from outside)
if( oldBackground != background &&
oldBackground != disabledBackground &&
oldBackground != inactiveBackground &&
oldBackground != oldDisabledBackground &&
oldBackground != oldInactiveBackground )
return;
Color newBackground = !c.isEnabled()
? disabledBackground
: (!c.isEditable()
? inactiveBackground
: background);
if( newBackground != oldBackground )
c.setBackground( newBackground );
}
@Override
protected void paintSafely( Graphics g ) {
paintBackground( g, getComponent(), isIntelliJTheme, focusedBackground );

View File

@@ -62,8 +62,14 @@ public class FlatTextPaneUI
{
@Styleable protected int minimumWidth;
protected boolean isIntelliJTheme;
private Color background;
@Styleable protected Color disabledBackground;
@Styleable protected Color inactiveBackground;
@Styleable protected Color focusedBackground;
private Color oldDisabledBackground;
private Color oldInactiveBackground;
private Object oldHonorDisplayProperties;
private FocusListener focusListener;
private Map<String, Object> oldStyleValues;
@@ -86,6 +92,9 @@ public class FlatTextPaneUI
String prefix = getPropertyPrefix();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
background = UIManager.getColor( prefix + ".background" );
disabledBackground = UIManager.getColor( prefix + ".disabledBackground" );
inactiveBackground = UIManager.getColor( prefix + ".inactiveBackground" );
focusedBackground = UIManager.getColor( prefix + ".focusedBackground" );
// use component font and foreground for HTML text
@@ -97,8 +106,14 @@ public class FlatTextPaneUI
protected void uninstallDefaults() {
super.uninstallDefaults();
background = null;
disabledBackground = null;
inactiveBackground = null;
focusedBackground = null;
oldDisabledBackground = null;
oldInactiveBackground = null;
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
}
@@ -121,6 +136,11 @@ public class FlatTextPaneUI
@Override
protected void propertyChange( PropertyChangeEvent e ) {
// invoke updateBackground() before super.propertyChange()
String propertyName = e.getPropertyName();
if( "editable".equals( propertyName ) || "enabled".equals( propertyName ) )
updateBackground();
super.propertyChange( e );
FlatEditorPaneUI.propertyChange( getComponent(), e, this::applyStyle );
}
@@ -129,7 +149,12 @@ public class FlatTextPaneUI
* @since TODO
*/
protected void applyStyle( Object style ) {
oldDisabledBackground = disabledBackground;
oldInactiveBackground = inactiveBackground;
oldStyleValues = FlatStyleSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
updateBackground();
}
/**
@@ -139,6 +164,12 @@ public class FlatTextPaneUI
return FlatStyleSupport.applyToAnnotatedObject( this, key, value );
}
private void updateBackground() {
FlatTextFieldUI.updateBackground( getComponent(), background,
disabledBackground, inactiveBackground,
oldDisabledBackground, oldInactiveBackground );
}
@Override
public Dimension getPreferredSize( JComponent c ) {
return FlatEditorPaneUI.applyMinimumWidth( c, super.getPreferredSize( c ), minimumWidth );