Theme Editor:

- support lazy values and icon colors in overlay color preview
- support icon colors in preview
- support icon colors in reference auto-completion
- support changing preview theme when editing FlatLaf.properties via adding `@baseTheme = dark|darcula|intellij`
This commit is contained in:
Karl Tauber
2021-08-28 14:18:11 +02:00
parent 968e508bb5
commit 7ca48bd136
3 changed files with 55 additions and 4 deletions

View File

@@ -24,9 +24,11 @@ import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JLayer;
import javax.swing.UIDefaults.LazyValue;
import javax.swing.plaf.LayerUI;
import javax.swing.text.BadLocationException;
import org.fife.ui.rsyntaxtextarea.Token;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.HSLColor;
@@ -157,6 +159,20 @@ class FlatThemeEditorOverlay
private Color getColorInLine( FlatSyntaxTextArea textArea, int line ) {
Object value = textArea.propertiesSupport.getParsedValueAtLine( line );
// resolve lazy value
if( value instanceof LazyValue ) {
Object[] pValue = new Object[] { value };
FlatLaf.runWithUIDefaultsGetter( key -> {
return (key instanceof String)
? textArea.propertiesSupport.getParsedProperty( (String) key )
: null;
}, () -> {
pValue[0] = ((LazyValue)pValue[0]).createValue( null );
} );
value = pValue[0];
}
if( value instanceof Color )
return (Color) value;

View File

@@ -29,6 +29,7 @@ import java.util.Properties;
import java.util.Set;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatIconColors;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
@@ -111,10 +112,6 @@ class FlatThemePropertiesBaseManager
// core themes
switch( name ) {
case "FlatLaf":
result.add( "FlatLightLaf" );
break;
case "FlatLightLaf":
case "FlatDarkLaf":
result.add( "FlatLaf" );
@@ -160,6 +157,10 @@ class FlatThemePropertiesBaseManager
result.add( "FlatLaf" );
break;
}
// exclude base properties if editing base properties
if( name.equals( "FlatLaf" ) )
result.remove( "FlatLaf" );
}
return result;
@@ -173,6 +174,7 @@ class FlatThemePropertiesBaseManager
private final String name;
private final FlatThemePropertiesSupport propertiesSupport;
private final boolean isCoreTheme;
private final String coreBaseTheme;
private List<String> baseFiles;
private String lastBaseTheme;
@@ -181,10 +183,22 @@ class FlatThemePropertiesBaseManager
this.name = name;
this.propertiesSupport = propertiesSupport;
this.isCoreTheme = isCoreTheme;
switch( name ) {
case "FlatLightLaf": coreBaseTheme = "light"; break;
case "FlatDarkLaf": coreBaseTheme = "dark"; break;
case "FlatIntelliJLaf": coreBaseTheme = "intellij"; break;
case "FlatDarculaLaf": coreBaseTheme = "darcula"; break;
default: coreBaseTheme = null; break;
}
}
@Override
public String getProperty( String key, String baseTheme ) {
// override base theme for core themes
if( coreBaseTheme != null )
baseTheme = coreBaseTheme;
updateBaseFiles( baseTheme );
// search in opened editors
@@ -209,6 +223,15 @@ class FlatThemePropertiesBaseManager
}
}
// search in icon colors
if( key.startsWith( "Actions." ) || key.startsWith( "Objects." ) ) {
boolean dark = FlatThemePropertiesSupport.isDark( baseTheme );
for( FlatIconColors c : FlatIconColors.values() ) {
if( c.key.equals( key ) && (c.light == !dark || c.dark == dark) )
return String.format( "#%06x", c.rgb );
}
}
return null;
}
@@ -236,6 +259,10 @@ class FlatThemePropertiesBaseManager
@Override
public void addAllKeys( Set<String> allKeys, String baseTheme ) {
// override base theme for core themes
if( coreBaseTheme != null )
baseTheme = coreBaseTheme;
updateBaseFiles( baseTheme );
// search in opened editors
@@ -257,6 +284,10 @@ class FlatThemePropertiesBaseManager
for( String baseFile : baseFiles )
copyKeys( coreThemes.get( baseFile ), allKeys );
}
// icon colors
for( FlatIconColors c : FlatIconColors.values() )
allKeys.add( c.key );
}
private void copyKeys( Properties properties, Set<String> allKeys ) {

View File

@@ -207,6 +207,10 @@ class FlatThemePropertiesSupport
return allKeysCache;
}
static boolean isDark( String baseTheme ) {
return "dark".equals( baseTheme ) || "darcula".equals( baseTheme );
}
private String getBaseTheme() {
if( baseTheme == null )
baseTheme = getProperties().getProperty( "@baseTheme", "light" );