X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=getdown%2Fsrc%2Fgetdown%2Fcore%2Fsrc%2Fmain%2Fjava%2Fjalview%2Fbin%2FHiDPISetting.java;h=4472a1904acb3e4058a4509d97134f39a0ad305d;hb=62d10e940e86310c996659dd729aa61f7fd516fd;hp=5e20d7658cfd6fa952bb069c2791c67be948e309;hpb=1e26a902b2a53081ab6035d43c2bdf13f1a7ee4e;p=jalview.git diff --git a/getdown/src/getdown/core/src/main/java/jalview/bin/HiDPISetting.java b/getdown/src/getdown/core/src/main/java/jalview/bin/HiDPISetting.java index 5e20d76..4472a19 100644 --- a/getdown/src/getdown/core/src/main/java/jalview/bin/HiDPISetting.java +++ b/getdown/src/getdown/core/src/main/java/jalview/bin/HiDPISetting.java @@ -4,45 +4,145 @@ import java.awt.Toolkit; public class HiDPISetting { - public static final int dpi; + private static final int hidpiThreshold = 130; - public static final int scale; + private static final int tallScreenThreshold = 1300; - private static final int hidpi = 110; + private static final String scalePropertyName = "sun.java2d.uiScale"; - private static final String scaleProperty = "sun.java2d.uiScale"; + private static final boolean isAMac; - private static final boolean isAMac = System.getProperty("os.name") - .indexOf("Mac") > -1; + private static final boolean isLinux; - private static final boolean isLinux = System.getProperty("os.name") - .toLowerCase().indexOf("linux") > -1; + private static final boolean isWindows; - private static final boolean isWindows = System.getProperty("os.name") - .toLowerCase().indexOf("windows") > -1; + 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 maxdimension = 0; + + public static int width = 0; + + public static int scale = 0; + + private static boolean doneInit = false; + + private static boolean allowScalePropertyArg = false; static { - dpi = Toolkit.getDefaultToolkit().getScreenResolution(); - scale = dpi / hidpi + 1; - if (scale > 1 && isLinux) + 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 maxdimension in case of portrait screens + maxdimension = Math.max(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 maxdimension. + // scale + // will be an integer >=1 + if (dpi > 0 && maxdimension > 0) + { + scale = Math.min(dpi / hidpiThreshold, + maxdimension / tallScreenThreshold) + 1; + } + else if (dpi == 0 && maxdimension > tallScreenThreshold) + { + // dpi couldn't be found but the screen has a large vertical pixel count + scale = maxdimension / tallScreenThreshold + 1; + } + /* do nothing if maxdimension == 0 -- might be just a small HD screen (e.g. Gemini PDA) + else if (maxdimension == 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. - if (!isLinux) - { - return null; - } - if (scale > 1) - { - return "-D" + scaleProperty + "=" + scale; - } - return null; + return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale + : null; } }