Skip to content

Commit fa741e2

Browse files
committed
lots of code to handle scaling on Windows; also for #378
1 parent c175233 commit fa741e2

2 files changed

Lines changed: 78 additions & 34 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ public class PApplet implements PConstants {
733733
*/
734734
static public final String ARGS_SKETCH_FOLDER = "--sketch-path";
735735

736-
static public final String ARGS_DENSITY = "--density";
736+
static public final String ARGS_UI_SCALE = "--ui-scale";
737737

738738
/**
739739
* When run externally to a PdeEditor,
@@ -785,11 +785,9 @@ public PSurface getSurface() {
785785

786786
boolean fullScreen;
787787
int display = -1; // use default
788-
// GraphicsDevice[] displayDevices;
789788
// Unlike the others above, needs to be public to support
790789
// the pixelWidth and pixelHeight fields.
791790
public int pixelDensity = 1;
792-
int suggestedDensity = -1;
793791

794792
boolean present;
795793

@@ -10181,19 +10179,6 @@ static public void runSketch(final String[] args,
1018110179
uncaughtThrowable = e;
1018210180
});
1018310181

10184-
/*
10185-
if (platform == WINDOWS) {
10186-
// Set DPI scaling to either 1 or 2, but avoid fractional versions like
10187-
// 125% and 250% that make things look gross, or even 300% since that
10188-
// is not even a thing.
10189-
int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); // no longer possible to set prop after this line
10190-
System.out.println("dpi came back " + dpi);
10191-
int scaleFactor = constrain(dpi / 96, 1, 2);
10192-
System.out.println("setting scale factor to " + scaleFactor);
10193-
System.setProperty("sun.java2d.uiscale", String.valueOf(scaleFactor));
10194-
}
10195-
*/
10196-
1019710182
// This doesn't work, need to mess with Info.plist instead
1019810183
/*
1019910184
// In an exported application, add the Contents/Java folder to the
@@ -10234,10 +10219,8 @@ static public void runSketch(final String[] args,
1023410219
boolean hideStop = false;
1023510220

1023610221
int displayNum = -1; // use default
10237-
// boolean fullScreen = false;
1023810222
boolean present = false;
10239-
// boolean spanDisplays = false;
10240-
int density = -1;
10223+
float uiScale = 0;
1024110224

1024210225
String param, value;
1024310226
String folder = calcSketchPath();
@@ -10286,23 +10269,17 @@ static public void runSketch(final String[] args,
1028610269
} else if (param.equals(ARGS_LOCATION)) {
1028710270
location = parseInt(split(value, ','));
1028810271

10289-
} else if (param.equals(ARGS_DENSITY)) {
10290-
density = parseInt(value, -1);
10291-
if (density == -1) {
10292-
System.err.println("Could not parse " + value + " for " + ARGS_DENSITY);
10293-
} else if (density != 1 && density != 2) {
10294-
density = -1;
10295-
System.err.println(ARGS_DENSITY + " should be 1 or 2");
10272+
} else if (param.equals(ARGS_UI_SCALE)) {
10273+
uiScale = parseFloat(value, 0);
10274+
if (uiScale == 0) {
10275+
System.err.println("Could not parse " + value + " for " + ARGS_UI_SCALE);
1029610276
}
1029710277
}
1029810278

1029910279
} else {
1030010280
if (args[argIndex].equals(ARGS_PRESENT)) {
1030110281
present = true;
1030210282

10303-
// } else if (args[argIndex].equals(ARGS_SPAN_DISPLAYS)) {
10304-
// spanDisplays = true;
10305-
1030610283
} else if (args[argIndex].equals(ARGS_HIDE_STOP)) {
1030710284
hideStop = true;
1030810285

@@ -10317,6 +10294,29 @@ static public void runSketch(final String[] args,
1031710294
argIndex++;
1031810295
}
1031910296

10297+
if (platform == WINDOWS) {
10298+
// Set DPI scaling to either 1 or 2, but avoid fractional
10299+
// settings such as 125% and 250% that make things look gross.
10300+
// Also applies to 300% since that is not even a thing.
10301+
10302+
// no longer possible to set prop after this line initializes AWT
10303+
//int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
10304+
10305+
// Attempt to get the resolution using a helper app. This code is
10306+
// fairly conservative: if there is trouble, we go with the default.
10307+
if (uiScale == 0) {
10308+
int dpi = getWindowsDPI();
10309+
if (dpi != 0) {
10310+
uiScale = constrain(dpi / 96, 1, 2);
10311+
}
10312+
}
10313+
if (uiScale != 0) {
10314+
System.setProperty("sun.java2d.uiScale", String.valueOf(uiScale));
10315+
} else {
10316+
System.err.println("Could not identify Windows DPI, not setting sun.java2d.uiScale");
10317+
}
10318+
}
10319+
1032010320
if (!disableAWT) {
1032110321
ShimAWT.initRun();
1032210322
}
@@ -10357,10 +10357,6 @@ static public void runSketch(final String[] args,
1035710357
// (and most likely, from the PDE's preference setting).
1035810358
sketch.display = displayNum;
1035910359

10360-
// Set the suggested density that is coming from command line
10361-
// (most likely set from the PDE based on a system DPI scaling)
10362-
sketch.suggestedDensity = density;
10363-
1036410360
sketch.present = present;
1036510361

1036610362
// For 3.0.1, moved this above handleSettings() so that loadImage() can be
@@ -10526,6 +10522,46 @@ static public void hideMenuBar() {
1052610522
}
1052710523

1052810524

10525+
/**
10526+
* Find the location of fenster.exe by walking through java.library.path.
10527+
* (It will be on the path because it's part of core/library/windows-amd64)
10528+
*/
10529+
static private String findFenster() {
10530+
String libraryPath = System.getProperty("java.library.path");
10531+
// Should not be null, but cannot assume
10532+
if (libraryPath != null) {
10533+
String[] folders = split(libraryPath, ';');
10534+
// Usually, the most relevant paths will be at the front of the list,
10535+
// so hopefully this will not walk several entries.
10536+
for (String folder : folders) {
10537+
File file = new File(folder, "fenster.exe");
10538+
if (file.exists()) {
10539+
return file.getAbsolutePath();
10540+
}
10541+
}
10542+
}
10543+
return null;
10544+
}
10545+
10546+
10547+
/**
10548+
* Get the display scaling for Windows by calling out to a helper app.
10549+
* https://github.com/processing/processing4/tree/master/build/windows/fenster
10550+
*/
10551+
static private int getWindowsDPI() {
10552+
String fensterPath = findFenster();
10553+
if (fensterPath != null) {
10554+
StringList stdout = new StringList();
10555+
StringList stderr = new StringList();
10556+
int result = exec(stdout, stderr, fensterPath);
10557+
if (result == 0) {
10558+
return parseInt(stdout.join(""), 0);
10559+
}
10560+
}
10561+
return 0;
10562+
}
10563+
10564+
1052910565
/**
1053010566
* Convenience method for Python Mode to run an already-constructed sketch.
1053110567
* This makes it makes it easy to launch a sketch in Jython:

java/src/processing/mode/java/runner/Runner.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import processing.app.*;
2626
import processing.app.exec.StreamRedirectThread;
27-
import processing.app.ui.Toolkit;
2827
import processing.core.*;
2928
import processing.data.StringList;
3029
import processing.mode.java.JavaBuild;
@@ -33,6 +32,7 @@
3332
import java.awt.GraphicsDevice;
3433
import java.awt.GraphicsEnvironment;
3534
import java.awt.Point;
35+
import java.awt.Toolkit;
3636
import java.io.*;
3737
import java.net.ConnectException;
3838
import java.net.InetAddress;
@@ -490,9 +490,17 @@ protected StringList getSketchParams(boolean present, String[] args) {
490490
// removed for 3.0a6 because it would break the args passed to sketches.
491491
params.append(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath());
492492

493+
/*
493494
if (Toolkit.zoom(100) >= 200) { // Use 100 to bypass possible rounding in zoom()
494495
params.append(PApplet.ARGS_DENSITY + "=2");
495496
}
497+
*/
498+
if (Platform.isWindows()) {
499+
// Pass the DPI setting to the app to avoid using the helper app.
500+
int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
501+
int uiScale = PApplet.constrain(dpi / 96, 1, 2);
502+
params.append(PApplet.ARGS_UI_SCALE + "=" + uiScale);
503+
}
496504

497505
params.append(build.getSketchClassName());
498506
}

0 commit comments

Comments
 (0)