From a88f187095b0eea6fbb3bf0b50f4d6bf2aa48db3 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Mar 2025 10:38:19 -0700 Subject: [PATCH 1/5] Revert `sealed` appended to `Point` and `Rectangle`. --- .../Eclipse SWT/common/org/eclipse/swt/graphics/Point.java | 2 +- .../Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 6de7f344460..bf1b6ec5857 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 @@ -41,7 +41,7 @@ * @see Sample code and further information */ -public sealed class Point implements Serializable permits MonitorAwarePoint { +public class Point implements Serializable { /** * the x coordinate of the point 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 b8e78d3b8da..477ef6cc96c 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 @@ -45,7 +45,7 @@ * @see Sample code and further information */ -public sealed class Rectangle implements Serializable permits MonitorAwareRectangle { +public class Rectangle implements Serializable { /** * the x coordinate of the rectangle From 7dc41edde69ad33ae8b4a0b8a72f8d52b80d3409 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Mar 2025 10:57:58 -0700 Subject: [PATCH 2/5] Make `MonitorAware` package-private and move it next to the win32-specific `MultiZoomCoordinateSystemMapper`. --- .../org/eclipse/swt/widgets}/MonitorAwarePoint.java | 2 +- .../org/eclipse/swt/widgets}/MonitorAwareRectangle.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename bundles/org.eclipse.swt/Eclipse SWT/{common/org/eclipse/swt/graphics => win32/org/eclipse/swt/widgets}/MonitorAwarePoint.java (97%) rename bundles/org.eclipse.swt/Eclipse SWT/{common/org/eclipse/swt/graphics => win32/org/eclipse/swt/widgets}/MonitorAwareRectangle.java (97%) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java similarity index 97% rename from bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java rename to bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java index 282acdbe07b..1045aafd168 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java @@ -26,7 +26,7 @@ * @since 3.129 * @noreference This class is not intended to be referenced by clients */ -public final class MonitorAwarePoint extends Point { +final class MonitorAwarePoint extends Point { private static final long serialVersionUID = 6077427420686999194L; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java similarity index 97% rename from bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java rename to bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java index f20d620e356..2e38779dd21 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java @@ -26,7 +26,7 @@ * @since 3.129 * @noreference This class is not intended to be referenced by clients */ -public final class MonitorAwareRectangle extends Rectangle { +final class MonitorAwareRectangle extends Rectangle { private static final long serialVersionUID = 5041911840525116925L; From 2a7ec35f45864a935aeaf04e574185f79817636f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Mar 2025 12:00:19 -0700 Subject: [PATCH 3/5] Remove the `MonitorAware` variants. --- .../swt/widgets/MonitorAwarePoint.java | 71 ------------------ .../swt/widgets/MonitorAwareRectangle.java | 73 ------------------- 2 files changed, 144 deletions(-) delete mode 100644 bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java delete mode 100644 bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java deleted file mode 100644 index 1045aafd168..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwarePoint.java +++ /dev/null @@ -1,71 +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 java.util.*; - -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 - */ -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) { - if (this == object) { - return true; - } - if (!super.equals(object)) { - return false; - } - MonitorAwarePoint other = (MonitorAwarePoint) object; - return Objects.equals(this.monitor, other.monitor); - } - - @Override - public int hashCode() { - return Objects.hash(super.hashCode(), monitor); - } - -} diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java deleted file mode 100644 index 2e38779dd21..00000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MonitorAwareRectangle.java +++ /dev/null @@ -1,73 +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 java.util.*; - -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 - */ -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) { - if (this == object) { - return true; - } - if (!super.equals(object)) { - return false; - } - MonitorAwareRectangle other = (MonitorAwareRectangle) object; - return Objects.equals(this.monitor, other.monitor); - } - - @Override - public int hashCode() { - return Objects.hash(super.hashCode(), monitor); - } - -} From 03abfeac8a359a340c831b8cdadefcd058691149 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Mar 2025 12:00:56 -0700 Subject: [PATCH 4/5] Restore old `Point` and `Rectangle` as final with simple equality. --- .../common/org/eclipse/swt/graphics/Point.java | 16 ++++------------ .../org/eclipse/swt/graphics/Rectangle.java | 16 ++++------------ 2 files changed, 8 insertions(+), 24 deletions(-) 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 bf1b6ec5857..069fb4d81eb 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 @@ -41,7 +41,7 @@ * @see Sample code and further information */ -public class Point implements Serializable { +public final class Point implements Serializable { /** * the x coordinate of the point @@ -78,17 +78,9 @@ public Point (int x, int y) { */ @Override public boolean equals (Object object) { - if (object == null) { - return false; - } - if (object == this) { - return true; - } - if (object.getClass() != this.getClass()) { - return false; - } - Point other = (Point) object; - return (other.x == this.x) && (other.y == this.y); + if (object == this) return true; + if (!(object instanceof Point p)) return false; + return (p.x == this.x) && (p.y == this.y); } /** 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 477ef6cc96c..8c1503488d7 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 @@ -45,7 +45,7 @@ * @see Sample code and further information */ -public class Rectangle implements Serializable { +public final class Rectangle implements Serializable { /** * the x coordinate of the rectangle @@ -156,17 +156,9 @@ public boolean contains (Point pt) { */ @Override public boolean equals(Object object) { - if (object == null) { - return false; - } - if (object == this) { - return true; - } - if (object.getClass() != this.getClass()) { - return false; - } - Rectangle other = (Rectangle) object; - return (other.x == this.x) && (other.y == this.y) && (other.width == this.width) && (other.height == this.height); + if (object == this) return true; + if (!(object instanceof Rectangle r)) return false; + return (r.x == this.x) && (r.y == this.y) && (r.width == this.width) && (r.height == this.height); } /** From 5726ec18a500842cee410aa7b4dbf6bae44b303b Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 12 Mar 2025 12:01:33 -0700 Subject: [PATCH 5/5] Add `WithMonitor` into `MultiZoomCoordinateSystemMapper` to deal with monitor-specific transformations. --- .../MultiZoomCoordinateSystemMapper.java | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) 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 2057c9305ed..72585418a03 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 @@ -19,6 +19,14 @@ import org.eclipse.swt.internal.*; class MultiZoomCoordinateSystemMapper implements CoordinateSystemMapper { + static class WithMonitor { + final T geometry; + final Monitor monitor; + WithMonitor(T geometry, Monitor monitor) { + this.geometry = geometry; + this.monitor = monitor; + } + } private final Display display; @@ -45,42 +53,40 @@ public Rectangle map(Control from, Control to, Rectangle rectangle) { @Override public Point map(Control from, Control to, int x, int y) { - Point mappedPointInPoints; if (from == null) { Point mappedPointInpixels = display.mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y)); - mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); + return DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); } else if (to == null) { Point mappedPointInpixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom())); - mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, + WithMonitor mappedPointInPointsWithMonitor = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y); + return mappedPointInPointsWithMonitor.geometry; } else { Point mappedPointInpixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom())); - mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); + return DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); } - return mappedPointInPoints; } @Override public Rectangle map(Control from, Control to, int x, int y, int width, int height) { - Rectangle mappedRectangleInPoints; if (from == null) { Rectangle mappedRectangleInPixels = display.mapInPixels(from, to, translateRectangleInPointsToPixels(x, y, width, height, to.getShell().getMonitor())); - mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); + return DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); } else if (to == null) { Rectangle mappedRectangleInPixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom())); - mappedRectangleInPoints = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x, + WithMonitor mappedRectangleInPoints = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor()); + return mappedRectangleInPoints.geometry; } else { Rectangle mappedRectangleInPixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom())); - mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); + return DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); } - return mappedRectangleInPoints; } @Override @@ -98,19 +104,20 @@ 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 = 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; - return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor); + Monitor monitor = null; + WithMonitor value = translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor); + return value.geometry; } @Override public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; + Monitor monitor = null; return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor); } @@ -133,7 +140,8 @@ private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) { private Point translateLocationInPixelsToPoints(int x, int y) { Monitor monitor = getContainingMonitorForPixels(x, y); - return getPointFromPixels(monitor, x, y); + WithMonitor value = getPointFromPixels(monitor, x, y); + return value.geometry; } private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, int height, Monitor monitor) { @@ -145,15 +153,14 @@ private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, in return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels); } - private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) { + private WithMonitor translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) { if (monitor == null) monitor = getContainingMonitorForPixels(x, y, widthInPixels, heightInPixels); int zoom = getApplicableMonitorZoom(monitor); - Point topLeft = getPointFromPixels(monitor, x, y); + WithMonitor 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); - return rect; + return new WithMonitor<>(new Rectangle(topLeft.geometry.x, topLeft.geometry.y, width, height), monitor); } private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) { @@ -257,11 +264,11 @@ private Point getPixelsFromPoint(Monitor monitor, int x, int y) { return new Point(mappedX, mappedY); } - private Point getPointFromPixels(Monitor monitor, int x, int y) { + private WithMonitor 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 WithMonitor<>(new Point(mappedX, mappedY), monitor); } private int getApplicableMonitorZoom(Monitor monitor) {