Skip to content

Commit 523a47f

Browse files
* Deprecating Display#getDPI
Having the scale factor being based on the screen DPI leads to unexpected result e.g. Image too big/small. Having a screen dpi independent factor leads to consistent results
1 parent a413b4b commit 523a47f

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,31 @@ public int getIconDepth () {
20372037
};
20382038
}
20392039

2040+
/**
2041+
* Returns the DPI (dots per inch) for the display.
2042+
* <p>
2043+
* <strong>Deprecated:</strong> This method returns a single global DPI value
2044+
* that does not reflect per-monitor DPI settings on modern operating systems.
2045+
* In environments with different scaling factors across monitors, this method
2046+
* provides a misleading or meaningless result, as it does not correspond to
2047+
* the actual DPI of any specific monitor.
2048+
* </p>
2049+
* <p>
2050+
* In most cases, callers should not rely on this method. If a fallback is
2051+
* needed, a value of 96 DPI (the default baseline for 100% scaling) may be
2052+
* used.
2053+
* </p>
2054+
*
2055+
* @return the DPI of the display device
2056+
*
2057+
* @deprecated Use per-monitor DPI or scaling APIs instead. This method is unreliable in multi-monitor setups with varying DPI.
2058+
*/
2059+
@Deprecated
2060+
@Override
2061+
public Point getDPI () {
2062+
return super.getDPI();
2063+
}
2064+
20402065
ImageList getImageList (int style, int width, int height, int zoom) {
20412066
if (imageList == null) imageList = new ImageList [4];
20422067

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,16 +1165,16 @@ void menuPrint() {
11651165
if (image == null) return;
11661166

11671167
try {
1168+
final int DOTS_PER_INCH = 96;
11681169
// Ask the user to specify the printer.
11691170
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
11701171
if (printerData != null) dialog.setPrinterData(printerData);
11711172
printerData = dialog.open();
11721173
if (printerData == null) return;
11731174

11741175
Printer printer = new Printer(printerData);
1175-
Point screenDPI = display.getDPI();
11761176
Point printerDPI = printer.getDPI();
1177-
int scaleFactor = printerDPI.x / screenDPI.x;
1177+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
11781178
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
11791179
if (printer.startJob(currentName)) {
11801180
if (printer.startPage()) {

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) {
146146
}
147147

148148
private static void performPrintAction(final Display display, final Shell shell) {
149+
final int DOTS_PER_INCH = 96;
149150
Rectangle r = composite.getBounds();
150151
Point p = shell.toDisplay(r.x, r.y);
151152
org.eclipse.swt.graphics.Image snapshotImage
@@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell)
159160
data = dialog.open();
160161
if (data != null) {
161162
Printer printer = new Printer(data);
162-
Point screenDPI = display.getDPI();
163163
Point printerDPI = printer.getDPI();
164-
int scaleFactor = printerDPI.x / screenDPI.x;
164+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
165165
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
166166
if (printer.startJob("Print Image")) {
167167
ImageData imageData = snapshotImage.getImageData();

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public int getGcStyle() {
185185

186186
createSeparator(shell);
187187

188-
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
188+
new Label (shell, SWT.NONE).setText ("5. 50x50 box");
189189
Label box= new Label (shell, SWT.NONE);
190190
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
191191
box.setLayoutData (new GridData (50, 50));

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ public void test_wake() {
15731573
/* custom */
15741574
boolean disposeExecRan;
15751575

1576+
@SuppressWarnings("deprecation")
15761577
@Test
15771578
public void test_getDPI() {
15781579
Display display = new Display();

0 commit comments

Comments
 (0)