3 import java.awt.HeadlessException;
5 public class HiDPISetting
7 private static final int hidpiThreshold = 160;
9 private static final int hidpiMultiThreshold = 240;
11 private static final int bigScreenThreshold = 1400;
13 public static final String scalePropertyName = "sun.java2d.uiScale";
15 private static final boolean isLinux;
17 // private static final boolean isAMac;
19 // private static final boolean isWindows;
21 public static final String setHiDPIPropertyName = "setHiDPI";
23 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
25 private static boolean setHiDPI = false;
27 private static int setHiDPIScale = 0;
29 public static int dpi = 0;
31 public static int mindimension = 0;
33 public static int width = 0;
35 public static int scale = 0;
37 private static boolean doneInit = false;
39 private static boolean allowScalePropertyArg = false;
41 private static ScreenInfo screenInfo = new ScreenInfo();
45 String system = System.getProperty("os.name") == null ? null
46 : System.getProperty("os.name").toLowerCase();
49 isLinux = system.indexOf("linux") > -1;
50 // isAMac = system.indexOf("mac") > -1;
51 // isWindows = system.indexOf("windows") > -1;
56 // isAMac = isWindows = false;
60 private static void init()
67 // get and use command line property values first
68 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
69 setHiDPI = setHiDPIProperty != null
70 && setHiDPIProperty.equalsIgnoreCase("true");
72 String setHiDPIScaleProperty = System
73 .getProperty(setHiDPIScalePropertyName);
74 if (setHiDPIScaleProperty != null)
78 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
79 } catch (NumberFormatException e)
81 System.err.println(setHiDPIScalePropertyName + " property give ("
82 + setHiDPIScaleProperty + ") but not parseable as integer");
85 if (setHiDPI && setHiDPIScale > 0)
87 setHiDPIScale(setHiDPIScale);
91 // check to see if the scale property has already been set by something else
93 String existingProperty = System.getProperty(scalePropertyName);
94 if (existingProperty != null)
98 int existingPropertyVal = Integer.parseInt(existingProperty);
99 System.out.println("Existing " + scalePropertyName + " is "
100 + existingPropertyVal);
101 if (existingPropertyVal > 1)
103 setHiDPIScale(existingPropertyVal);
106 } catch (NumberFormatException e)
108 System.out.println("Could not convert property " + scalePropertyName
109 + " vale '" + existingProperty + "' to number");
113 // Try and auto guess a good scale based on reported DPI (not trustworthy)
114 // and screen resolution (more trustworthy)
117 screenInfo = getScreenInfo();
120 dpi = screenInfo.getScreenResolution();
121 } catch (HeadlessException e)
123 System.err.println("Cannot get screen resolution: " + e.getMessage());
126 // try and get screen size height and width
129 int height = screenInfo.getScreenHeight();
130 int width = screenInfo.getScreenWidth();
131 // using mindimension in case of portrait screens
132 mindimension = Math.min(height, width);
133 } catch (HeadlessException e)
136 "Cannot get screen size height and width:" + e.getMessage());
139 // attempt at a formula for scaling based on screen dpi and mindimension.
140 // scale will be an integer >=1. This formula is based on some testing and
143 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
144 // of hidpiMultiThreshold (else scale=1)
145 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
146 int dpiScale = (dpi - hidpiThreshold > 0)
147 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
150 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
152 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
153 // as dpiScale often misreported)
154 int autoScale = Math.max(dpiScale, dimensionScale);
156 // only make an automatic change if scale is changed and other conditions
157 // (OS is linux) apply, or if setHiDPI has been specified
158 if ((autoScale > 1 && isLinux) || setHiDPI)
160 setHiDPIScale(autoScale);
164 // looks like we're not doing any scaling
168 public static void setHiDPIScale(int s)
171 allowScalePropertyArg = true;
175 public static String getScalePropertyArg(int s)
177 return "-D" + scalePropertyName + "=" + String.valueOf(s);
180 public static String getScalePropertyArg()
183 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
184 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
187 public static void clear()
196 allowScalePropertyArg = false;
199 public static void setScreenInfo(ScreenInfo si)
204 public static ScreenInfo getScreenInfo()
206 if (screenInfo == null)
208 screenInfo = new ScreenInfo();