package jalview.bin; import java.awt.Toolkit; public class HiDPISetting { private static final int hidpiThreshold = 130; private static final int tallScreenThreshold = 1400; private static final String scalePropertyName = "sun.java2d.uiScale"; private static final boolean isAMac; private static final boolean isLinux; private static final boolean isWindows; public static final String setHiDPIPropertyName = "setHiDPI"; public static final String setHiDPIScalePropertyName = "setHiDPIScale"; private static boolean setHiDPI = false; private static int setHiDPIScale = 0; public static int dpi = 0; public static int mindimension = 0; public static int width = 0; public static int scale = 0; private static boolean doneInit = false; private static boolean allowScalePropertyArg = false; static { isAMac = System.getProperty("os.name").indexOf("Mac") > -1; isLinux = System.getProperty("os.name").toLowerCase() .indexOf("linux") > -1; isWindows = System.getProperty("os.name").toLowerCase() .indexOf("windows") > -1; } private static void init() { if (doneInit) { return; } String setHiDPIProperty = System.getProperty(setHiDPIPropertyName); setHiDPI = setHiDPIProperty != null && setHiDPIProperty.equalsIgnoreCase("true"); String setHiDPIScaleProperty = System .getProperty(setHiDPIScalePropertyName); if (setHiDPIScaleProperty != null) { try { setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty); } catch (NumberFormatException e) { System.err.println(setHiDPIScalePropertyName + " property give (" + setHiDPIScaleProperty + ") but not parseable as integer"); } } // try and get screen resolution try { dpi = Toolkit.getDefaultToolkit().getScreenResolution(); } catch (Throwable t) { System.err.println("Cannot get screen resolution"); } // try and get screen size height and width try { int height = Toolkit.getDefaultToolkit().getScreenSize().height; int width = Toolkit.getDefaultToolkit().getScreenSize().width; // using mindimension in case of portrait screens mindimension = Math.min(height, width); } catch (Throwable t) { System.err.println("Cannot get screen size height and width"); } if (setHiDPI && setHiDPIScale > 0) { scale = setHiDPIScale; } else { // attempt at a formula for scaling based on screen dpi and mindimension. // scale // will be an integer >=1 if (dpi > 0 && mindimension > 0) { scale = Math.min(dpi / hidpiThreshold, mindimension / tallScreenThreshold) + 1; } else if (dpi == 0 && mindimension > tallScreenThreshold) { // dpi couldn't be found but the screen has a large vertical pixel count scale = mindimension / tallScreenThreshold + 1; } /* do nothing if mindimension == 0 -- might be just a small HD screen (e.g. Gemini PDA) else if (mindimension == 0 && dpi > hidpiThreshold) { } */ } // only make a change if scale is changed and other conditions (OS is linux) // apply, or if setHiDPI has been specified allowScalePropertyArg = (scale > 1 && isLinux) || setHiDPI; if (allowScalePropertyArg) { System.out.println("boolean setHiDPI=" + setHiDPI); System.out.println("DPI detected as " + dpi + ". Scaling factor set to " + scale + "."); } doneInit = true; } public static void setHiDPIScale(int s) { scale = s; allowScalePropertyArg = true; doneInit = true; } public static String getScalePropertyArg() { init(); // HiDPI setting. Just looking at Linux to start with. Test with Windows. return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale : null; } }