From e15f5491f3a513a433b90c9aacecdb9f23301a34 Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Thu, 3 Jul 2025 15:05:02 +0200 Subject: [PATCH] Reintroduce scaling methods for Geometries This commit reintroduces scaling methods for plain Rectangle & Point to provide backwards compatibility to the consumers which explicitly create a Rectangle & Point and want to call scaleBounds or scaleUp/scaleDown since they have different behaviour and Rectangle.OfFloat unifies this. --- .../swt/widgets/ControlWin32Tests.java | 4 +- .../eclipse/swt/internal/Win32DPIUtils.java | 43 +++++++++++++++++++ .../swt/tests/win32/Win32DPIUtilTests.java | 4 +- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java index 759580dc17b..7926fb9e16c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java @@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() { button.setBounds(new Rectangle(0, 47, 200, 47)); assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly", - new Rectangle(0, 82, 350, 82), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 83), button.getBoundsInPixels()); button.setBounds(0, 47, 200, 47); assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly", - new Rectangle(0, 82, 350, 82), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 83), button.getBoundsInPixels()); } record FontComparison(int originalFontHeight, int currentFontHeight) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java index a783734f57e..6d21d20be7d 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java @@ -108,6 +108,20 @@ public static Point pixelToPoint(Drawable drawable, Point point, int zoom) { } public static Rectangle pixelToPoint(Rectangle rect, int zoom) { + if (zoom == 100 || rect == null) return rect; + if (rect instanceof Rectangle.OfFloat rectOfFloat) return pixelToPoint(rectOfFloat, zoom); + Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0); + Point scaledTopLeft = pixelToPoint(new Point (rect.x, rect.y), zoom); + Point scaledBottomRight = pixelToPoint(new Point (rect.x + rect.width, rect.y + rect.height), zoom); + + scaledRect.x = scaledTopLeft.x; + scaledRect.y = scaledTopLeft.y; + scaledRect.width = scaledBottomRight.x - scaledTopLeft.x; + scaledRect.height = scaledBottomRight.y - scaledTopLeft.y; + return scaledRect; + } + + private static Rectangle pixelToPoint(Rectangle.OfFloat rect, int zoom) { return scaleBounds(rect, 100, zoom); } @@ -120,6 +134,21 @@ public static Rectangle pixelToPoint(Drawable drawable, Rectangle rect, int zoom * Returns a new rectangle as per the scaleFactor. */ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) { + if (rect == null || targetZoom == currentZoom) return rect; + if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleBounds(rectOfFloat, targetZoom, currentZoom); + float scaleFactor = ((float)targetZoom) / (float)currentZoom; + Rectangle returnRect = new Rectangle.OfFloat (0,0,0,0); + returnRect.x = Math.round (rect.x * scaleFactor); + returnRect.y = Math.round (rect.y * scaleFactor); + returnRect.width = Math.round (rect.width * scaleFactor); + returnRect.height = Math.round (rect.height * scaleFactor); + return returnRect; + } + + /** + * Returns a new rectangle as per the scaleFactor. + */ + private static Rectangle scaleBounds (Rectangle.OfFloat rect, int targetZoom, int currentZoom) { if (rect == null || targetZoom == currentZoom) return rect; Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect); float scaleFactor = DPIUtil.getScalingFactor(targetZoom, currentZoom); @@ -185,6 +214,20 @@ public static Point pointToPixel(Drawable drawable, Point point, int zoom) { } public static Rectangle pointToPixel(Rectangle rect, int zoom) { + if (zoom == 100 || rect == null) return rect; + if (rect instanceof Rectangle.OfFloat rectOfFloat) return pointToPixel(rectOfFloat, zoom); + Rectangle scaledRect = new Rectangle.OfFloat(0,0,0,0); + Point scaledTopLeft = pointToPixel (new Point(rect.x, rect.y), zoom); + Point scaledBottomRight = pointToPixel (new Point(rect.x + rect.width, rect.y + rect.height), zoom); + + scaledRect.x = scaledTopLeft.x; + scaledRect.y = scaledTopLeft.y; + scaledRect.width = scaledBottomRight.x - scaledTopLeft.x; + scaledRect.height = scaledBottomRight.y - scaledTopLeft.y; + return scaledRect; + } + + private static Rectangle pointToPixel(Rectangle.OfFloat rect, int zoom) { return scaleBounds(rect, zoom, 100); } diff --git a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java index 656a3e35bf1..1d1c6ec56fb 100644 --- a/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java +++ b/tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java @@ -202,7 +202,7 @@ public void scaleUpPoint() { @Test public void scaleUpRectangle() { Rectangle valueAt200 = new Rectangle(100, 150, 10, 14); - Rectangle valueAt150 = new Rectangle(75, 113, 8, 11); + Rectangle valueAt150 = new Rectangle(75, 113, 8, 10); Rectangle valueAt100 = new Rectangle(50, 75, 5, 7); Rectangle scaledValue = Win32DPIUtils.pointToPixel(valueAt100, 200); @@ -225,7 +225,7 @@ public void scaleDownscaleUpRectangleInvertible() { for (int zoom1 : zooms) { for (int zoom2 : zooms) { for (int i = 1; i <= 10000; i++) { - Rectangle rect = new Rectangle(0, 0, i, i); + Rectangle rect = new Rectangle.OfFloat(0, 0, i, i); Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom1); Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2); scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);