From 35b1c1d1c8bef90b208cf237fbd86dba9ba9fdfa Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Sun, 21 Oct 2018 23:07:59 +0200 Subject: [PATCH] IDEA-200179 Make *.propertyName patterns from UIThemes work with named colors --- .../src/com/intellij/ide/ui/UITheme.java | 13 ++++++ .../util/src/com/intellij/ui/JBColor.java | 42 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/ui/UITheme.java b/platform/platform-impl/src/com/intellij/ide/ui/UITheme.java index 74dfe39d8651..61b33b38d5bf 100644 --- a/platform/platform-impl/src/com/intellij/ide/ui/UITheme.java +++ b/platform/platform-impl/src/com/intellij/ide/ui/UITheme.java @@ -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(); + 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; diff --git a/platform/util/src/com/intellij/ui/JBColor.java b/platform/util/src/com/intellij/ui/JBColor.java index e0e1e5d5e6fd..28af720d9a5b 100644 --- a/platform/util/src/com/intellij/ui/JBColor.java +++ b/platform/util/src/com/intellij/ui/JBColor.java @@ -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(); + UIManager.put("*cache", o); + } + Map 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() {