From fba56a0955a48f49caae1ee2715696dd3a4d8fb0 Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Fri, 2 Jan 2026 15:14:35 +0100 Subject: [PATCH] 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. --- .../cocoa/org/eclipse/swt/printing/Printer.java | 1 + .../win32/org/eclipse/swt/printing/PDFDocument.java | 2 ++ .../cocoa/org/eclipse/swt/graphics/Device.java | 11 +++++++++++ .../gtk/org/eclipse/swt/graphics/Device.java | 11 +++++++++++ .../gtk/org/eclipse/swt/widgets/Display.java | 4 +++- .../win32/org/eclipse/swt/graphics/Device.java | 11 +++++++++++ .../junit/Test_org_eclipse_swt_widgets_Display.java | 1 + 7 files changed, 40 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java index 97b489a57b3..9149ae77840 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/cocoa/org/eclipse/swt/printing/Printer.java @@ -623,6 +623,7 @@ public Point getDPI() { } } +@SuppressWarnings("deprecation") Point getIndependentDPI() { return super.getDPI(); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PDFDocument.java b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PDFDocument.java index db15aa658f9..bd8696b0da5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PDFDocument.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Printing/win32/org/eclipse/swt/printing/PDFDocument.java @@ -215,6 +215,7 @@ public PDFDocument(Device device, String filename, double width, double height) int screenDpiX = 96; int screenDpiY = 96; if (this.device != null) { + @SuppressWarnings("deprecation") Point dpi = this.device.getDPI(); screenDpiX = dpi.x; screenDpiY = dpi.y; @@ -427,6 +428,7 @@ public long internal_new_GC(GCData data) { int screenDpiX = 96; int screenDpiY = 96; if (device != null) { + @SuppressWarnings("deprecation") Point dpi = device.getDPI(); screenDpiX = dpi.x; screenDpiY = dpi.y; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java index ef30e780765..af48ed1cf68 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Device.java @@ -391,7 +391,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); return getScreenDPI(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java index 0178d8c60ba..4b6347263be 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Device.java @@ -455,7 +455,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); return getScreenDPI(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java index a8f29694eef..9db0c56dde8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java @@ -5052,7 +5052,9 @@ String debugInfoForIndex(long index) { } void dpiChanged(int newScaleFactor) { - DPIUtil.setDeviceZoom (DPIUtil.mapDPIToZoom(getDPI().x * newScaleFactor)); + @SuppressWarnings("deprecation") + int dpiX = getDPI().x; + DPIUtil.setDeviceZoom (DPIUtil.mapDPIToZoom(dpiX * newScaleFactor)); Shell[] shells = getShells(); for (int i = 0; i < shells.length; i++) { shells[i].layout(true, true); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java index 04c7c51b1c4..1a3783448d3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Device.java @@ -519,7 +519,18 @@ public int getDepth () { * @exception SWTException + * + * @deprecated

This method returns a single global DPI value + * that does not reflect per-monitor DPI settings on modern operating systems. + * In environments with different scaling factors across monitors, it may provide + * a misleading or meaningless result, as it does not correspond to the actual DPI + * of any specific monitor.

+ * + *

Note: While deprecated for general {@code Device} instances like {@code Display}, + * this method may still be validly used when called on a {@code Printer} instance, + * where a single global DPI value is meaningful and expected.

*/ +@Deprecated public Point getDPI () { checkDevice (); long hDC = internal_new_GC (null); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java index 11a67f30d08..91610a25577 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java @@ -1553,6 +1553,7 @@ public void test_wake() { /* custom */ boolean disposeExecRan; +@SuppressWarnings("deprecation") @Test public void test_getDPI() { Display display = new Display();