Skip to content

Commit

Permalink
Fix theme properties not being displayed in the generated UITable.
Browse files Browse the repository at this point in the history
Improve sorting of values.
  • Loading branch information
weisJ committed May 27, 2021
1 parent b01ef6a commit 3f16eec
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ private void loadThemeDefaults(final Theme currentTheme, final UIDefaults defaul

private void backupAccentColors(final Properties uiProps) {
uiProps.put(ACCENT_COLOR_BACKUP_KEY, Objects.requireNonNull(
uiProps.get(PropertyLoader.asKey(ACCENT_COLOR_SOURCE_KEY)), ACCENT_COLOR_SOURCE_KEY));
uiProps.get(ACCENT_COLOR_SOURCE_KEY), ACCENT_COLOR_SOURCE_KEY));
uiProps.put(SELECTION_COLOR_BACKUP_KEY, Objects.requireNonNull(
uiProps.get(PropertyLoader.asKey(SELECTION_COLOR_SOURCE_KEY)), SELECTION_COLOR_SOURCE_KEY));
uiProps.get(SELECTION_COLOR_SOURCE_KEY), SELECTION_COLOR_SOURCE_KEY));
}

private void initAccentProperties(final Theme currentTheme, final Properties uiProps) {
Expand Down
65 changes: 32 additions & 33 deletions core/src/test/java/documentation/CreateUITable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;
Expand All @@ -51,9 +50,9 @@
import com.github.weisj.darklaf.parser.PrimitiveParser;
import com.github.weisj.darklaf.theme.Theme;
import com.github.weisj.darklaf.util.ImageUtil;
import com.github.weisj.darklaf.util.LogUtil;
import com.github.weisj.darklaf.util.StringUtil;
import com.github.weisj.darklaf.util.SystemInfo;
import com.github.weisj.darklaf.util.Types;
import com.kitfox.svg.LinearGradient;
import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGElement;
Expand All @@ -62,8 +61,6 @@

public class CreateUITable {

private static final Logger LOGGER = LogUtil.getLogger(CreateUITable.class);

private static final int SAMPLE_WIDTH = 150;
private static final int SAMPLE_HEIGHT = 25;

Expand All @@ -78,7 +75,6 @@ public class CreateUITable {

public static void main(final String[] args) throws IOException {
for (Theme theme : LafManager.getRegisteredThemes()) {
LOGGER.info("Creating defaults table for " + theme.getDisplayName());
createThemeDefaultsPage(theme);
}
}
Expand All @@ -103,16 +99,21 @@ public static void createThemeDefaultsPage(final Theme theme) throws IOException
}
}

private String getGroup(final Map.Entry<Object, Object> entry) {
Parser.DebugParseResult value = Types.safeCast(entry.getValue(), Parser.DebugParseResult.class);
String s = value != null ? value.originalKey : entry.getKey().toString();
if (s.startsWith(THEME_GROUP)) return THEME_GROUP;
if (s.contains(".")) return s.split("\\.")[0];
if (s.endsWith("UI")) return s.substring(0, s.length() - 2);
return MISC_GROUP + s;
}

private String createTables(final Theme theme, final int ident) {
UIDefaults defaults = setupThemeDefaults(theme);

Set<String> groups = defaults.keySet().stream().map(key -> {
String s = key.toString();
if (s.startsWith(THEME_GROUP)) return THEME_GROUP;
if (s.contains(".")) return s.split("\\.")[0];
if (s.endsWith("UI")) return s.substring(0, s.length() - 2);
return MISC_GROUP + s;
}).collect(Collectors.toSet());
Set<String> groups = defaults.entrySet().stream()
.map(this::getGroup)
.collect(Collectors.toSet());

Set<String> miscKeys = groups.stream()
.filter(s -> s.startsWith(MISC_GROUP))
Expand Down Expand Up @@ -148,18 +149,20 @@ public Theme getTheme() {
private void appendGroup(final int ident, final UIDefaults defaults, final StringBuilder builder,
final String group, final String heading) {
builder.append(StringUtil.repeat(IDENT, ident)).append("<h3>").append(heading).append("</h3>\n");
Set<Map.Entry<Object, Object>> values = defaults.entrySet().stream().filter(entry -> {
String key = entry.getKey().toString();
if (key.startsWith("%")) return true;
if (key.endsWith("UI")) return key.substring(0, key.length() - 2).equals(group);
if (key.contains(".")) return key.split("\\.")[0].equals(group);
return key.equals(group);
}).collect(Collectors.toSet());
Set<Map.Entry<Object, Object>> values = defaults.entrySet().stream()
.filter(entry -> getGroup(entry).equals(group)).collect(Collectors.toSet());
appendTable(builder, values, ident);
values.forEach(entry -> defaults.remove(entry.getKey()));
builder.append('\n');
}

private Object unwrap(final Object o) {
if (o instanceof ParseResult) {
return ((ParseResult) o).result;
}
return o;
}

private void appendTable(final StringBuilder builder, final Set<Map.Entry<Object, Object>> values,
final int ident) {
builder.append(StringUtil.repeat(IDENT, ident)).append("<table>\n");
Expand All @@ -172,16 +175,11 @@ private void appendTable(final StringBuilder builder, final Set<Map.Entry<Object
values.stream().filter(entry -> entry.getKey().toString().endsWith("UI"))
.forEach(entry -> appendRow(builder, entry, ident + 1));
values.stream().filter(entry -> !entry.getKey().toString().endsWith("UI")).sorted((o1, o2) -> {
int res = o1.getValue().getClass().getSimpleName().compareTo(o2.getValue().getClass().getSimpleName());
Object v1 = unwrap(o1.getValue());
Object v2 = unwrap(o2.getValue());
int res = v1.getClass().getSimpleName().compareTo(v2.getClass().getSimpleName());
if (res != 0) return res;
Object val1 = o1.getValue();
Object val2 = o2.getValue();
if (val1 instanceof Comparable) {
// noinspection unchecked
return ((Comparable<Object>) val1).compareTo(val2);
} else {
return val1.toString().compareTo(val2.toString());
}
return o1.getKey().toString().compareTo(o2.getKey().toString());
}).forEach(entry -> appendRow(builder, entry, ident + 1));
builder.append(StringUtil.repeat(IDENT, ident)).append("</table>\n");
}
Expand All @@ -207,9 +205,7 @@ private void appendRow(final StringBuilder builder, final Map.Entry<Object, Obje
appendData(builder, debugResult.referenceKey, ident + 1); // Reference
builder.append(parsePreview(key, debugResult.result, ident + 1));
} else {
if (value instanceof ParseResult) {
value = ((ParseResult) value).result;
}
value = unwrap(value);
appendData(builder, parseValue(value), ident + 1); // Value
appendData(builder, "", ident + 1); // Reference
builder.append(parsePreview(key, value, ident + 1));
Expand Down Expand Up @@ -261,7 +257,7 @@ private String parseImage(final String key, final Object value, final int ident)
return StringUtil.repeat(IDENT, ident) + "<td></td>\n";
}
return StringUtil.repeat(IDENT, ident) + String
.format("<td style=\"padding:0px\" align=\"center\"><img src=\"%s\" alt=\"%s\"></td>\n", path, key);
.format("<td style=\"padding:0\" align=\"center\"><img src=\"%s\" alt=\"%s\"></td>\n", path, key);
}

private String createImage(final Object value, final String name, final Dimension size) throws IOException {
Expand Down Expand Up @@ -314,7 +310,10 @@ private String parseSVGIcon(final DarkSVGIcon value, final int ident) {
String id = ((LinearGradient) child).getId();
String match = "=\"url\\(#" + id + "\\)\"";
String fillReplacement = "fill=\"#" + ColorUtil.toHex(color) + "\"";
if (opacity != 1) fillReplacement += " fill-opacity=\"" + opacity + "\"";
if (opacity != 1) {
fillReplacement += " fill-opacity=\"" + opacity + "\"";
svg = svg.replaceAll("fill-opacity=\"[^\"]*\"", "");
}
svg = svg.replaceAll("fill" + match, fillReplacement);

String strokeReplacement = "stroke=\"#" + ColorUtil.toHex(color) + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,4 @@ public static <T> void replacePropertyEntriesOfType(final Class<T> type, final M
public static String getReferencePrefix() {
return String.valueOf(REFERENCE_PREFIX);
}

public static String asKey(final String key) {
// if (Parser.isDebugMode()) return getReferencePrefix() + key;
return key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ public static ParseResult createParseResult(final String key, final String value

public static class DebugParseResult extends ParseResult {

public String originalKey;
public String originalValue;
public String referenceKey;

public DebugParseResult(final String key, final String value) {
super(key, value);
this.originalKey = key;
this.originalValue = value;
}
}
}

0 comments on commit 3f16eec

Please sign in to comment.