| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import com.bulenkov.iconloader.IconLoader; |
| 20 |
|
| 21 |
import javax.swing.*; |
| 22 |
import javax.swing.border.Border; |
| 23 |
import javax.swing.plaf.UIResource; |
| 24 |
import java.awt.*; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class JBUI { |
| 30 |
private static float scaleFactor = 1.0f; |
| 31 |
|
| 32 |
static { |
| 33 |
calculateScaleFactor(); |
| 34 |
} |
| 35 |
|
| 36 |
private static void calculateScaleFactor() { |
| 37 |
if (SystemInfo.isMac) { |
| 38 |
scaleFactor = 1.0f; |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
if (System.getProperty("hidpi") != null && !"true".equalsIgnoreCase(System.getProperty("hidpi"))) { |
| 43 |
scaleFactor = 1.0f; |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
UIUtil.initSystemFontData(); |
| 48 |
Pair<String, Integer> fdata = UIUtil.getSystemFontData(); |
| 49 |
|
| 50 |
int size; |
| 51 |
if (fdata != null) { |
| 52 |
size = fdata.getSecond(); |
| 53 |
} else { |
| 54 |
size = Fonts.label().getSize(); |
| 55 |
} |
| 56 |
setScaleFactor(size / UIUtil.DEF_SYSTEM_FONT_SIZE); |
| 57 |
} |
| 58 |
|
| 59 |
public static void setScaleFactor(float scale) { |
| 60 |
final String value = System.getProperty("hidpi"); |
| 61 |
if (value != null && "false".equalsIgnoreCase(value)) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (scale < 1.25f) scale = 1.0f; |
| 66 |
else if (scale < 1.5f) scale = 1.25f; |
| 67 |
else if (scale < 1.75f) scale = 1.5f; |
| 68 |
else if (scale < 2f) scale = 1.75f; |
| 69 |
else scale = 2.0f; |
| 70 |
|
| 71 |
if (SystemInfo.isLinux && scale == 1.25f) { |
| 72 |
|
| 73 |
scale = 1f; |
| 74 |
} |
| 75 |
if (scaleFactor == scale) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
scaleFactor = scale; |
| 80 |
IconLoader.setScale(scale); |
| 81 |
} |
| 82 |
|
| 83 |
public static int scale(int i) { |
| 84 |
return Math.round(scaleFactor * i); |
| 85 |
} |
| 86 |
|
| 87 |
public static int scaleFontSize(int fontSize) { |
| 88 |
if (scaleFactor == 1.25f) return (int)(fontSize * 1.34f); |
| 89 |
if (scaleFactor == 1.75f) return (int)(fontSize * 1.67f); |
| 90 |
return scale(fontSize); |
| 91 |
} |
| 92 |
|
| 93 |
public static JBDimension size(int width, int height) { |
| 94 |
return new JBDimension(width, height); |
| 95 |
} |
| 96 |
|
| 97 |
public static JBDimension size(int widthAndHeight) { |
| 98 |
return new JBDimension(widthAndHeight, widthAndHeight); |
| 99 |
} |
| 100 |
|
| 101 |
public static JBDimension size(Dimension size) { |
| 102 |
if (size instanceof JBDimension) { |
| 103 |
final JBDimension jbSize = (JBDimension)size; |
| 104 |
if (jbSize.originalScale == scale(1f)) { |
| 105 |
return jbSize; |
| 106 |
} |
| 107 |
final JBDimension newSize = new JBDimension((int)(jbSize.width / jbSize.originalScale), (int)(jbSize.height / jbSize.originalScale)); |
| 108 |
return size instanceof UIResource ? newSize.asUIResource() : newSize; |
| 109 |
} |
| 110 |
return new JBDimension(size.width, size.height); |
| 111 |
} |
| 112 |
|
| 113 |
public static JBInsets insets(int top, int left, int bottom, int right) { |
| 114 |
return new JBInsets(top, left, bottom, right); |
| 115 |
} |
| 116 |
|
| 117 |
public static JBInsets insets(int all) { |
| 118 |
return insets(all, all, all, all); |
| 119 |
} |
| 120 |
|
| 121 |
public static JBInsets insets(int topBottom, int leftRight) { |
| 122 |
return insets(topBottom, leftRight, topBottom, leftRight); |
| 123 |
} |
| 124 |
|
| 125 |
public static JBInsets emptyInsets() { |
| 126 |
return new JBInsets(0, 0, 0, 0); |
| 127 |
} |
| 128 |
|
| 129 |
public static JBInsets insetsTop(int t) { |
| 130 |
return insets(t, 0, 0, 0); |
| 131 |
} |
| 132 |
|
| 133 |
public static JBInsets insetsLeft(int l) { |
| 134 |
return insets(0, l, 0, 0); |
| 135 |
} |
| 136 |
|
| 137 |
public static JBInsets insetsBottom(int b) { |
| 138 |
return insets(0, 0, b, 0); |
| 139 |
} |
| 140 |
|
| 141 |
public static JBInsets insetsRight(int r) { |
| 142 |
return insets(0, 0, 0, r); |
| 143 |
} |
| 144 |
|
| 145 |
public static EmptyIcon emptyIcon(int i) { |
| 146 |
return (EmptyIcon)EmptyIcon.create(scale(i)); |
| 147 |
} |
| 148 |
|
| 149 |
public static JBDimension emptySize() { |
| 150 |
return new JBDimension(0, 0); |
| 151 |
} |
| 152 |
|
| 153 |
public static float scale(float f) { |
| 154 |
return f * scaleFactor; |
| 155 |
} |
| 156 |
|
| 157 |
public static JBInsets insets(Insets insets) { |
| 158 |
return JBInsets.create(insets); |
| 159 |
} |
| 160 |
|
| 161 |
public static boolean isHiDPI() { |
| 162 |
return scaleFactor > 1.0f; |
| 163 |
} |
| 164 |
|
| 165 |
public static class Fonts { |
| 166 |
public static JBFont label() { |
| 167 |
return JBFont.create(UIManager.getFont("Label.font"), false); |
| 168 |
} |
| 169 |
|
| 170 |
public static JBFont label(float size) { |
| 171 |
return label().deriveFont(scale(size)); |
| 172 |
} |
| 173 |
|
| 174 |
public static JBFont smallFont() { |
| 175 |
return label().deriveFont(UIUtil.getFontSize(UIUtil.FontSize.SMALL)); |
| 176 |
} |
| 177 |
|
| 178 |
public static JBFont miniFont() { |
| 179 |
return label().deriveFont(UIUtil.getFontSize(UIUtil.FontSize.MINI)); |
| 180 |
} |
| 181 |
|
| 182 |
public static JBFont create(String fontFamily, int size) { |
| 183 |
return JBFont.create(new Font(fontFamily, Font.PLAIN, size)); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public static class Borders { |
| 188 |
public static JBEmptyBorder empty(int top, int left, int bottom, int right) { |
| 189 |
return new JBEmptyBorder(top, left, bottom, right); |
| 190 |
} |
| 191 |
|
| 192 |
public static JBEmptyBorder empty(int topAndBottom, int leftAndRight) { |
| 193 |
return empty(topAndBottom, leftAndRight, topAndBottom, leftAndRight); |
| 194 |
} |
| 195 |
|
| 196 |
public static JBEmptyBorder emptyTop(int offset) { |
| 197 |
return empty(offset, 0, 0, 0); |
| 198 |
} |
| 199 |
|
| 200 |
public static JBEmptyBorder emptyLeft(int offset) { |
| 201 |
return empty(0, offset, 0, 0); |
| 202 |
} |
| 203 |
|
| 204 |
public static JBEmptyBorder emptyBottom(int offset) { |
| 205 |
return empty(0, 0, offset, 0); |
| 206 |
} |
| 207 |
|
| 208 |
public static JBEmptyBorder emptyRight(int offset) { |
| 209 |
return empty(0, 0, 0, offset); |
| 210 |
} |
| 211 |
|
| 212 |
public static JBEmptyBorder empty() { |
| 213 |
return empty(0, 0, 0, 0); |
| 214 |
} |
| 215 |
|
| 216 |
public static Border empty(int offsets) { |
| 217 |
return empty(offsets, offsets, offsets, offsets); |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
} |