Skip to content

Commit

Permalink
IntelliJ Themes: fixes for text component disabled backgrounds, butto…
Browse files Browse the repository at this point in the history
…n backgrounds, combobox disabled backgrounds and spinner disabled backgrounds
  • Loading branch information
DevCharly committed Nov 14, 2019
1 parent 3b740cb commit a907cd7
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ private void applyProperties( UIDefaults defaults ) {
if( ui == null )
return;

defaults.put( "Component.isIntelliJTheme", true );

loadNamedColors( defaults );

// convert Json "ui" structure to UI defaults
Expand Down Expand Up @@ -229,7 +231,7 @@ else if( key.endsWith( ".endBackground" ) || key.endsWith( ".endBorderColor" ) )
// (e.g. set ComboBox.buttonEditableBackground to *.background
// because it is mapped from ComboBox.ArrowButton.background)
String km = uiKeyInverseMapping.getOrDefault( k, (String) k );
if( km.endsWith( tail ) )
if( km.endsWith( tail ) && !noWildcardReplace.contains( k ) )
defaults.put( k, uiValue );
}
}
Expand Down Expand Up @@ -312,6 +314,7 @@ private void fixCheckBoxColor( UIDefaults defaults, Map<String, Object> colorPal
private static Map<String, String> uiKeyMapping = new HashMap<>();
private static Map<String, String> uiKeyInverseMapping = new HashMap<>();
private static Map<String, String> checkboxKeyMapping = new HashMap<>();
private static Set<String> noWildcardReplace = new HashSet<>();

static {
// ComboBox
Expand All @@ -336,6 +339,16 @@ private void fixCheckBoxColor( UIDefaults defaults, Map<String, Object> colorPal
checkboxKeyMapping.put( "Checkbox.Border.Selected", "CheckBox.icon.selectedBorderColor" );
checkboxKeyMapping.put( "Checkbox.Foreground.Selected", "CheckBox.icon.checkmarkColor" );
checkboxKeyMapping.put( "Checkbox.Focus.Thin.Selected", "CheckBox.icon.selectedFocusedBorderColor" );

// because FlatLaf uses Button.background and Button.borderColor,
// but Darcula uses Button.startBackground and Button.startBorderColor,
// our default button background and border colors may be replaced by
// wildcard *.background and *.borderColor colors
noWildcardReplace.add( "Button.background" );
noWildcardReplace.add( "Button.borderColor" );
noWildcardReplace.add( "Button.default.background" );
noWildcardReplace.add( "Button.default.borderColor" );
noWildcardReplace.add( "ToggleButton.background" );
}

//---- class ThemeLaf -----------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @uiDefault Component.focusWidth int
* @uiDefault Component.arc int
* @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault Component.isIntelliJTheme boolean
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault ComboBox.editableBackground Color optional; defaults to ComboBox.background
Expand All @@ -89,6 +90,7 @@ public class FlatComboBoxUI
protected int focusWidth;
protected int arc;
protected String arrowType;
protected boolean isIntelliJTheme;
protected Color borderColor;
protected Color disabledBorderColor;

Expand Down Expand Up @@ -140,6 +142,7 @@ protected void installDefaults() {
focusWidth = UIManager.getInt( "Component.focusWidth" );
arc = UIManager.getInt( "Component.arc" );
arrowType = UIManager.getString( "Component.arrowType" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
borderColor = UIManager.getColor( "Component.borderColor" );
disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );

Expand Down Expand Up @@ -309,7 +312,7 @@ public void update( Graphics g, JComponent c ) {
// paint background
g2.setColor( enabled
? (editableBackground != null && comboBox.isEditable() ? editableBackground : c.getBackground())
: disabledBackground );
: getDisabledBackground( comboBox ) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );

// paint arrow button background
Expand Down Expand Up @@ -347,7 +350,7 @@ public void paintCurrentValue( Graphics g, Rectangle bounds, boolean hasFocus )

boolean enabled = comboBox.isEnabled();
c.setForeground( enabled ? comboBox.getForeground() : disabledForeground );
c.setBackground( enabled ? comboBox.getBackground() : disabledBackground );
c.setBackground( enabled ? comboBox.getBackground() : getDisabledBackground( comboBox ) );

boolean shouldValidate = (c instanceof JPanel);
if( padding != null )
Expand All @@ -364,10 +367,14 @@ public void paintCurrentValue( Graphics g, Rectangle bounds, boolean hasFocus )

@Override
public void paintCurrentValueBackground( Graphics g, Rectangle bounds, boolean hasFocus ) {
g.setColor( comboBox.isEnabled() ? comboBox.getBackground() : disabledBackground );
g.setColor( comboBox.isEnabled() ? comboBox.getBackground() : getDisabledBackground( comboBox ) );
g.fillRect( bounds.x, bounds.y, bounds.width, bounds.height );
}

private Color getDisabledBackground( JComponent c ) {
return isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground;
}

@Override
protected Dimension getSizeForComponent( Component comp ) {
Dimension size = super.getSizeForComponent( comp );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicEditorPaneUI;
import javax.swing.text.JTextComponent;

/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JEditorPane}.
Expand All @@ -45,13 +48,15 @@
* <!-- FlatEditorPaneUI -->
*
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
*
* @author Karl Tauber
*/
public class FlatEditorPaneUI
extends BasicEditorPaneUI
{
protected int minimumWidth;
protected boolean isIntelliJTheme;

private Object oldHonorDisplayProperties;

Expand All @@ -64,6 +69,7 @@ protected void installDefaults() {
super.installDefaults();

minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );

// use component font and foreground for HTML text
oldHonorDisplayProperties = getComponent().getClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES );
Expand Down Expand Up @@ -95,4 +101,17 @@ private Dimension applyMinimumWidth( Dimension size ) {
size.width = Math.max( size.width, scale( minimumWidth ) - (scale( 1 ) * 2) );
return size;
}

@Override
protected void paintBackground( Graphics g ) {
JTextComponent c = getComponent();

// for compatibility with IntelliJ themes
if( isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) && (c.getBackground() instanceof UIResource) ) {
FlatUIUtils.paintParentBackground( g, c );
return;
}

super.paintBackground( g );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
*
* @author Karl Tauber
*/
Expand All @@ -58,6 +59,7 @@ public class FlatPasswordFieldUI
{
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;

private FocusListener focusListener;

Expand All @@ -75,6 +77,7 @@ protected void installDefaults() {

focusWidth = UIManager.getInt( "Component.focusWidth" );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );

LookAndFeel.installProperty( getComponent(), "opaque", focusWidth == 0 );

Expand Down Expand Up @@ -106,7 +109,7 @@ protected void uninstallListeners() {

@Override
protected void paintSafely( Graphics g ) {
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth );
FlatTextFieldUI.paintBackground( g, getComponent(), focusWidth, isIntelliJTheme );
super.paintSafely( g );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* @uiDefault Component.arc int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.arrowType String triangle (default) or chevron
* @uiDefault Component.isIntelliJTheme boolean
* @uiDefault Component.borderColor Color
* @uiDefault Component.disabledBorderColor Color
* @uiDefault Spinner.disabledBackground Color
Expand All @@ -81,6 +82,7 @@ public class FlatSpinnerUI
protected int arc;
protected int minimumWidth;
protected String arrowType;
protected boolean isIntelliJTheme;
protected Color borderColor;
protected Color disabledBorderColor;
protected Color disabledBackground;
Expand All @@ -105,6 +107,7 @@ protected void installDefaults() {
arc = UIManager.getInt( "Component.arc" );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
arrowType = UIManager.getString( "Component.arrowType" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
borderColor = UIManager.getColor( "Component.borderColor" );
disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );
disabledBackground = UIManager.getColor( "Spinner.disabledBackground" );
Expand Down Expand Up @@ -261,7 +264,9 @@ public void update( Graphics g, JComponent c ) {
boolean isLeftToRight = spinner.getComponentOrientation().isLeftToRight();

// paint background
g2.setColor( enabled ? c.getBackground() : disabledBackground );
g2.setColor( enabled
? c.getBackground()
: (isIntelliJTheme ? FlatUIUtils.getParentBackground( c ) : disabledBackground) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, width, height, focusWidth, arc );

// paint arrow buttons background
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* <!-- FlatTextAreaUI -->
*
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
* @uiDefault TextArea.disabledBackground Color used if not enabled
* @uiDefault TextArea.inactiveBackground Color used if not editable
*
Expand All @@ -56,6 +57,7 @@ public class FlatTextAreaUI
extends BasicTextAreaUI
{
protected int minimumWidth;
protected boolean isIntelliJTheme;
protected Color disabledBackground;
protected Color inactiveBackground;

Expand All @@ -68,6 +70,7 @@ protected void installDefaults() {
super.installDefaults();

minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );
disabledBackground = UIManager.getColor( "TextArea.disabledBackground" );
inactiveBackground = UIManager.getColor( "TextArea.inactiveBackground" );
}
Expand All @@ -87,9 +90,11 @@ protected void paintBackground( Graphics g ) {
Color background = c.getBackground();
g.setColor( !(background instanceof UIResource)
? background
: (!c.isEnabled()
? disabledBackground
: (!c.isEditable() ? inactiveBackground : background)) );
: (isIntelliJTheme && (!c.isEnabled() || !c.isEditable())
? FlatUIUtils.getParentBackground( c )
: (!c.isEnabled()
? disabledBackground
: (!c.isEditable() ? inactiveBackground : background))) );
g.fillRect( 0, 0, c.getWidth(), c.getHeight() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.formdev.flatlaf.ui;

import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
Expand All @@ -29,6 +30,7 @@
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;

Expand All @@ -54,6 +56,7 @@
*
* @uiDefault Component.focusWidth int
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
*
* @author Karl Tauber
*/
Expand All @@ -62,6 +65,7 @@ public class FlatTextFieldUI
{
protected int focusWidth;
protected int minimumWidth;
protected boolean isIntelliJTheme;

private FocusListener focusListener;

Expand All @@ -75,6 +79,7 @@ protected void installDefaults() {

focusWidth = UIManager.getInt( "Component.focusWidth" );
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );

LookAndFeel.installProperty( getComponent(), "opaque", focusWidth == 0 );

Expand Down Expand Up @@ -106,7 +111,7 @@ protected void uninstallListeners() {

@Override
protected void paintSafely( Graphics g ) {
paintBackground( g, getComponent(), focusWidth );
paintBackground( g, getComponent(), focusWidth, isIntelliJTheme );
super.paintSafely( g );
}

Expand All @@ -115,7 +120,7 @@ protected void paintBackground( Graphics g ) {
// background is painted elsewhere
}

static void paintBackground( Graphics g, JTextComponent c, int focusWidth ) {
static void paintBackground( Graphics g, JTextComponent c, int focusWidth, boolean isIntelliJTheme ) {
// do not paint background if:
// - not opaque and
// - border is not a flat border and
Expand All @@ -135,7 +140,12 @@ static void paintBackground( Graphics g, JTextComponent c, int focusWidth ) {

float fFocusWidth = (c.getBorder() instanceof FlatBorder) ? scale( (float) focusWidth ) : 0;

g2.setColor( c.getBackground() );
Color background = c.getBackground();
g2.setColor( !(background instanceof UIResource)
? background
: (isIntelliJTheme && (!c.isEnabled() || !c.isEditable())
? FlatUIUtils.getParentBackground( c )
: background) );
FlatUIUtils.fillRoundRectangle( g2, 0, 0, c.getWidth(), c.getHeight(), fFocusWidth, 0 );
} finally {
g2.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.JTextComponent;

/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTextPane}.
Expand All @@ -45,13 +48,15 @@
* <!-- FlatTextPaneUI -->
*
* @uiDefault Component.minimumWidth int
* @uiDefault Component.isIntelliJTheme boolean
*
* @author Karl Tauber
*/
public class FlatTextPaneUI
extends BasicTextPaneUI
{
protected int minimumWidth;
protected boolean isIntelliJTheme;

private Object oldHonorDisplayProperties;

Expand All @@ -64,6 +69,7 @@ protected void installDefaults() {
super.installDefaults();

minimumWidth = UIManager.getInt( "Component.minimumWidth" );
isIntelliJTheme = UIManager.getBoolean( "Component.isIntelliJTheme" );

// use component font and foreground for HTML text
oldHonorDisplayProperties = getComponent().getClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES );
Expand Down Expand Up @@ -95,4 +101,17 @@ private Dimension applyMinimumWidth( Dimension size ) {
size.width = Math.max( size.width, scale( minimumWidth ) - (scale( 1 ) * 2) );
return size;
}

@Override
protected void paintBackground( Graphics g ) {
JTextComponent c = getComponent();

// for compatibility with IntelliJ themes
if( isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) && (c.getBackground() instanceof UIResource) ) {
FlatUIUtils.paintParentBackground( g, c );
return;
}

super.paintBackground( g );
}
}
Loading

0 comments on commit a907cd7

Please sign in to comment.