Merge pull request #34 from processing/display_density

Fix display density detection and use GraphicsConfiguration.
This commit is contained in:
Ben Fry
2019-11-26 05:35:39 -05:00
committed by GitHub
2 changed files with 17 additions and 68 deletions
+8 -16
View File
@@ -30,6 +30,7 @@ import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
@@ -907,23 +908,14 @@ public class Toolkit {
// A 5-minute search didn't turn up any such event in the Java API.
// Also, should we use the Toolkit associated with the editor window?
static private boolean checkRetina() {
if (Platform.isMacOS()) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
GraphicsDevice graphicsDevice = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = graphicsDevice
.getDefaultConfiguration();
try {
Field field = device.getClass().getDeclaredField("scale");
if (field != null) {
field.setAccessible(true);
Object scale = field.get(device);
if (scale instanceof Integer && ((Integer)scale).intValue() == 2) {
return true;
}
}
} catch (Exception ignore) { }
}
return false;
AffineTransform tx = graphicsConfig.getDefaultTransform();
return Math.round(tx.getScaleX()) == 2;
}
+9 -52
View File
@@ -30,8 +30,10 @@ import java.awt.Frame;
// before calling settings() to get displayWidth/Height
import java.awt.DisplayMode;
// handleSettings() and displayDensity()
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.geom.AffineTransform;
// used to present the fullScreen() warning about Spaces on OS X
import javax.swing.JOptionPane;
@@ -1158,59 +1160,14 @@ public class PApplet implements PConstants {
* @param display the display number to check
*/
public int displayDensity(int display) {
if (PApplet.platform == PConstants.MACOS) {
// This should probably be reset each time there's a display change.
// A 5-minute search didn't turn up any such event in the Java 7 API.
// Also, should we use the Toolkit associated with the editor window?
final String javaVendor = System.getProperty("java.vendor");
if (javaVendor.contains("Oracle")) {
GraphicsDevice device;
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = graphicsDevice
.getDefaultConfiguration();
if (display == -1) {
device = env.getDefaultScreenDevice();
} else if (display == SPAN) {
throw new RuntimeException("displayDensity() only works with specific display numbers");
} else {
GraphicsDevice[] devices = env.getScreenDevices();
if (display > 0 && display <= devices.length) {
device = devices[display - 1];
} else {
if (devices.length == 1) {
System.err.println("Only one display is currently known, use displayDensity(1).");
} else {
System.err.format("Your displays are numbered %d through %d, " +
"pass one of those numbers to displayDensity()%n", 1, devices.length);
}
throw new RuntimeException("Display " + display + " does not exist.");
}
}
try {
Field field = device.getClass().getDeclaredField("scale");
if (field != null) {
field.setAccessible(true);
Object scale = field.get(device);
if (scale instanceof Integer && ((Integer)scale).intValue() == 2) {
return 2;
}
}
} catch (Exception ignore) { }
}
} else if (PApplet.platform == PConstants.WINDOWS ||
PApplet.platform == PConstants.LINUX) {
if (suggestedDensity == -1) {
// TODO: detect and return DPI scaling using JNA; Windows has
// a system-wide value, not sure how it works on Linux
return 1;
} else if (suggestedDensity == 1 || suggestedDensity == 2) {
return suggestedDensity;
}
}
return 1;
AffineTransform tx = graphicsConfig.getDefaultTransform();
return (int) Math.round(tx.getScaleX());
}