3 import java.awt.HeadlessException;
4 import java.util.Locale;
7 public class HiDPISetting
9 private static final int hidpiThreshold = 160;
11 private static final int hidpiMultiThreshold = 240;
13 private static final int bigScreenThreshold = 1400;
15 public static final String scalePropertyName = "sun.java2d.uiScale";
17 private static final boolean isLinux;
19 // private static final boolean isAMac;
21 // private static final boolean isWindows;
23 public static final String setHiDPIPropertyName = "setHiDPI";
25 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
27 private static boolean setHiDPI = false;
29 private static int setHiDPIScale = 0;
31 public static int dpi = 0;
33 public static int mindimension = 0;
35 public static int width = 0;
37 public static int scale = 0;
39 private static boolean doneInit = false;
41 private static boolean allowScalePropertyArg = false;
43 private static ScreenInfo screenInfo = new ScreenInfo();
47 String system = System.getProperty("os.name") == null ? null
48 : System.getProperty("os.name").toLowerCase(Locale.ROOT);
51 isLinux = system.indexOf("linux") > -1;
52 // isAMac = system.indexOf("mac") > -1;
53 // isWindows = system.indexOf("windows") > -1;
58 // isAMac = isWindows = false;
62 private static void init()
69 // get and use command line property values first
70 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
71 setHiDPI = setHiDPIProperty != null
72 && setHiDPIProperty.equalsIgnoreCase("true");
74 String setHiDPIScaleProperty = System
75 .getProperty(setHiDPIScalePropertyName);
76 if (setHiDPIScaleProperty != null)
80 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
81 } catch (NumberFormatException e)
83 System.err.println(setHiDPIScalePropertyName + " property give ("
84 + setHiDPIScaleProperty + ") but not parseable as integer");
87 if (setHiDPI && setHiDPIScale > 0)
89 setHiDPIScale(setHiDPIScale);
93 // check to see if the scale property has already been set by something else
95 String existingProperty = System.getProperty(scalePropertyName);
96 if (existingProperty != null)
100 int existingPropertyVal = Integer.parseInt(existingProperty);
101 System.out.println("Existing " + scalePropertyName + " is "
102 + existingPropertyVal);
103 if (existingPropertyVal > 1)
105 setHiDPIScale(existingPropertyVal);
108 } catch (NumberFormatException e)
110 System.out.println("Could not convert property " + scalePropertyName
111 + " vale '" + existingProperty + "' to number");
115 // Try and auto guess a good scale based on reported DPI (not trustworthy)
116 // and screen resolution (more trustworthy)
119 screenInfo = getScreenInfo();
122 dpi = screenInfo.getScreenResolution();
123 } catch (HeadlessException e)
125 System.err.println("Cannot get screen resolution: " + e.getMessage());
128 // try and get screen size height and width
131 int height = screenInfo.getScreenHeight();
132 int width = screenInfo.getScreenWidth();
133 // using mindimension in case of portrait screens
134 mindimension = Math.min(height, width);
135 } catch (HeadlessException e)
138 "Cannot get screen size height and width:" + e.getMessage());
141 // attempt at a formula for scaling based on screen dpi and mindimension.
142 // scale will be an integer >=1. This formula is based on some testing and
145 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
146 // of hidpiMultiThreshold (else scale=1)
147 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
148 int dpiScale = (dpi - hidpiThreshold > 0)
149 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
152 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
154 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
155 // as dpiScale often misreported)
156 int autoScale = Math.max(dpiScale, dimensionScale);
158 // only make an automatic change if scale is changed and other conditions
159 // (OS is linux) apply, or if setHiDPI has been specified
160 if ((autoScale > 1 && isLinux) || setHiDPI)
162 setHiDPIScale(autoScale);
166 // looks like we're not doing any scaling
170 public static void setHiDPIScale(int s)
173 allowScalePropertyArg = true;
177 public static String getScalePropertyArg(int s)
179 return "-D" + scalePropertyName + "=" + String.valueOf(s);
182 public static String getScalePropertyArg()
185 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
186 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
189 public static void clear()
198 allowScalePropertyArg = false;
201 public static void setScreenInfo(ScreenInfo si)
206 public static ScreenInfo getScreenInfo()
208 if (screenInfo == null)
210 screenInfo = new ScreenInfo();