From 1c213103e92861f4a7ea6e3339c4bf8561d82adb Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Thu, 12 Jun 2025 11:46:25 +0200 Subject: [PATCH 1/2] Internalize MonitorAware* Classes to Parent #62 This commit moves the classes MonitorAwareRectangle and MonitorAwarePoint to Rectangle and Point as a static class and renames them to WithMonitor to allow them to be used as Rectangle.WithMonitor and Point.WithMonitor. contributes to https://github.com/eclipse-platform/eclipse.platform.swt/issues/62 and https://github.com/eclipse-platform/eclipse.platform.swt/issues/128 --- .../widgets/CoordinateSystemMapperTests.java | 6 +- .../swt/graphics/MonitorAwarePoint.java | 62 ----------------- .../swt/graphics/MonitorAwareRectangle.java | 69 ------------------- .../org/eclipse/swt/graphics/Point.java | 40 ++++++++++- .../org/eclipse/swt/graphics/Rectangle.java | 50 +++++++++++++- .../MultiZoomCoordinateSystemMapper.java | 12 ++-- 6 files changed, 95 insertions(+), 144 deletions(-) delete mode 100644 bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java delete mode 100644 bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java index d2c8068877a..a4852735005 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java @@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() { MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); - Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]); + Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]); Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); } @@ -223,7 +223,7 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Point(x, y); } else { - return new MonitorAwarePoint(x, y, monitor); + return new Point.WithMonitor(x, y, monitor); } } @@ -231,7 +231,7 @@ private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Rectangle(x, y, width, height); } else { - return new MonitorAwareRectangle(x, y, width, height, monitor); + return new Rectangle.WithMonitor(x, y, width, height, monitor); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java deleted file mode 100644 index 5e73e051e69..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2025 Yatta Solutions and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Yatta Solutions - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.graphics; - -import org.eclipse.swt.widgets.*; - -/** - * Instances of this class represent {@link org.eclipse.swt.graphics.Point} - * objects along with the context of the monitor in relation to which they are - * placed on the display. The monitor awareness makes it easy to scale and - * translate the points between pixels and points. - * - * @since 3.129 - * @noreference This class is not intended to be referenced by clients - */ -public final class MonitorAwarePoint extends Point { - - private static final long serialVersionUID = 6077427420686999194L; - - private final Monitor monitor; - - /** - * Constructs a new MonitorAwarePoint - * - * @param x the x coordinate of the point - * @param y the y coordinate of the point - * @param monitor the monitor with whose context the point is created - */ - public MonitorAwarePoint(int x, int y, Monitor monitor) { - super(x, y); - this.monitor = monitor; - } - - /** - * {@return the monitor with whose context the instance is created} - */ - public Monitor getMonitor() { - return monitor; - } - - @Override - public boolean equals(Object object) { - return super.equals(object); - } - - @Override - public int hashCode() { - return super.hashCode(); - } - -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java deleted file mode 100644 index ea0597621c3..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2025 Yatta Solutions and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * Yatta Solutions - initial API and implementation - *******************************************************************************/ -package org.eclipse.swt.graphics; - -import org.eclipse.swt.widgets.*; - -/** - * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle} - * objects along with the context of the monitor in relation to which they are - * placed on the display. The monitor awareness makes it easy to scale and - * translate the rectangles between pixels and points. - * - * @since 3.129 - * @noreference This class is not intended to be referenced by clients - */ -public final class MonitorAwareRectangle extends Rectangle { - - private static final long serialVersionUID = 5041911840525116925L; - - private final Monitor monitor; - - /** - * Constructs a new MonitorAwareRectangle - * - * @param x the x coordinate of the top left corner of the rectangle - * @param y the y coordinate of the top left corner of the rectangle - * @param width the width of the rectangle - * @param height the height of the rectangle - * @param monitor the monitor with whose context the rectangle is created - */ - public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) { - super(x, y, width, height); - this.monitor = monitor; - } - - /** - * {@return the monitor with whose context the instance is created} - */ - public Monitor getMonitor() { - return monitor; - } - - @Override - public boolean equals(Object object) { - return super.equals(object); - } - - @Override - public int hashCode() { - return super.hashCode(); - } - - @Override - public MonitorAwareRectangle clone() { - return new MonitorAwareRectangle(x, y, width, height, monitor); - } - -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java index 2aef2d06820..07257e03fd8 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java @@ -16,6 +16,8 @@ import java.io.*; +import org.eclipse.swt.widgets.*; + /** * Instances of this class represent places on the (x, y) * coordinate plane. @@ -41,7 +43,7 @@ * @see Sample code and further information */ -public sealed class Point implements Serializable permits MonitorAwarePoint { +public sealed class Point implements Serializable permits Point.WithMonitor { /** * the x coordinate of the point @@ -116,5 +118,41 @@ public String toString () { return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Point} + * objects along with the context of the monitor in relation to which they are + * placed on the display. The monitor awareness makes it easy to scale and + * translate the points between pixels and points. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static final class WithMonitor extends Point { + + private static final long serialVersionUID = 6077427420686999194L; + + private final Monitor monitor; + + /** + * Constructs a new Point.WithMonitor + * + * @param x the x coordinate of the point + * @param y the y coordinate of the point + * @param monitor the monitor with whose context the point is created + */ + public WithMonitor(int x, int y, Monitor monitor) { + super(x, y); + this.monitor = monitor; + } + + /** + * {@return the monitor with whose context the instance is created} + */ + public Monitor getMonitor() { + return monitor; + } + +} + } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java index b50cb13e6ac..3bfbb293ac3 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java @@ -17,6 +17,7 @@ import java.io.*; import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; /** * Instances of this class represent rectangular areas in an @@ -45,7 +46,7 @@ * @see Sample code and further information */ -public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle { +public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.WithMonitor { /** * the x coordinate of the rectangle @@ -373,8 +374,8 @@ public Rectangle union (Rectangle rect) { * @since 3.131 */ public static Rectangle of(Point topLeft, int width, int height) { - if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) { - return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor()); + if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) { + return new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor()); } return new Rectangle(topLeft.x, topLeft.y, width, height); } @@ -396,4 +397,47 @@ public static Rectangle of(Point topLeft, int width, int height) { public Rectangle clone() { return new Rectangle(x, y, width, height); } + +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle} + * objects along with the context of the monitor in relation to which they are + * placed on the display. The monitor awareness makes it easy to scale and + * translate the rectangles between pixels and points. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static final class WithMonitor extends Rectangle { + + private static final long serialVersionUID = 5041911840525116925L; + + private final Monitor monitor; + + /** + * Constructs a new Rectangle.WithMonitor + * + * @param x the x coordinate of the top left corner of the rectangle + * @param y the y coordinate of the top left corner of the rectangle + * @param width the width of the rectangle + * @param height the height of the rectangle + * @param monitor the monitor with whose context the rectangle is created + */ + public WithMonitor(int x, int y, int width, int height, Monitor monitor) { + super(x, y, width, height); + this.monitor = monitor; + } + + /** + * {@return the monitor with whose context the instance is created} + */ + public Monitor getMonitor() { + return monitor; + } + + @Override + public Rectangle.WithMonitor clone() { + return new Rectangle.WithMonitor(x, y, width, height, monitor); + } + +} } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java index 886a47701e3..80347e36e19 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java @@ -98,19 +98,19 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) { @Override public Point translateToDisplayCoordinates(Point point, int zoom) { - Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null; + Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null; return translateLocationInPointsToPixels(point.x, point.y, monitor); } @Override public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; + Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null; return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor); } @Override public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; + Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null; return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor); } @@ -152,7 +152,7 @@ private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPi Point topLeft = getPointFromPixels(monitor, x, y); int width = DPIUtil.scaleDown(widthInPixels, zoom); int height = DPIUtil.scaleDown(heightInPixels, zoom); - MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor); + Rectangle.WithMonitor rect = new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitor); return rect; } @@ -264,7 +264,7 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) { int zoom = getApplicableMonitorZoom(monitor); int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX; int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY; - return new MonitorAwarePoint(mappedX, mappedY, monitor); + return new Point.WithMonitor(mappedX, mappedY, monitor); } private int getApplicableMonitorZoom(Monitor monitor) { @@ -273,7 +273,7 @@ private int getApplicableMonitorZoom(Monitor monitor) { @Override public Rectangle getContainingMonitorBoundsInPixels(Point point) { - Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() + Monitor monitor = point instanceof Point.WithMonitor monitorAwarePoint ? monitorAwarePoint.getMonitor() : getContainingMonitorForPoints(point.x, point.y); return getMonitorClientAreaInPixels(monitor); } From a9f3fc7d99bf9ce485a5a5afcc72d518d8573d3d Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Thu, 12 Jun 2025 13:23:49 +0200 Subject: [PATCH 2/2] Introduce OfFloat classes for better scaling #62 This commit introduces Rectangle.OfFloat and Point.OfFloat classes as an extension of Rectangle and Point respectively to improve scaling precision in the DPIUtil by using residuals obtained from rounding. Contributes to https://github.com/eclipse-platform/eclipse.platform.swt/issues/62 and https://github.com/eclipse-platform/eclipse.platform.swt/issues/128 --- .../swt/widgets/ControlWin32Tests.java | 4 +- .../org/eclipse/swt/graphics/Point.java | 47 ++++++++++- .../org/eclipse/swt/graphics/Rectangle.java | 78 +++++++++++++++++-- .../org/eclipse/swt/internal/DPIUtil.java | 66 ++++++++-------- .../eclipse/swt/tests/junit/DPIUtilTests.java | 36 +++++++++ 5 files changed, 187 insertions(+), 44 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 ab56cc9bf49..ada7b902d84 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 @@ -78,11 +78,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, 83), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 82), 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, 83), button.getBoundsInPixels()); + new Rectangle(0, 82, 350, 82), button.getBoundsInPixels()); } record FontComparison(int originalFontHeight, int currentFontHeight) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java index 07257e03fd8..b1a026ca92c 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java @@ -43,7 +43,7 @@ * @see Sample code and further information */ -public sealed class Point implements Serializable permits Point.WithMonitor { +public sealed class Point implements Serializable permits Point.OfFloat { /** * the x coordinate of the point @@ -120,6 +120,48 @@ public String toString () { /** * Instances of this class represent {@link org.eclipse.swt.graphics.Point} + * objects with the fields capable of storing more precise value in float. + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static sealed class OfFloat extends Point permits Point.WithMonitor { + + private static final long serialVersionUID = -1862062276431597053L; + + public float residualX, residualY; + + public OfFloat(int x, int y) { + super(x, y); + } + + public OfFloat(float x, float y) { + super(Math.round(x), Math.round(y)); + this.residualX = x - this.x; + this.residualY = y - this.y; + } + + public float getX() { + return x + residualX; + } + + public float getY() { + return y + residualY; + } + + public void setX(float x) { + this.x = Math.round(x); + this.residualX = x - this.x; + } + + public void setY(float y) { + this.y = Math.round(y); + this.residualY = y - this.y; + } +} + +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Point.OfFloat} * objects along with the context of the monitor in relation to which they are * placed on the display. The monitor awareness makes it easy to scale and * translate the points between pixels and points. @@ -127,7 +169,7 @@ public String toString () { * @since 3.131 * @noreference This class is not intended to be referenced by clients */ -public static final class WithMonitor extends Point { +public static final class WithMonitor extends Point.OfFloat { private static final long serialVersionUID = 6077427420686999194L; @@ -155,4 +197,3 @@ public Monitor getMonitor() { } } - diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java index 3bfbb293ac3..935f61e9e6e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java @@ -46,7 +46,7 @@ * @see Sample code and further information */ -public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.WithMonitor { +public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.OfFloat { /** * the x coordinate of the rectangle @@ -375,7 +375,7 @@ public Rectangle union (Rectangle rect) { */ public static Rectangle of(Point topLeft, int width, int height) { if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) { - return new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor()); + return new Rectangle.WithMonitor(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor()); } return new Rectangle(topLeft.x, topLeft.y, width, height); } @@ -400,6 +400,69 @@ public Rectangle clone() { /** * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle} + * objects which supports values of Float type for it's fields + * + * @since 3.131 + * @noreference This class is not intended to be referenced by clients + */ +public static sealed class OfFloat extends Rectangle permits Rectangle.WithMonitor { + + private static final long serialVersionUID = -3006999002677468391L; + + private float residualX, residualY, residualWidth, residualHeight; + + public OfFloat(int x, int y, int width, int height) { + super(x, y, width, height); + } + + public OfFloat(float x, float y, float width, float height) { + super(Math.round(x), Math.round(y), Math.round(width), Math.round(height)); + this.residualX = x - this.x; + this.residualY = y - this.y; + this.residualWidth = width - this.width; + this.residualHeight = height - this.height; + } + + public float getX() { + return x + residualX; + } + + public float getY() { + return y + residualY; + } + + public float getWidth() { + return width + residualWidth; + } + + public float getHeight() { + return height + residualHeight; + } + + public void setX(float x) { + this.x = Math.round(x); + this.residualX = x - this.x; + } + + public void setY(float y) { + this.y = Math.round(y); + this.residualY = y - this.y; + } + + public void setWidth(float width) { + this.width = Math.round(width); + this.residualWidth = width - this.width; + } + + public void setHeight(float height) { + this.height = Math.round(height); + this.residualHeight = height - this.height; + } + +} + +/** + * Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle.OfFloat} * objects along with the context of the monitor in relation to which they are * placed on the display. The monitor awareness makes it easy to scale and * translate the rectangles between pixels and points. @@ -407,7 +470,7 @@ public Rectangle clone() { * @since 3.131 * @noreference This class is not intended to be referenced by clients */ -public static final class WithMonitor extends Rectangle { +public static final class WithMonitor extends Rectangle.OfFloat { private static final long serialVersionUID = 5041911840525116925L; @@ -427,6 +490,11 @@ public WithMonitor(int x, int y, int width, int height, Monitor monitor) { this.monitor = monitor; } + private WithMonitor(float x, float y, float width, float height, Monitor monitor) { + super(x, y, width, height); + this.monitor = monitor; + } + /** * {@return the monitor with whose context the instance is created} */ @@ -436,8 +504,8 @@ public Monitor getMonitor() { @Override public Rectangle.WithMonitor clone() { - return new Rectangle.WithMonitor(x, y, width, height, monitor); + return new Rectangle.WithMonitor(getX(), getY(), getWidth(), getHeight(), monitor); } } -} +} \ No newline at end of file diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index a15f4683ae3..0f7e475ebe5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -228,11 +228,11 @@ public static Point autoScaleDown(Point point) { public static Point scaleDown(Point point, int zoom) { if (zoom == 100 || point == null) return point; + Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point); float scaleFactor = getScalingFactor(zoom); - Point scaledPoint = new Point (0,0); - scaledPoint.x = Math.round (point.x / scaleFactor); - scaledPoint.y = Math.round (point.y / scaleFactor); - return scaledPoint; + float scaledX = fPoint.getX() / scaleFactor; + float scaledY = fPoint.getY() / scaleFactor; + return new Point.OfFloat(scaledX, scaledY); } /** @@ -255,16 +255,7 @@ public static Rectangle autoScaleDown(Rectangle rect) { } public static Rectangle scaleDown(Rectangle rect, int zoom) { - if (zoom == 100 || rect == null) return rect; - Rectangle scaledRect = new Rectangle (0,0,0,0); - Point scaledTopLeft = scaleDown(new Point (rect.x, rect.y), zoom); - Point scaledBottomRight = scaleDown(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; + return scaleBounds(rect, 100, zoom); } /** * Returns a new scaled down Rectangle if enabled for Drawable class. @@ -333,13 +324,13 @@ public static boolean isSmoothScalingEnabled() { */ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) { if (rect == null || targetZoom == currentZoom) return rect; + Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect); float scaleFactor = ((float)targetZoom) / (float)currentZoom; - Rectangle returnRect = new Rectangle (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; + float scaledX = fRect.getX() * scaleFactor; + float scaledY = fRect.getY() * scaleFactor; + float scaledWidth = fRect.getWidth() * scaleFactor; + float scaledHeight = fRect.getHeight() * scaleFactor; + return new Rectangle.OfFloat(scaledX, scaledY, scaledWidth, scaledHeight); } /** @@ -436,11 +427,11 @@ public static Point autoScaleUp(Point point) { public static Point scaleUp(Point point, int zoom) { if (zoom == 100 || point == null) return point; + Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point); float scaleFactor = getScalingFactor(zoom); - Point scaledPoint = new Point(0,0); - scaledPoint.x = Math.round (point.x * scaleFactor); - scaledPoint.y = Math.round (point.y * scaleFactor); - return scaledPoint; + float scaledX = fPoint.getX() * scaleFactor; + float scaledY = fPoint.getY() * scaleFactor; + return new Point.OfFloat(scaledX, scaledY); } /** @@ -463,16 +454,7 @@ public static Rectangle autoScaleUp(Rectangle rect) { } public static Rectangle scaleUp(Rectangle rect, int zoom) { - if (zoom == 100 || rect == null) return rect; - Rectangle scaledRect = new Rectangle(0,0,0,0); - Point scaledTopLeft = scaleUp (new Point(rect.x, rect.y), zoom); - Point scaledBottomRight = scaleUp (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; + return scaleBounds(rect, zoom, 100); } /** @@ -751,4 +733,20 @@ public ImageData getImageData(int zoom) { return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom); } } + +private class FloatAwareGeometryFactory { + static Rectangle.OfFloat createFrom(Rectangle rectangle) { + if (rectangle instanceof Rectangle.OfFloat) { + return (Rectangle.OfFloat) rectangle; + } + return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height); + } + + static Point.OfFloat createFrom(Point point) { + if (point instanceof Point.OfFloat) { + return (Point.OfFloat) point; + } + return new Point.OfFloat(point.x, point.y); + } +} } \ No newline at end of file diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java index d501c4d0a67..316e8f9da17 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java @@ -319,4 +319,40 @@ public void scaleUpRectangle() { scaledValue = DPIUtil.scaleUp((Device) null, valueAt100, 100); assertSame(valueAt100, scaledValue, "Scaling up Rectangle without zoom change with device failed"); } + + @Test + public void scaleDownscaleUpRectangleInvertible() { + int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400}; + for (int zoom1 : zooms) { + for (int zoom2 : zooms) { + for (int i = 1; i <= 10000; i++) { + Rectangle rect = new Rectangle(0, 0, i, i); + Rectangle scaleDown = DPIUtil.scaleDown(rect, zoom1); + Rectangle scaleUp = DPIUtil.scaleUp(scaleDown, zoom2); + scaleDown = DPIUtil.scaleDown(scaleUp, zoom2); + scaleUp = DPIUtil.scaleUp(scaleDown, zoom1); + assertEquals(rect.width, scaleUp.width); + assertEquals(rect.height, scaleUp.height); + } + } + } + } + + @Test + public void scaleDownscaleUpPointInvertible() { + int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400}; + for (int zoom1 : zooms) { + for (int zoom2 : zooms) { + for (int i = 1; i <= 10000; i++) { + Point pt = new Point(i, i); + Point scaleDown = DPIUtil.scaleDown(pt, zoom1); + Point scaleUp = DPIUtil.scaleUp(scaleDown, zoom2); + scaleDown = DPIUtil.scaleDown(scaleUp, zoom2); + scaleUp = DPIUtil.scaleUp(scaleDown, zoom1); + assertEquals(pt.x, scaleUp.x); + assertEquals(pt.y, scaleUp.y); + } + } + } + } }