Theme Editor: Preview: support usage of variables in styles

This commit is contained in:
Karl Tauber
2021-12-10 23:14:19 +01:00
parent 70336e31c7
commit 09d8d09aad
5 changed files with 53 additions and 2 deletions

View File

@@ -80,7 +80,7 @@ class UIDefaultsLoader
private static final String OPTIONAL_PREFIX = "?";
private static final String WILDCARD_PREFIX = "*.";
private static final String KEY_VARIABLES = "FlatLaf.internal.variables";
static final String KEY_VARIABLES = "FlatLaf.internal.variables";
private static int parseColorDepth;

View File

@@ -27,6 +27,8 @@ import com.formdev.flatlaf.UIDefaultsLoader.ValueType;
*/
public class UIDefaultsLoaderAccessor
{
public static final String KEY_VARIABLES = UIDefaultsLoader.KEY_VARIABLES;
public static Object UNKNOWN = ValueType.UNKNOWN;
public static Object STRING = ValueType.STRING;
public static Object BOOLEAN = ValueType.BOOLEAN;

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.themeeditor;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.prefs.Preferences;
@@ -28,6 +29,7 @@ import javax.swing.UIDefaults.LazyValue;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
import com.formdev.flatlaf.extras.components.*;
/**
@@ -51,6 +53,7 @@ class FlatThemePreview
private final Map<LazyValue, Object> lazyValueCache = new WeakHashMap<>();
private int runWithUIDefaultsGetterLevel;
private boolean inGetDefaultFont;
private boolean inGetVariables;
FlatThemePreview( FlatSyntaxTextArea textArea ) {
this.textArea = textArea;
@@ -162,6 +165,7 @@ class FlatThemePreview
}
}
@SuppressWarnings( "unchecked" )
Object getUIDefaultProperty( Object key ) {
// avoid StackOverflowError because "defaultFont" value is an active value
// that itself uses UIManager.getFont( "defaultFont" ) to get base font
@@ -176,6 +180,19 @@ class FlatThemePreview
if( ((String)key).endsWith( "UI" ) )
return null;
// handle access to variables map, which is used in styles
if( UIDefaultsLoaderAccessor.KEY_VARIABLES.equals( key ) ) {
if( inGetVariables )
return null;
inGetVariables = true;
try {
return new VariablesDelegateMap( (Map<String, String>) UIManager.get( UIDefaultsLoaderAccessor.KEY_VARIABLES ) );
} finally {
inGetVariables = false;
}
}
Object value = textArea.propertiesSupport.getParsedProperty( (String) key );
boolean isDefaultFont = "defaultFont".equals( key );
@@ -341,4 +358,31 @@ class FlatThemePreview
}
}
}
//---- class VariablesDelegateMap -----------------------------------------
private class VariablesDelegateMap
extends HashMap<String, String>
{
private final Map<String, String> variables;
public VariablesDelegateMap( Map<String, String> variables ) {
this.variables = variables;
}
@Override
public String get( Object key ) {
String value = textArea.propertiesSupport.getProperty( (String) key );
if( value != null )
return value;
if( variables != null ) {
value = variables.get( key );
if( value != null )
return value;
}
return super.get( key );
}
}
}

View File

@@ -179,7 +179,7 @@ class FlatThemePropertiesSupport
return getProperty( wildcardKey );
}
private String getProperty( String key ) {
String getProperty( String key ) {
// look in current text area
String value = getProperties().getProperty( key );
if( value != null )

View File

@@ -117,3 +117,8 @@ Prop.6.selectionForeground = contrast($Prop.6.selectionBackground,#000,#fff)
Prop.7.selectionBackground = #FF9500
Prop.7.selectionForeground = contrast($Prop.7.selectionBackground,#000,#fff)
@varStyle1 = #f0f
[style].style1 = background: @varStyle1