Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display density detection and use GraphicsConfiguration. #34

Merged
merged 1 commit into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions app/src/processing/app/ui/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -907,23 +908,14 @@ static public boolean isRetina() {
// 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();

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;
GraphicsDevice graphicsDevice = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = graphicsDevice
.getDefaultConfiguration();

AffineTransform tx = graphicsConfig.getDefaultTransform();
return Math.round(tx.getScaleX()) == 2;
}


Expand Down
63 changes: 10 additions & 53 deletions core/src/processing/core/PApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
// 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;

Expand Down Expand Up @@ -1158,59 +1160,14 @@ public int displayDensity() {
* @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();

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;
GraphicsDevice graphicsDevice = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
GraphicsConfiguration graphicsConfig = graphicsDevice
.getDefaultConfiguration();

AffineTransform tx = graphicsConfig.getDefaultTransform();
return (int) Math.round(tx.getScaleX());
}


Expand Down