IDEA-200179 Make *.propertyName patterns from UIThemes work with named colors

This commit is contained in:
Konstantin Bulenkov
2018-10-21 23:43:04 +02:00
parent 0e957b8631
commit 35b1c1d1c8
2 changed files with 53 additions and 2 deletions
@@ -232,6 +232,7 @@ public class UITheme {
if (key.startsWith("*.")) {
String tail = key.substring(1);
Object finalValue = value;
addPattern(key, value, defaults);
//please DO NOT stream on UIDefaults directly
((UIDefaults)defaults.clone()).keySet().stream()
@@ -243,6 +244,18 @@ public class UITheme {
}
}
private static void addPattern(String key, Object value, UIDefaults defaults) {
Object o = defaults.get("*");
if (! (o instanceof Map)) {
o = new HashMap<String, Object>();
defaults.put("*", o);
}
Map map = (Map)o;
if (key != null && key.startsWith("*.")) {
map.put(key.substring(2), value);
}
}
public static Object parseValue(String key, @NotNull String value) {
if ("null".equals(value)) {
return null;
+40 -2
View File
@@ -2,7 +2,6 @@
package com.intellij.ui;
import com.intellij.util.NotNullProducer;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.hash.HashMap;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
@@ -15,6 +14,8 @@ import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.util.Map;
import static com.intellij.util.ObjectUtils.notNull;
/**
* @author Konstantin Bulenkov
*/
@@ -53,11 +54,48 @@ public class JBColor extends Color {
@NotNull
@Override
public Color produce() {
return ObjectUtils.notNull(UIManager.getColor(propertyName), defaultColor);
Color color = notNull(findPatternMatch(propertyName),
notNull(UIManager.getColor(propertyName), defaultColor));
if (UIManager.get(propertyName) == null) {
UIManager.put(propertyName, color);
}
return color;
}
});
}
// Let's find if namedColor can be overridden by *.propertyName rule in ui theme and apply it
// We need to cache calculated results. Cache and rules will be reset after LaF change
private static Color findPatternMatch(String name) {
Object value = UIManager.get("*");
if (value instanceof Map) {
Color color = null;
Map<?,?> map = (Map<?, ?>)value;
Object o = UIManager.get("*cache");
if (! (o instanceof Map)) {
o = new java.util.HashMap<String, Color>();
UIManager.put("*cache", o);
}
Map<String,Color> cache = (Map)o;
if (cache.containsKey(name)) {
return cache.get(name);
}
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (entry.getKey() instanceof String && name.endsWith(((String)entry.getKey()))) {
Object result = map.get(entry.getKey());
if (result instanceof Color) {
color = (Color)result;
break;
}
}
}
cache.put(name, color);
return color;
}
return null;
}
@NotNull
public static Color link() {
return new JBColor(new NotNullProducer<Color>() {