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