Debian Codegraph @

src/com/bulenkov/iconloader/util/UIUtil.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader.util;
18
19 import com.bulenkov.iconloader.IsRetina;
20 import com.bulenkov.iconloader.JBHiDPIScaledImage;
21 import com.bulenkov.iconloader.RetinaImage;
22 import org.jetbrains.annotations.Nullable;
23
24 import javax.swing.*;
25 import java.awt.*;
26 import java.awt.image.BufferedImage;
27 import java.awt.image.ImageObserver;
28 import java.lang.reflect.Field;
29
30 /**
31 * @author Konstantin Bulenkov
32 */
33 public class UIUtil {
34 public static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);
35 private static volatile Pair<String, Integer> ourSystemFontData;
36 public static final float DEF_SYSTEM_FONT_SIZE = 12f; // TODO: consider 12 * 1.33 to compensate JDK's 72dpi font scale
37
38 public static <T extends JComponent> T findComponentOfType(JComponent parent, Class<T> cls) {
39 if (parent == null || cls.isAssignableFrom(parent.getClass())) {
40 @SuppressWarnings({"unchecked"}) final T t = (T) parent;
41 return t;
42 }
43 for (Component component : parent.getComponents()) {
44 if (component instanceof JComponent) {
45 T comp = findComponentOfType((JComponent) component, cls);
46 if (comp != null) return comp;
47 }
48 }
49 return null;
50 }
51
52 @Nullable
53 public static Pair<String, Integer> getSystemFontData() {
54 return ourSystemFontData;
55 }
56
57 public static <T> T getParentOfType(Class<? extends T> cls, Component c) {
58 Component eachParent = c;
59 while (eachParent != null) {
60 if (cls.isAssignableFrom(eachParent.getClass())) {
61 @SuppressWarnings({"unchecked"}) final T t = (T) eachParent;
62 return t;
63 }
64
65 eachParent = eachParent.getParent();
66 }
67
68 return null;
69 }
70
71 public static boolean isAppleRetina() {
72 return isRetina() && SystemInfo.isAppleJvm;
73 }
74
75 public static Color getControlColor() {
76 return UIManager.getColor("control");
77 }
78
79 public static Color getPanelBackground() {
80 return UIManager.getColor("Panel.background");
81 }
82
83 public static boolean isUnderDarcula() {
84 return UIManager.getLookAndFeel().getName().equals("Darcula");
85 }
86
87 public static Color getListBackground() {
88 return UIManager.getColor("List.background");
89 }
90
91 public static Color getListForeground() {
92 return UIManager.getColor("List.foreground");
93 }
94
95 public static Color getLabelForeground() {
96 return UIManager.getColor("Label.foreground");
97 }
98
99 public static Color getTextFieldBackground() {
100 return UIManager.getColor("TextField.background");
101 }
102
103 public static Color getTreeSelectionForeground() {
104 return UIManager.getColor("Tree.selectionForeground");
105 }
106
107 public static Color getTreeForeground() {
108 return UIManager.getColor("Tree.foreground");
109 }
110
111 private static final Color DECORATED_ROW_BG_COLOR = new DoubleColor(new Color(242, 245, 249), new Color(65, 69, 71));
112
113 public static Color getDecoratedRowColor() {
114 return DECORATED_ROW_BG_COLOR;
115 }
116
117 public static Color getTreeSelectionBackground(boolean focused) {
118 return focused ? getTreeSelectionBackground() : getTreeUnfocusedSelectionBackground();
119 }
120
121 private static Color getTreeSelectionBackground() {
122 return UIManager.getColor("Tree.selectionBackground");
123 }
124
125 public static Color getTreeUnfocusedSelectionBackground() {
126 Color background = getTreeTextBackground();
127 return ColorUtil.isDark(background) ? new DoubleColor(Gray._30, new Color(13, 41, 62)) : Gray._212;
128 }
129
130 public static Color getTreeTextBackground() {
131 return UIManager.getColor("Tree.textBackground");
132 }
133
134 public static void drawImage(Graphics g, Image image, int x, int y, ImageObserver observer) {
135 if (image instanceof JBHiDPIScaledImage) {
136 final Graphics2D newG = (Graphics2D) g.create(x, y, image.getWidth(observer), image.getHeight(observer));
137 newG.scale(0.5, 0.5);
138 Image img = ((JBHiDPIScaledImage) image).getDelegate();
139 if (img == null) {
140 img = image;
141 }
142 newG.drawImage(img, 0, 0, observer);
143 newG.scale(1, 1);
144 newG.dispose();
145 } else {
146 g.drawImage(image, x, y, observer);
147 }
148 }
149
150
151 private static final Ref<Boolean> ourRetina = Ref.create(SystemInfo.isMac ? null : false);
152
153 public static boolean isRetina() {
154 synchronized (ourRetina) {
155 if (ourRetina.isNull()) {
156 ourRetina.set(false); // in case HiDPIScaledImage.drawIntoImage is not called for some reason
157
158 if (SystemInfo.isJavaVersionAtLeast("1.6.0_33") && SystemInfo.isAppleJvm) {
159 if (!"false".equals(System.getProperty("ide.mac.retina"))) {
160 ourRetina.set(IsRetina.isRetina());
161 return ourRetina.get();
162 }
163 } else if (SystemInfo.isJavaVersionAtLeast("1.7.0_40") && (SystemInfo.isOracleJvm || SystemInfo.isJetbrainsJvm)) {
164 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
165 final GraphicsDevice device = env.getDefaultScreenDevice();
166 try {
167 Field field = device.getClass().getDeclaredField("scale");
168 if (field != null) {
169 field.setAccessible(true);
170 Object scale = field.get(device);
171 if (scale instanceof Integer && (Integer) scale == 2) {
172 ourRetina.set(true);
173 return true;
174 }
175 }
176 } catch (Exception ignore) {
177 }
178 }
179 ourRetina.set(false);
180 }
181
182 return ourRetina.get();
183 }
184 }
185
186 public static BufferedImage createImage(int width, int height, int type) {
187 if (isRetina()) {
188 return RetinaImage.create(width, height, type);
189 }
190 //noinspection UndesirableClassUsage
191 return new BufferedImage(width, height, type);
192 }
193
194
195 private static final GrayFilter DEFAULT_GRAY_FILTER = new GrayFilter(true, 65);
196 private static final GrayFilter DARCULA_GRAY_FILTER = new GrayFilter(true, 30);
197
198 public static GrayFilter getGrayFilter() {
199 return isUnderDarcula() ? DARCULA_GRAY_FILTER : DEFAULT_GRAY_FILTER;
200 }
201
202 public static Font getLabelFont() {
203 return UIManager.getFont("Label.font");
204 }
205
206 public static float getFontSize(FontSize size) {
207 int defSize = getLabelFont().getSize();
208 switch (size) {
209 case SMALL:
210 return Math.max(defSize - JBUI.scale(2f), JBUI.scale(11f));
211 case MINI:
212 return Math.max(defSize - JBUI.scale(4f), JBUI.scale(9f));
213 default:
214 return defSize;
215 }
216 }
217
218 public enum FontSize {NORMAL, SMALL, MINI}
219
220 public static void initSystemFontData() {
221 if (ourSystemFontData != null) return;
222
223 // With JB Linux JDK the label font comes properly scaled based on Xft.dpi settings.
224 Font font = getLabelFont();
225
226 Float forcedScale = null;
227 if (Registry.is("ide.ui.scale.override")) {
228 forcedScale = Registry.getFloat("ide.ui.scale");
229 }
230 else if (SystemInfo.isLinux && !SystemInfo.isJetbrainsJvm) {
231 // With Oracle JDK: derive scale from X server DPI
232 float scale = getScreenScale();
233 if (scale > 1f) {
234 forcedScale = scale;
235 }
236 // Or otherwise leave the detected font. It's undetermined if it's scaled or not.
237 // If it is (likely with GTK DE), then the UI scale will be derived from it,
238 // if it's not, then IDEA will start unscaled. This lets the users of GTK DEs
239 // not to bother about X server DPI settings. Users of other DEs (like KDE)
240 // will have to set X server DPI to meet their display.
241 }
242 else if (SystemInfo.isWindows) {
243 //noinspection HardCodedStringLiteral
244 Font winFont = (Font)Toolkit.getDefaultToolkit().getDesktopProperty("win.messagebox.font");
245 if (winFont != null) {
246 font = winFont; // comes scaled
247 }
248 }
249 if (forcedScale != null) {
250 // With forced scale, we derive font from a hard-coded value as we cannot be sure
251 // the system font comes unscaled.
252 font = font.deriveFont(DEF_SYSTEM_FONT_SIZE * forcedScale.floatValue());
253 }
254 ourSystemFontData = Pair.create(font.getName(), font.getSize());
255 }
256
257 private static float getScreenScale() {
258 int dpi = 96;
259 try {
260 dpi = Toolkit.getDefaultToolkit().getScreenResolution();
261 } catch (HeadlessException e) {
262 }
263 float scale = 1f;
264 if (dpi < 120) scale = 1f;
265 else if (dpi < 144) scale = 1.25f;
266 else if (dpi < 168) scale = 1.5f;
267 else if (dpi < 192) scale = 1.75f;
268 else scale = 2f;
269
270 return scale;
271 }
272 }