Browse Source

Refactored property replacements.

pull/139/head
weisj 5 years ago
parent
commit
7ab5c8c2a4
  1. 13
      core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java
  2. 16
      core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java
  3. 31
      property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java

13
core/src/main/java/com/github/weisj/darklaf/task/FontDefaultsInitTask.java

@ -75,12 +75,7 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
}
private void patchMacOSFonts(final UIDefaults defaults) {
for (Map.Entry<Object, Object> entry : defaults.entrySet()) {
if (entry.getValue() instanceof Font) {
Font font = (Font) entry.getValue();
entry.setValue(macOSFontFromFont(font));
}
}
PropertyLoader.replacePropertiesOfType(Font.class, defaults, this::macOSFontFromFont);
}
private Font macOSFontFromFont(final Font font) {
@ -95,11 +90,7 @@ public class FontDefaultsInitTask implements DefaultsInitTask {
private void applyFontRule(final Theme currentTheme, final UIDefaults defaults) {
FontSizeRule rule = currentTheme.getFontSizeRule();
if (rule == null || rule.getType() == FontSizeRule.AdjustmentType.NO_ADJUSTMENT) return;
for (Map.Entry<Object, Object> entry : defaults.entrySet()) {
if (entry != null && entry.getValue() instanceof Font) {
entry.setValue(fontWithRule((Font) entry.getValue(), rule, defaults));
}
}
PropertyLoader.replacePropertiesOfType(Font.class, defaults, f -> fontWithRule(f, rule, defaults));
}
private Font fontWithRule(final Font font, final FontSizeRule rule, final UIDefaults defaults) {

16
core/src/main/java/com/github/weisj/darklaf/task/ThemeDefaultsInitTask.java

@ -92,16 +92,14 @@ public class ThemeDefaultsInitTask implements DefaultsInitTask {
globalSettings.put(((String) key).substring(GLOBAL_PREFIX.length()), uiProps.get(key));
}
}
for (final Object key : defaults.keySet()) {
if (key instanceof String && ((String) key).contains(".")) {
final String s = (String) key;
PropertyLoader.replaceProperties(
defaults,
e -> e.getKey() instanceof String && ((String) e.getKey()).contains("."),
e -> {
final String s = (String) e.getKey();
final String globalKey = s.substring(s.lastIndexOf('.') + 1);
if (globalSettings.containsKey(globalKey)) {
defaults.put(key, globalSettings.get(globalKey));
}
}
}
return globalSettings.get(globalKey);
});
}
private void initUIProperties(final Theme currentTheme, final UIDefaults defaults, final Properties uiProps) {

31
property-loader/src/main/java/com/github/weisj/darklaf/PropertyLoader.java

@ -44,6 +44,8 @@ import java.io.InputStream;
import java.text.AttributedCharacterIterator;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -130,6 +132,35 @@ public final class PropertyLoader {
}
}
public static void replaceProperties(final Map<Object, Object> properties,
final Predicate<Map.Entry<Object, Object>> predicate,
final Function<Map.Entry<Object, Object>, Object> mapper) {
replacePropertiesOfType(Object.class, properties, predicate, mapper);
}
public static <T> void replacePropertiesOfType(final Class<T> type,
final Map<Object, Object> properties,
final Function<T, T> mapper) {
replacePropertiesOfType(type, properties, e -> true, e -> mapper.apply(e.getValue()));
}
@SuppressWarnings("unchecked")
public static <T> void replacePropertiesOfType(final Class<T> type,
final Map<Object, Object> properties,
final Predicate<Map.Entry<Object, T>> predicate,
final Function<Map.Entry<Object, T>, T> mapper) {
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
if (type == Object.class || type.isAssignableFrom(entry.getValue().getClass())) {
if (predicate.test((Map.Entry<Object, T>) entry)) {
T newValue = mapper.apply((Map.Entry<Object, T>) entry);
if (newValue != null) {
entry.setValue(newValue);
}
}
}
}
}
private static String parseKey(final String key) {
if (addReferenceInfo) return key;
return key.startsWith(String.valueOf(REFERENCE_PREFIX)) ? key.substring(1) : key;

Loading…
Cancel
Save