From beb20b4e6bd14bc4307eb0329f4ec17141f18e91 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:20:22 -0700 Subject: [PATCH 1/8] Break the MonitorAware variants out of the inheritance hierarchy, and remove equality. --- .../swt/graphics/MonitorAwarePoint.java | 31 +++++----------- .../swt/graphics/MonitorAwareRectangle.java | 35 ++++++------------- .../org/eclipse/swt/graphics/Point.java | 2 +- .../org/eclipse/swt/graphics/Rectangle.java | 2 +- 4 files changed, 20 insertions(+), 50 deletions(-) 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 index 282acdbe07b..04032664379 100644 --- 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 @@ -13,8 +13,6 @@ *******************************************************************************/ package org.eclipse.swt.graphics; -import java.util.*; - import org.eclipse.swt.widgets.*; /** @@ -26,10 +24,8 @@ * @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; - +public final class MonitorAwarePoint { + private final Point point; private final Monitor monitor; /** @@ -40,7 +36,7 @@ public final class MonitorAwarePoint extends Point { * @param monitor the monitor with whose context the point is created */ public MonitorAwarePoint(int x, int y, Monitor monitor) { - super(x, y); + this.point = new Point(x, y); this.monitor = monitor; } @@ -51,21 +47,10 @@ 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); + /** + * {@return the monitor with whose context the instance is created} + */ + public Point getPoint() { + return point; } - } 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 index f20d620e356..37dc0dcd6ad 100644 --- 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 @@ -13,8 +13,6 @@ *******************************************************************************/ package org.eclipse.swt.graphics; -import java.util.*; - import org.eclipse.swt.widgets.*; /** @@ -26,10 +24,8 @@ * @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; - +public final class MonitorAwareRectangle { + private final Rectangle rect; private final Monitor monitor; /** @@ -42,32 +38,21 @@ public final class MonitorAwareRectangle extends 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.rect = new Rectangle(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); + public Rectangle getRectangle() { + return rect; } - @Override - public int hashCode() { - return Objects.hash(super.hashCode(), 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/Point.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java index 6de7f344460..3d5ae0b4a24 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 final 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..afba751d056 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 final class Rectangle implements Serializable { /** * the x coordinate of the rectangle From a4f1aa4beb580d51bfe628efffa5ad80c7ccb38a Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:31:16 -0700 Subject: [PATCH 2/8] Add convenience constructors. --- .../org/eclipse/swt/graphics/MonitorAwarePoint.java | 10 +++++++++- .../eclipse/swt/graphics/MonitorAwareRectangle.java | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) 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 index 04032664379..3e015b7f973 100644 --- 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 @@ -36,7 +36,15 @@ public final class MonitorAwarePoint { * @param monitor the monitor with whose context the point is created */ public MonitorAwarePoint(int x, int y, Monitor monitor) { - this.point = new Point(x, y); + this(new Point(x, y), monitor); + } + + public MonitorAwarePoint(Point point) { + this(point, null); + } + + public MonitorAwarePoint(Point point, Monitor monitor) { + this.point = point; this.monitor = monitor; } 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 index 37dc0dcd6ad..a79deb223ee 100644 --- 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 @@ -38,7 +38,15 @@ public final class MonitorAwareRectangle { * @param monitor the monitor with whose context the rectangle is created */ public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) { - this.rect = new Rectangle(x, y, width, height); + this(new Rectangle(x, y, width, height), monitor); + } + + public MonitorAwareRectangle(Rectangle rect) { + this(rect, null); + } + + public MonitorAwareRectangle(Rectangle rect, Monitor monitor) { + this.rect = rect; this.monitor = monitor; } From c5e616df85f75e579c2b44bc16ac485392c8fe6f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:33:24 -0700 Subject: [PATCH 3/8] Update `CoordinateSystemMapper` so that it takes `MonitorAware` anywhere that the monitor was being used. --- .../swt/widgets/CoordinateSystemMapper.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java index 28527ed39e4..83a005661fd 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java @@ -17,23 +17,23 @@ interface CoordinateSystemMapper { - Rectangle map(Control from, Control to, Rectangle rectangle); + MonitorAwareRectangle map(Control from, Control to, Rectangle rectangle); - Rectangle map(Control from, Control to, int x, int y, int width, int height); + MonitorAwareRectangle map(Control from, Control to, int x, int y, int width, int height); - Point map(Control from, Control to, Point point); + MonitorAwarePoint map(Control from, Control to, Point point); - Point map(Control from, Control to, int x, int y); + MonitorAwarePoint map(Control from, Control to, int x, int y); Rectangle mapMonitorBounds(Rectangle rectangle, int zoom); Point translateFromDisplayCoordinates(Point point, int zoom); - Point translateToDisplayCoordinates(Point point, int zoom); + Point translateToDisplayCoordinates(MonitorAwarePoint point, int zoom); - Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom); + Rectangle translateFromDisplayCoordinates(MonitorAwareRectangle rect, int zoom); - Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom); + Rectangle translateToDisplayCoordinates(MonitorAwareRectangle rect, int zoom); void setCursorLocation(int x, int y); From 9da8bb43d68e07aa19efa5577b710bd02d460f60 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:34:15 -0700 Subject: [PATCH 4/8] Update Display so that `MonitorAware` is trimmed off to end users, but preserved for internal use. --- .../win32/org/eclipse/swt/widgets/Display.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index 5d2c82aadc4..a82c622c3ae 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -2981,7 +2981,7 @@ boolean isValidThread () { public Point map (Control from, Control to, Point point) { checkDevice (); if (point == null) error (SWT.ERROR_NULL_ARGUMENT); - return coordinateSystemMapper.map(from, to, point); + return coordinateSystemMapper.map(from, to, point).getPoint(); } Point mapInPixels (Control from, Control to, Point point) { @@ -3026,7 +3026,7 @@ Point mapInPixels (Control from, Control to, Point point) { */ public Point map (Control from, Control to, int x, int y) { checkDevice (); - return coordinateSystemMapper.map(from, to, x, y); + return coordinateSystemMapper.map(from, to, x, y).getPoint(); } Point mapInPixels (Control from, Control to, int x, int y) { @@ -3081,7 +3081,7 @@ Point mapInPixels (Control from, Control to, int x, int y) { public Rectangle map (Control from, Control to, Rectangle rectangle) { checkDevice (); if (rectangle == null) error (SWT.ERROR_NULL_ARGUMENT); - return coordinateSystemMapper.map(from, to, rectangle); + return coordinateSystemMapper.map(from, to, rectangle).getRectangle(); } Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) { @@ -3128,7 +3128,7 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) { */ public Rectangle map (Control from, Control to, int x, int y, int width, int height) { checkDevice (); - return coordinateSystemMapper.map(from, to, x, y, width, height); + return coordinateSystemMapper.map(from, to, x, y, width, height).getRectangle(); } Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int height) { @@ -3150,15 +3150,15 @@ Point translateFromDisplayCoordinates(Point point, int zoom) { return coordinateSystemMapper.translateFromDisplayCoordinates(point, zoom); } -Point translateToDisplayCoordinates(Point point, int zoom) { +Point translateToDisplayCoordinates(MonitorAwarePoint point, int zoom) { return coordinateSystemMapper.translateToDisplayCoordinates(point, zoom); } -Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) { +Rectangle translateFromDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { return coordinateSystemMapper.translateFromDisplayCoordinates(rect, zoom); } -Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { +Rectangle translateToDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { return coordinateSystemMapper.translateToDisplayCoordinates(rect, zoom); } From f43eadfc404aac0ee3c2e096ddb9bbea9faa50c0 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:37:13 -0700 Subject: [PATCH 5/8] Update Control/Menu/Shell so that they create `MonitorAware` with `monitor` initialized to null. In some cases I think we can easily pre-populate `monitor`, but I'm leaving that out on purpose to match existing behavior. --- .../Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java | 2 +- .../Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java | 2 +- .../Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java index ab059c02332..dae1cf3666a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java @@ -4009,7 +4009,7 @@ void subclass () { public Point toControl (int x, int y) { checkWidget (); int zoom = getZoom(); - Point displayPointInPixels = getDisplay().translateToDisplayCoordinates(new Point(x, y), zoom); + Point displayPointInPixels = getDisplay().translateToDisplayCoordinates(new MonitorAwarePoint(new Point(x, y)), zoom); final Point controlPointInPixels = toControlInPixels(displayPointInPixels.x, displayPointInPixels.y); return DPIUtil.scaleDown(controlPointInPixels, zoom); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java index 3a6428e4a58..596a9d1e37b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Menu.java @@ -1225,7 +1225,7 @@ void setLocationInPixels (int x, int y) { public void setLocation (Point location) { checkWidget (); if (location == null) error (SWT.ERROR_NULL_ARGUMENT); - Point locationInPixels = getDisplay().translateToDisplayCoordinates(location, getZoom()); + Point locationInPixels = getDisplay().translateToDisplayCoordinates(new MonitorAwarePoint(location), getZoom()); setLocationInPixels(locationInPixels.x, locationInPixels.y); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java index c3a388b770b..76dbaa6ed39 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java @@ -1569,7 +1569,7 @@ public void setAlpha (int alpha) { @Override public Rectangle getBounds() { checkWidget (); - return getDisplay().translateFromDisplayCoordinates(getBoundsInPixels(), getZoom()); + return getDisplay().translateFromDisplayCoordinates(new MonitorAwareRectangle(getBoundsInPixels()), getZoom()); } @Override @@ -1582,7 +1582,7 @@ public Point getLocation() { public void setLocation(Point location) { if (location == null) error (SWT.ERROR_NULL_ARGUMENT); checkWidget (); - Point locationInPixels = getDisplay().translateToDisplayCoordinates(location, getZoom()); + Point locationInPixels = getDisplay().translateToDisplayCoordinates(new MonitorAwarePoint(location), getZoom()); setLocationInPixels(locationInPixels.x, locationInPixels.y); } @@ -1595,7 +1595,7 @@ public void setLocation(int x, int y) { public void setBounds(Rectangle rect) { if (rect == null) error (SWT.ERROR_NULL_ARGUMENT); checkWidget (); - Rectangle boundsInPixels = getDisplay().translateToDisplayCoordinates(rect, getZoom()); + Rectangle boundsInPixels = getDisplay().translateToDisplayCoordinates(new MonitorAwareRectangle(rect), getZoom()); // The scaling of the width and height in case of a monitor change is handled by // the WM_DPICHANGED event processing. So to avoid duplicate scaling, we always // have to scale width and height with the zoom of the original monitor (still From 35f4331e1e14766594544f9b86c7d7e2e1ee2e34 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:54:03 -0700 Subject: [PATCH 6/8] Consequences for `SingleZoomCoordinateSystemMapper`. --- .../SingleZoomCoordinateSystemMapper.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java index 84ae1fb64f7..2dccac909ce 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java @@ -39,35 +39,35 @@ private int getZoomLevelForMapping(Control from, Control to) { } @Override - public Point map(Control from, Control to, Point point) { + public MonitorAwarePoint map(Control from, Control to, Point point) { int zoom = getZoomLevelForMapping(from, to); point = DPIUtil.scaleUp(point, zoom); - return DPIUtil.scaleDown(display.mapInPixels(from, to, point), zoom); + return new MonitorAwarePoint(DPIUtil.scaleDown(display.mapInPixels(from, to, point), zoom)); } @Override - public Rectangle map(Control from, Control to, Rectangle rectangle) { + public MonitorAwareRectangle map(Control from, Control to, Rectangle rectangle) { int zoom = getZoomLevelForMapping(from, to); rectangle = DPIUtil.scaleUp(rectangle, zoom); - return DPIUtil.scaleDown(display.mapInPixels(from, to, rectangle), zoom); + return new MonitorAwareRectangle(DPIUtil.scaleDown(display.mapInPixels(from, to, rectangle), zoom)); } @Override - public Point map(Control from, Control to, int x, int y) { + public MonitorAwarePoint map(Control from, Control to, int x, int y) { int zoom = getZoomLevelForMapping(from, to); x = DPIUtil.scaleUp(x, zoom); y = DPIUtil.scaleUp(y, zoom); - return DPIUtil.scaleDown(display.mapInPixels(from, to, x, y), zoom); + return new MonitorAwarePoint(DPIUtil.scaleDown(display.mapInPixels(from, to, x, y), zoom)); } @Override - public Rectangle map(Control from, Control to, int x, int y, int width, int height) { + public MonitorAwareRectangle map(Control from, Control to, int x, int y, int width, int height) { int zoom = getZoomLevelForMapping(from, to); x = DPIUtil.scaleUp(x, zoom); y = DPIUtil.scaleUp(y, zoom); width = DPIUtil.scaleUp(width, zoom); height = DPIUtil.scaleUp(height, zoom); - return DPIUtil.scaleDown(display.mapInPixels(from, to, x, y, width, height), zoom); + return new MonitorAwareRectangle(DPIUtil.scaleDown(display.mapInPixels(from, to, x, y, width, height), zoom)); } @Override @@ -81,18 +81,18 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) { } @Override - public Point translateToDisplayCoordinates(Point point, int zoom) { - return DPIUtil.scaleUp(point, zoom); + public Point translateToDisplayCoordinates(MonitorAwarePoint point, int zoom) { + return DPIUtil.scaleUp(point.getPoint(), zoom); } @Override - public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) { - return DPIUtil.scaleDown(rect, zoom); + public Rectangle translateFromDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { + return DPIUtil.scaleDown(rect.getRectangle(), zoom); } @Override - public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { - return DPIUtil.scaleUp(rect, zoom); + public Rectangle translateToDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { + return DPIUtil.scaleUp(rect.getRectangle(), zoom); } @Override From a6d8c147ce4123b0189c6d789681c2bf3b6e0485 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:54:12 -0700 Subject: [PATCH 7/8] Consequences for `MultiZoomCoordinateSystemMapper`. --- .../MultiZoomCoordinateSystemMapper.java | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 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..e02d382884f 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 @@ -34,53 +34,49 @@ private MultiZoomCoordinateSystemMapper(Display display) { } @Override - public Point map(Control from, Control to, Point point) { + public MonitorAwarePoint map(Control from, Control to, Point point) { return map(from, to, point.x, point.y); } @Override - public Rectangle map(Control from, Control to, Rectangle rectangle) { + public MonitorAwareRectangle map(Control from, Control to, Rectangle rectangle) { return map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height); } @Override - public Point map(Control from, Control to, int x, int y) { - Point mappedPointInPoints; + public MonitorAwarePoint map(Control from, Control to, int x, int y) { if (from == null) { Point mappedPointInpixels = display.mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y)); - mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); + return new MonitorAwarePoint(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, + return getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y); } else { Point mappedPointInpixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom())); - mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); + return new MonitorAwarePoint(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; + public MonitorAwareRectangle map(Control from, Control to, int x, int y, int width, int height) { if (from == null) { Rectangle mappedRectangleInPixels = display.mapInPixels(from, to, translateRectangleInPointsToPixels(x, y, width, height, to.getShell().getMonitor())); - mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); + return new MonitorAwareRectangle(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, + return translateRectangleInPixelsToPoints(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor()); } else { Rectangle mappedRectangleInPixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom())); - mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); + return new MonitorAwareRectangle(DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom())); } - return mappedRectangleInPoints; } @Override @@ -93,31 +89,28 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) { @Override public Point translateFromDisplayCoordinates(Point point, int zoom) { - return translateLocationInPixelsToPoints(point.x, point.y); + return translateLocationInPixelsToPoints(point.x, point.y).getPoint(); } @Override - public Point translateToDisplayCoordinates(Point point, int zoom) { - Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null; - return translateLocationInPointsToPixels(point.x, point.y, monitor); + public Point translateToDisplayCoordinates(MonitorAwarePoint point, int zoom) { + return translateLocationInPointsToPixels(point.getPoint().x, point.getPoint().y, point.getMonitor()); } @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); + public Rectangle translateFromDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { + return translateRectangleInPixelsToPoints(rect.getRectangle().x, rect.getRectangle().y, rect.getRectangle().width, rect.getRectangle().height, rect.getMonitor()).getRectangle(); } @Override - public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) { - Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null; - return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor); + public Rectangle translateToDisplayCoordinates(MonitorAwareRectangle rect, int zoom) { + return translateRectangleInPointsToPixels(rect.getRectangle().x, rect.getRectangle().y, rect.getRectangle().width, rect.getRectangle().height, rect.getMonitor()); } @Override public Point getCursorLocation() { Point cursorLocationInPixels = display.getCursorLocationInPixels(); - return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y); + return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y).getPoint(); } @Override @@ -131,7 +124,7 @@ private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) { return getPixelsFromPoint(monitor, x, y); } - private Point translateLocationInPixelsToPoints(int x, int y) { + private MonitorAwarePoint translateLocationInPixelsToPoints(int x, int y) { Monitor monitor = getContainingMonitorForPixels(x, y); return getPointFromPixels(monitor, x, y); } @@ -145,15 +138,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 MonitorAwareRectangle 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); + MonitorAwarePoint 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 MonitorAwareRectangle(topLeft.getPoint().x, topLeft.getPoint().y, width, height, monitor); } private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) { @@ -257,7 +249,7 @@ private Point getPixelsFromPoint(Monitor monitor, int x, int y) { return new Point(mappedX, mappedY); } - private Point getPointFromPixels(Monitor monitor, int x, int y) { + private MonitorAwarePoint 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; From 599f870dd88c57aee1469b7ca2a25daf440a5dfd Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 13 Mar 2025 10:54:24 -0700 Subject: [PATCH 8/8] Consequences for `CoordinateSystemMapperTests`. --- .../widgets/CoordinateSystemMapperTests.java | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) 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 9f207ee7a8b..cbe638f6371 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 @@ -63,9 +63,9 @@ private SingleZoomCoordinateSystemMapper getSingleZoomCoordinateSystemMapper() { @MethodSource("provideCoordinateSystemMappers") void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { setupMonitors(mapper); - Point pt = createExpectedPoint(mapper, 5000, -400, monitors[0]); + MonitorAwarePoint pt = createExpectedPoint(mapper, 5000, -400, monitors[0]); Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom()); - assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom())); + assertEquals(pt.getPoint(), mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom())); } @Test @@ -73,7 +73,7 @@ void translatePointInGapBackAndForthInSingleZoomShouldBeTheSame() { SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper(); setupMonitors(mapper); Point pt = new Point(1900, 400); - Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom()); + Point px = mapper.translateToDisplayCoordinates(new MonitorAwarePoint(pt), monitors[0].getZoom()); assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom())); } @@ -82,9 +82,9 @@ void translatePointInGapBackAndForthInMultiZoomShouldEndInsideTheSameMonitor() { MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); Point pt = new Point(1900, 400); - Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom()); + Point px = mapper.translateToDisplayCoordinates(new MonitorAwarePoint(pt), monitors[0].getZoom()); Point translatedPt = mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()); - Point translatedPx = mapper.translateToDisplayCoordinates(translatedPt, monitors[0].getZoom()); + Point translatedPx = mapper.translateToDisplayCoordinates(new MonitorAwarePoint(translatedPt), monitors[0].getZoom()); assertEquals(new Point(translatedPt.x, translatedPt.y), translatedPx); assertEquals(translatedPx, px); } @@ -93,9 +93,9 @@ void translatePointInGapBackAndForthInMultiZoomShouldEndInsideTheSameMonitor() { @MethodSource("provideCoordinateSystemMappers") void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { setupMonitors(mapper); - Rectangle rectInPts = createExpectedRectangle(mapper, 5000, -400, 200, 200, monitors[0]); + MonitorAwareRectangle rectInPts = createExpectedRectangle(mapper, 5000, -400, 200, 200, monitors[0]); Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + assertEquals(rectInPts.getRectangle(), mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @Test @@ -103,8 +103,8 @@ void translateRectangleInGapBackAndForthInSingleZoomShouldBeTheSame() { SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper(); setupMonitors(mapper); Rectangle rectInPts = new Rectangle(1800, 400, 100, 100); - Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom()); + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @Test @@ -112,8 +112,8 @@ void translateRectangleInGapBackAndForthInMultiZoomShouldBeInMonitorBounds() { MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); Rectangle rectInPts = new Rectangle(1800, 400, 100, 100); - Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom()); + Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom()); boolean isInsideMonitor = false; for (Monitor monitor : monitors) { if (monitor.getClientArea().intersects(rectInPtsTranslated)) { @@ -129,26 +129,26 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper(); setupMonitors(mapper); Rectangle rectInPts = new Rectangle(1950, 400, 150, 100); - Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom()); + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @Test void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() { MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); - Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]); + MonitorAwareRectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]); Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + assertEquals(rectInPts.getRectangle(), mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @ParameterizedTest @MethodSource("provideCoordinateSystemMappers") void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { setupMonitors(mapper); - Rectangle rectInPts = createExpectedRectangle(mapper, 750, 400, 100, 100, monitors[0]); + MonitorAwareRectangle rectInPts = createExpectedRectangle(mapper, 750, 400, 100, 100, monitors[0]); Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + assertEquals(rectInPts.getRectangle(), mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @Test @@ -156,8 +156,8 @@ void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthInSingleZoomSh SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper(); setupMonitors(mapper); Rectangle rectInPts = new Rectangle(950, 400, 1500, 100); - Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom()); + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom())); } @Test @@ -165,9 +165,9 @@ void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthInMultiZoomSho MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper(); setupMonitors(mapper); Rectangle rectInPts = new Rectangle(950, 400, 1500, 100); - Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); - Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); - Rectangle rectInPxsTranslated = mapper.translateToDisplayCoordinates(rectInPtsTranslated, + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom()); + Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom()); + Rectangle rectInPxsTranslated = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPtsTranslated), monitors[0].getZoom()); assertEquals(rectInPxs, rectInPxsTranslated); } @@ -182,13 +182,13 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height / 2) - 200; expectedSmallRectInPxs.width = 400; expectedSmallRectInPxs.height = 400; - Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom()); Rectangle smallRectInPts = new Rectangle(0, 0, 0, monitors[0].getZoom()); smallRectInPts.x = rectInPts.x + (rectInPts.width / 2) - 200; smallRectInPts.y = rectInPts.y + (rectInPts.height / 2) - 200; smallRectInPts.width = 400; smallRectInPts.height = 400; - Rectangle smallRectInPxs = mapper.translateToDisplayCoordinates(smallRectInPts, monitors[0].getZoom()); + Rectangle smallRectInPxs = mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(smallRectInPts), monitors[0].getZoom()); assertEquals(expectedSmallRectInPxs, smallRectInPxs); } @@ -197,8 +197,8 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { setupMonitors(mapper); Rectangle rectInPxs = new Rectangle(400, 2400, 1000, 1000); - Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); - assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom())); + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom()); + assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom())); } @ParameterizedTest @@ -206,21 +206,21 @@ void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(Coordi void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { setupMonitors(mapper); Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500); - Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()); - assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom())); + Rectangle rectInPts = mapper.translateFromDisplayCoordinates(new MonitorAwareRectangle(rectInPxs), monitors[0].getZoom()); + assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(new MonitorAwareRectangle(rectInPts), monitors[0].getZoom())); } - private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) { + private MonitorAwarePoint createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) { if (mapper instanceof SingleZoomCoordinateSystemMapper) { - return new Point(x, y); + return new MonitorAwarePoint(new Point(x, y)); } else { return new MonitorAwarePoint(x, y, monitor); } } - private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) { + private MonitorAwareRectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) { if (mapper instanceof SingleZoomCoordinateSystemMapper) { - return new Rectangle(x, y, width, height); + return new MonitorAwareRectangle(new Rectangle(x, y, width, height)); } else { return new MonitorAwareRectangle(x, y, width, height, monitor); }