2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.Locale;
25 import java.awt.HeadlessException;
27 public class HiDPISetting
29 private static final int hidpiThreshold = 160;
31 private static final int hidpiMultiThreshold = 240;
33 private static final int bigScreenThreshold = 1400;
35 public static final String scalePropertyName = "sun.java2d.uiScale";
37 private static final boolean isLinux;
39 // private static final boolean isAMac;
41 // private static final boolean isWindows;
43 public static final String setHiDPIPropertyName = "setHiDPI";
45 public static final String setHiDPIScalePropertyName = "setHiDPIScale";
47 private static boolean setHiDPI = false;
49 private static int setHiDPIScale = 0;
51 public static int dpi = 0;
53 public static int mindimension = 0;
55 public static int width = 0;
57 public static int scale = 0;
59 public final static int MAX_SCALE = 8;
61 private static boolean doneInit = false;
63 private static boolean allowScalePropertyArg = false;
65 private static ScreenInfo screenInfo = new ScreenInfo();
69 String system = System.getProperty("os.name") == null ? null
70 : System.getProperty("os.name").toLowerCase(Locale.ROOT);
73 isLinux = system.indexOf("linux") > -1;
74 // isAMac = system.indexOf("mac") > -1;
75 // isWindows = system.indexOf("windows") > -1;
80 // isAMac = isWindows = false;
84 private static void init()
91 // get and use command line property values first
92 String setHiDPIProperty = System.getProperty(setHiDPIPropertyName);
93 boolean setHiDPIPropertyBool = Boolean.parseBoolean(setHiDPIProperty);
95 // allow -DsetHiDPI=false to turn off HiDPI scaling
96 if (setHiDPIProperty != null && !setHiDPIPropertyBool)
103 setHiDPI = setHiDPIProperty != null && setHiDPIPropertyBool;
105 String setHiDPIScaleProperty = System
106 .getProperty(setHiDPIScalePropertyName);
107 if (setHiDPIScaleProperty != null)
111 setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty);
112 // if setHiDPIScale property is validly set and setHiDPI property wasn't
113 // attempted to be set we assume setHiDPIScale to be true
114 if (setHiDPIProperty == null)
118 } catch (NumberFormatException e)
120 System.err.println(setHiDPIScalePropertyName + " property give ("
121 + setHiDPIScaleProperty + ") but not parseable as integer");
124 if (setHiDPI && setHiDPIScale > 0)
126 setHiDPIScale(setHiDPIScale);
130 // check to see if the scale property has already been set by something else
132 String existingProperty = System.getProperty(scalePropertyName);
133 if (existingProperty != null)
137 int existingPropertyVal = Integer.parseInt(existingProperty);
138 System.out.println("Existing " + scalePropertyName + " is "
139 + existingPropertyVal);
140 if (existingPropertyVal > 1)
142 setHiDPIScale(existingPropertyVal);
145 } catch (NumberFormatException e)
147 System.out.println("Could not convert property " + scalePropertyName
148 + " vale '" + existingProperty + "' to number");
152 // Try and auto guess a good scale based on reported DPI (not trustworthy)
153 // and screen resolution (more trustworthy)
156 screenInfo = getScreenInfo();
159 dpi = screenInfo.getScreenResolution();
160 } catch (HeadlessException e)
162 System.err.println("Cannot get screen resolution: " + e.getMessage());
165 // try and get screen size height and width
168 int height = screenInfo.getScreenHeight();
169 int width = screenInfo.getScreenWidth();
170 // using mindimension in case of portrait screens
171 mindimension = Math.min(height, width);
172 } catch (HeadlessException e)
175 "Cannot get screen size height and width:" + e.getMessage());
178 // attempt at a formula for scaling based on screen dpi and mindimension.
179 // scale will be an integer >=1. This formula is based on some testing and
182 // scale based on reported dpi. if dpi>hidpiThreshold then scale=2+multiples
183 // of hidpiMultiThreshold (else scale=1)
184 // (e.g. dpi of 110 scales 1. dpi of 120 scales 2. dpi of 360 scales 3)
185 int dpiScale = (dpi - hidpiThreshold > 0)
186 ? 2 + ((dpi - hidpiThreshold) / hidpiMultiThreshold)
189 int dimensionScale = 1 + (mindimension / bigScreenThreshold);
191 // reject outrageous values -- dpiScale in particular could be mistaken
192 if (dpiScale > MAX_SCALE)
196 if (dimensionScale > MAX_SCALE)
201 // choose larger of dimensionScale or dpiScale (most likely dimensionScale
202 // as dpiScale often misreported)
203 int autoScale = Math.max(dpiScale, dimensionScale);
205 // only make an automatic change if scale is changed and other conditions
206 // (OS is linux) apply, or if setHiDPI has been specified
207 if ((autoScale > 1 && isLinux) || setHiDPI)
209 setHiDPIScale(autoScale);
213 // looks like we're not doing any scaling
217 public static void setHiDPIScale(int s)
220 allowScalePropertyArg = true;
224 public static String getScalePropertyArg(int s)
226 return "-D" + scalePropertyName + "=" + String.valueOf(s);
229 public static String getScalePropertyArg()
232 // HiDPI setting. Just looking at Linux to start with. Test with Windows.
233 return allowScalePropertyArg ? getScalePropertyArg(scale) : null;
236 public static void clear()
245 allowScalePropertyArg = false;
248 public static void setScreenInfo(ScreenInfo si)
253 public static ScreenInfo getScreenInfo()
255 if (screenInfo == null)
257 screenInfo = new ScreenInfo();