forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCustomIconMaker.java
143 lines (112 loc) · 4.08 KB
/
CustomIconMaker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.utils;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.ui.LayeredIcon;
import com.intellij.util.ui.GraphicsUtil;
import com.intellij.util.ui.UIUtil;
import icons.FlutterIcons;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;
public class CustomIconMaker {
private static final Color normalColor = ColorUtil.fromHex("231F20");
private final Map<String, Icon> iconCache = new HashMap<>();
public CustomIconMaker() {
}
public Icon getCustomIcon(String fromText) {
return getCustomIcon(fromText, IconKind.kClass, false);
}
public Icon getCustomIcon(String fromText, IconKind kind) {
return getCustomIcon(fromText, kind, false);
}
public Icon getCustomIcon(String fromText, IconKind kind, boolean isAbstract) {
if (StringUtil.isEmpty(fromText)) {
return null;
}
final String text = fromText.toUpperCase().substring(0, 1);
final String mapKey = text + "_" + kind.name + "_" + isAbstract;
if (!iconCache.containsKey(mapKey)) {
final Icon baseIcon = isAbstract ? kind.abstractIcon : kind.icon;
final Icon icon = new LayeredIcon(baseIcon, new Icon() {
public void paintIcon(Component c, Graphics g, int x, int y) {
final Graphics2D g2 = (Graphics2D)g.create();
try {
GraphicsUtil.setupAAPainting(g2);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setColor(normalColor);
final Font font = UIUtil.getFont(UIUtil.FontSize.MINI, UIUtil.getTreeFont());
g2.setFont(font);
final Rectangle2D bounds = g2.getFontMetrics().getStringBounds(text, g2);
final float offsetX = (getIconWidth() - (float)bounds.getWidth()) / 2.0f;
// Some black magic here for vertical centering.
final float offsetY = getIconHeight() - ((getIconHeight() - (float)bounds.getHeight()) / 2.0f) - 2.0f;
g2.drawString(text, x + offsetX, y + offsetY);
}
finally {
g2.dispose();
}
}
public int getIconWidth() {
return baseIcon != null ? baseIcon.getIconWidth() : 13;
}
public int getIconHeight() {
return baseIcon != null ? baseIcon.getIconHeight() : 13;
}
});
iconCache.put(mapKey, icon);
}
return iconCache.get(mapKey);
}
@Nullable
public Icon fromWidgetName(String name) {
if (name == null) {
return null;
}
final boolean isPrivate = name.startsWith("_");
while (!name.isEmpty() && !Character.isAlphabetic(name.charAt(0))) {
name = name.substring(1);
}
if (name.isEmpty()) {
return null;
}
return getCustomIcon(name, isPrivate ? CustomIconMaker.IconKind.kMethod : CustomIconMaker.IconKind.kClass);
}
public Icon fromInfo(String name) {
if (name == null) {
return null;
}
if (name.isEmpty()) {
return null;
}
return getCustomIcon(name, CustomIconMaker.IconKind.kInfo);
}
public enum IconKind {
kClass("class", FlutterIcons.CustomClass, FlutterIcons.CustomClassAbstract),
kField("fields", FlutterIcons.CustomFields),
kInterface("interface", FlutterIcons.CustomInterface),
kMethod("method", FlutterIcons.CustomMethod, FlutterIcons.CustomMethodAbstract),
kProperty("property", FlutterIcons.CustomProperty),
kInfo("info", FlutterIcons.CustomInfo);
public final String name;
public final Icon icon;
public final Icon abstractIcon;
IconKind(String name, Icon icon) {
this.name = name;
this.icon = icon;
this.abstractIcon = icon;
}
IconKind(String name, Icon icon, Icon abstractIcon) {
this.name = name;
this.icon = icon;
this.abstractIcon = abstractIcon;
}
}
}