- support key prefixes for Linux desktop environments (issue #974)

- support custom key prefixes (issue #649)
- support multi-prefixed keys
- changed handling of prefixed keys
This commit is contained in:
Karl Tauber
2025-03-08 18:11:38 +01:00
parent babc8aa55d
commit f7495a0a5b
11 changed files with 218 additions and 79 deletions

View File

@@ -17,6 +17,7 @@
package com.formdev.flatlaf;
import java.util.Collections;
import java.util.Properties;
import java.util.function.Function;
import com.formdev.flatlaf.UIDefaultsLoader.ValueType;
@@ -72,4 +73,8 @@ public class UIDefaultsLoaderAccessor
{
return UIDefaultsLoader.parseColorRGBA( value );
}
public static Properties newUIProperties( boolean dark ) {
return UIDefaultsLoader.newUIProperties( dark );
}
}

View File

@@ -41,6 +41,7 @@ import org.fife.ui.autocomplete.ParameterChoicesProvider;
import org.fife.ui.autocomplete.ParameterizedCompletion;
import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import com.formdev.flatlaf.FlatLaf;
/**
* @author Karl Tauber
@@ -358,12 +359,18 @@ class FlatCompletionProvider
lastKeys = keys;
completions.clear();
outer:
for( String key : keys ) {
if( key.startsWith( "[" ) ) {
while( key.startsWith( "[" ) ) {
// remove prefix
int closeIndex = key.indexOf( ']' );
if( closeIndex < 0 )
continue;
continue outer;
String prefix = key.substring( 0, closeIndex + 1 );
if( FlatLaf.getUIKeySpecialPrefixes().contains( prefix ) )
continue outer; // can not reference properties with special prefix
key = key.substring( closeIndex + 1 );
}

View File

@@ -76,14 +76,21 @@ class FlatThemePropertiesBaseManager
definedCoreKeys = new HashSet<>();
for( Properties properties : coreThemes.values() ) {
outer:
for( Object k : properties.keySet() ) {
String key = (String) k;
if( key.startsWith( "*." ) || key.startsWith( "@" ) )
continue;
if( key.startsWith( "[" ) ) {
while( key.startsWith( "[" ) ) {
int closeIndex = key.indexOf( ']' );
if( closeIndex < 0 )
continue;
continue outer;
String prefix = key.substring( 0, closeIndex + 1 );
if( FlatLaf.getUIKeySpecialPrefixes().contains( prefix ) )
break; // keep special prefix
key = key.substring( closeIndex + 1 );
}
definedCoreKeys.add( key );

View File

@@ -33,7 +33,6 @@ import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.text.BadLocationException;
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
import com.formdev.flatlaf.util.SystemInfo;
/**
* Supports parsing content of text area in FlatLaf properties syntax.
@@ -54,17 +53,13 @@ class FlatThemePropertiesSupport
private final Map<String, Object> parsedValueCache2 = new HashMap<>();
private Set<String> allKeysCache;
private String baseTheme;
private boolean lastDark;
private static long globalCacheInvalidationCounter;
private long cacheInvalidationCounter;
private static Set<String> wildcardKeys;
private static final String platformPrefix =
SystemInfo.isWindows ? "[win]" :
SystemInfo.isMacOS ? "[mac]" :
SystemInfo.isLinux ? "[linux]" : "[unknown]";
FlatThemePropertiesSupport( FlatSyntaxTextArea textArea ) {
this.textArea = textArea;
@@ -115,6 +110,7 @@ class FlatThemePropertiesSupport
private KeyValue getKeyValueAtLine( int line ) {
try {
// get text at line
int startOffset = textArea.getLineStartOffset( line );
int endOffset = textArea.getLineEndOffset( line );
String text = textArea.getText( startOffset, endOffset - startOffset );
@@ -134,11 +130,13 @@ class FlatThemePropertiesSupport
text = text.substring( sepIndex + 1 );
}
// parse line
Properties properties = new Properties();
properties.load( new StringReader( text ) );
if( properties.isEmpty() )
return null;
// get key and value for line
String key = (String) properties.keys().nextElement();
String value = properties.getProperty( key );
return new KeyValue( key, value );
@@ -171,17 +169,7 @@ class FlatThemePropertiesSupport
}
private String getPropertyOrWildcard( String key ) {
// get platform specific properties
String value = getProperty( platformPrefix + key );
if( value != null )
return value;
// get light/dark specific properties
value = getProperty( (isDark( getBaseTheme() ) ? "[dark]" : "[light]") + key );
if( value != null )
return value;
value = getProperty( key );
String value = getProperty( key );
if( value != null )
return value;
@@ -213,9 +201,18 @@ class FlatThemePropertiesSupport
if( propertiesCache != null )
return propertiesCache;
propertiesCache = new Properties();
String text = textArea.getText();
try {
propertiesCache.load( new StringReader( textArea.getText() ) );
propertiesCache = UIDefaultsLoaderAccessor.newUIProperties( lastDark );
propertiesCache.load( new StringReader( text ) );
// re-load if dark has changed (getBaseTheme() invokes getProperties()!!!)
boolean dark = isDark( getBaseTheme() );
if( lastDark != dark ) {
lastDark = dark;
propertiesCache = UIDefaultsLoaderAccessor.newUIProperties( lastDark );
propertiesCache.load( new StringReader( text ) );
}
} catch( IOException ex ) {
ex.printStackTrace(); //TODO
}