X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FHiDPISetting.java;h=e14c032b61930cb94b4b958826c38db60591b2c5;hb=3fdc889794e3566af57628f0b6a308eb23886f96;hp=497900f28b04670133a8602f357d034bbc0aad85;hpb=678a85b1a46c29056536b6e8bc3c7a3ea1e3d1df;p=jalview.git diff --git a/src/jalview/bin/HiDPISetting.java b/src/jalview/bin/HiDPISetting.java index 497900f..e14c032 100644 --- a/src/jalview/bin/HiDPISetting.java +++ b/src/jalview/bin/HiDPISetting.java @@ -1,7 +1,27 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.bin; import java.awt.HeadlessException; -import java.awt.Toolkit; +import java.util.Locale; public class HiDPISetting { @@ -11,7 +31,7 @@ public class HiDPISetting private static final int bigScreenThreshold = 1400; - private static final String scalePropertyName = "sun.java2d.uiScale"; + public static final String scalePropertyName = "sun.java2d.uiScale"; private static final boolean isLinux; @@ -35,14 +55,18 @@ public class HiDPISetting public static int scale = 0; + public final static int MAX_SCALE = 8; + private static boolean doneInit = false; private static boolean allowScalePropertyArg = false; + private static ScreenInfo screenInfo = new ScreenInfo(); + static { String system = System.getProperty("os.name") == null ? null - : System.getProperty("os.name").toLowerCase(); + : System.getProperty("os.name").toLowerCase(Locale.ROOT); if (system != null) { isLinux = system.indexOf("linux") > -1; @@ -65,8 +89,17 @@ public class HiDPISetting // get and use command line property values first String setHiDPIProperty = System.getProperty(setHiDPIPropertyName); - setHiDPI = setHiDPIProperty != null - && setHiDPIProperty.equalsIgnoreCase("true"); + boolean setHiDPIPropertyBool = Boolean.parseBoolean(setHiDPIProperty); + + // allow -DsetHiDPI=false to turn off HiDPI scaling + if (setHiDPIProperty != null && !setHiDPIPropertyBool) + { + clear(); + doneInit = true; + return; + } + + setHiDPI = setHiDPIProperty != null && setHiDPIPropertyBool; String setHiDPIScaleProperty = System .getProperty(setHiDPIScalePropertyName); @@ -75,10 +108,17 @@ public class HiDPISetting try { setHiDPIScale = Integer.parseInt(setHiDPIScaleProperty); + // if setHiDPIScale property is validly set and setHiDPI property wasn't + // attempted to be set we assume setHiDPIScale to be true + if (setHiDPIProperty == null) + { + setHiDPI = true; + } } catch (NumberFormatException e) { - System.err.println(setHiDPIScalePropertyName + " property give (" - + setHiDPIScaleProperty + ") but not parseable as integer"); + jalview.bin.Console.errPrintln(setHiDPIScalePropertyName + + " property give (" + setHiDPIScaleProperty + + ") but not parseable as integer"); } } if (setHiDPI && setHiDPIScale > 0) @@ -95,8 +135,8 @@ public class HiDPISetting try { int existingPropertyVal = Integer.parseInt(existingProperty); - System.out.println("Existing " + scalePropertyName + " is " - + existingPropertyVal); + jalview.bin.Console.outPrintln("Existing " + scalePropertyName + + " is " + existingPropertyVal); if (existingPropertyVal > 1) { setHiDPIScale(existingPropertyVal); @@ -104,8 +144,9 @@ public class HiDPISetting } } catch (NumberFormatException e) { - System.out.println("Could not convert property " + scalePropertyName - + " vale '" + existingProperty + "' to number"); + jalview.bin.Console.outPrintln( + "Could not convert property " + scalePropertyName + + " vale '" + existingProperty + "' to number"); } } @@ -113,25 +154,34 @@ public class HiDPISetting // and screen resolution (more trustworthy) // get screen dpi + screenInfo = getScreenInfo(); try { - dpi = Toolkit.getDefaultToolkit().getScreenResolution(); + dpi = screenInfo.getScreenResolution(); } catch (HeadlessException e) { - System.err.println("Cannot get screen resolution: " + e.getMessage()); + if (isLinux) + { + jalview.bin.Console.errPrintln( + "Cannot get screen resolution: " + e.getMessage()); + } } // try and get screen size height and width try { - int height = Toolkit.getDefaultToolkit().getScreenSize().height; - int width = Toolkit.getDefaultToolkit().getScreenSize().width; + int height = screenInfo.getScreenHeight(); + int width = screenInfo.getScreenWidth(); // using mindimension in case of portrait screens mindimension = Math.min(height, width); } catch (HeadlessException e) { - System.err.println( - "Cannot get screen size height and width:" + e.getMessage()); + if (isLinux) + { + jalview.bin.Console + .errPrintln("Cannot get screen size height and width:" + + e.getMessage()); + } } // attempt at a formula for scaling based on screen dpi and mindimension. @@ -147,6 +197,16 @@ public class HiDPISetting int dimensionScale = 1 + (mindimension / bigScreenThreshold); + // reject outrageous values -- dpiScale in particular could be mistaken + if (dpiScale > MAX_SCALE) + { + dpiScale = 1; + } + if (dimensionScale > MAX_SCALE) + { + dimensionScale = 1; + } + // choose larger of dimensionScale or dpiScale (most likely dimensionScale // as dpiScale often misreported) int autoScale = Math.max(dpiScale, dimensionScale); @@ -170,11 +230,41 @@ public class HiDPISetting doneInit = true; } + public static String getScalePropertyArg(int s) + { + return "-D" + scalePropertyName + "=" + String.valueOf(s); + } + public static String getScalePropertyArg() { init(); // HiDPI setting. Just looking at Linux to start with. Test with Windows. - return allowScalePropertyArg ? "-D" + scalePropertyName + "=" + scale - : null; + return allowScalePropertyArg ? getScalePropertyArg(scale) : null; + } + + public static void clear() + { + setHiDPI = false; + setHiDPIScale = 0; + dpi = 0; + mindimension = 0; + width = 0; + scale = 0; + doneInit = false; + allowScalePropertyArg = false; + } + + public static void setScreenInfo(ScreenInfo si) + { + screenInfo = si; + } + + public static ScreenInfo getScreenInfo() + { + if (screenInfo == null) + { + screenInfo = new ScreenInfo(); + } + return screenInfo; } }