mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
Styling: support using variables (defined in properties files) in CSS styles
This commit is contained in:
@@ -886,6 +886,9 @@ public abstract class FlatLaf
|
||||
public static Object parseDefaultsValue( String key, String value, Class<?> valueType )
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
// resolve variables
|
||||
value = UIDefaultsLoader.resolveValueFromUIManager( value );
|
||||
|
||||
// parse value
|
||||
Object val = UIDefaultsLoader.parseValue( key, value, valueType, null,
|
||||
v -> UIDefaultsLoader.resolveValueFromUIManager( v ), Collections.emptyList() );
|
||||
|
||||
@@ -80,6 +80,8 @@ class UIDefaultsLoader
|
||||
private static final String OPTIONAL_PREFIX = "?";
|
||||
private static final String WILDCARD_PREFIX = "*.";
|
||||
|
||||
private static final String FLATLAF_VARIABLES = "FlatLaf.variables";
|
||||
|
||||
private static int parseColorDepth;
|
||||
|
||||
private static final Cache<String, Object> fontCache = new Cache<>();
|
||||
@@ -244,10 +246,13 @@ class UIDefaultsLoader
|
||||
};
|
||||
|
||||
// parse and add properties to UI defaults
|
||||
Map<String, String> variables = new HashMap<>( 50 );
|
||||
for( Map.Entry<Object, Object> e : properties.entrySet() ) {
|
||||
String key = (String) e.getKey();
|
||||
if( key.startsWith( VARIABLE_PREFIX ) )
|
||||
if( key.startsWith( VARIABLE_PREFIX ) ) {
|
||||
variables.put( key, (String) e.getValue() );
|
||||
continue;
|
||||
}
|
||||
|
||||
String value = resolveValue( (String) e.getValue(), propertiesGetter );
|
||||
try {
|
||||
@@ -256,6 +261,9 @@ class UIDefaultsLoader
|
||||
logParseError( key, value, ex, true );
|
||||
}
|
||||
}
|
||||
|
||||
// remember variables in defaults to allow using them in styles
|
||||
defaults.put( FLATLAF_VARIABLES, variables );
|
||||
} catch( IOException ex ) {
|
||||
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to load properties files.", ex );
|
||||
}
|
||||
@@ -299,6 +307,16 @@ class UIDefaultsLoader
|
||||
}
|
||||
|
||||
static String resolveValueFromUIManager( String value ) {
|
||||
if( value.startsWith( VARIABLE_PREFIX ) ) {
|
||||
@SuppressWarnings( "unchecked" )
|
||||
Map<String, String> variables = (Map<String, String>) UIManager.get( FLATLAF_VARIABLES );
|
||||
String newValue = (variables != null) ? variables.get( value ) : null;
|
||||
if( newValue == null )
|
||||
throw new IllegalArgumentException( "variable '" + value + "' not found" );
|
||||
|
||||
return newValue;
|
||||
}
|
||||
|
||||
if( !value.startsWith( PROPERTY_PREFIX ) )
|
||||
return value;
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import com.formdev.flatlaf.icons.*;
|
||||
import com.formdev.flatlaf.util.ColorFunctions;
|
||||
|
||||
/**
|
||||
* @author Karl Tauber
|
||||
@@ -157,6 +158,19 @@ public class TestFlatStyling
|
||||
testStyle( "arrowType", "chevron", "$Component.arrowType" );
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseVariables() {
|
||||
Color background = UIManager.getColor( "Panel.background" );
|
||||
|
||||
testColorStyle( background.getRGB(), "@background" );
|
||||
testColorStyle(
|
||||
ColorFunctions.darken( background, 0.2f ).getRGB(),
|
||||
"darken(@background,20%)" );
|
||||
testColorStyle(
|
||||
ColorFunctions.saturate( ColorFunctions.darken( background, 0.2f ), 0.1f ).getRGB(),
|
||||
"saturate(darken(@background,20%),10%)" );
|
||||
}
|
||||
|
||||
private void testColorStyle( int expectedRGB, String style ) {
|
||||
testStyle( "background", new Color( expectedRGB, (expectedRGB & 0xff000000) != 0 ), style );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user