Skip to content

Commit 66ba81c

Browse files
committed
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 #62 and #128
1 parent e40ad29 commit 66ba81c

File tree

7 files changed

+90
-139
lines changed

7 files changed

+90
-139
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS
137137
void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() {
138138
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
139139
setupMonitors(mapper);
140-
Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]);
140+
Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]);
141141
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
142142
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
143143
}
@@ -223,15 +223,15 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M
223223
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
224224
return new Point(x, y);
225225
} else {
226-
return new MonitorAwarePoint(x, y, monitor);
226+
return new Point.WithMonitor(x, y, monitor);
227227
}
228228
}
229229

230230
private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) {
231231
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
232232
return new Rectangle(x, y, width, height);
233233
} else {
234-
return new MonitorAwareRectangle(x, y, width, height, monitor);
234+
return new Rectangle.WithMonitor(x, y, width, height, monitor);
235235
}
236236
}
237237

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.io.*;
1818

19+
import org.eclipse.swt.widgets.*;
20+
1921
/**
2022
* Instances of this class represent places on the (x, y)
2123
* coordinate plane.
@@ -41,7 +43,7 @@
4143
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4244
*/
4345

44-
public sealed class Point implements Serializable permits MonitorAwarePoint {
46+
public sealed class Point implements Serializable permits Point.WithMonitor {
4547

4648
/**
4749
* the x coordinate of the point
@@ -116,5 +118,41 @@ public String toString () {
116118
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117119
}
118120

121+
/**
122+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
123+
* objects along with the context of the monitor in relation to which they are
124+
* placed on the display. The monitor awareness makes it easy to scale and
125+
* translate the points between pixels and points.
126+
*
127+
* @since 3.130
128+
* @noreference This class is not intended to be referenced by clients
129+
*/
130+
public static final class WithMonitor extends Point {
131+
132+
private static final long serialVersionUID = 6077427420686999194L;
133+
134+
private final Monitor monitor;
135+
136+
/**
137+
* Constructs a new Point.WithMonitor
138+
*
139+
* @param x the x coordinate of the point
140+
* @param y the y coordinate of the point
141+
* @param monitor the monitor with whose context the point is created
142+
*/
143+
public WithMonitor(int x, int y, Monitor monitor) {
144+
super(x, y);
145+
this.monitor = monitor;
146+
}
147+
148+
/**
149+
* {@return the monitor with whose context the instance is created}
150+
*/
151+
public Monitor getMonitor() {
152+
return monitor;
153+
}
154+
155+
}
156+
119157
}
120158

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.*;
1818

1919
import org.eclipse.swt.*;
20+
import org.eclipse.swt.widgets.*;
2021

2122
/**
2223
* Instances of this class represent rectangular areas in an
@@ -45,7 +46,7 @@
4546
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4647
*/
4748

48-
public sealed class Rectangle implements Serializable permits MonitorAwareRectangle {
49+
public sealed class Rectangle implements Serializable permits Rectangle.WithMonitor {
4950

5051
/**
5152
* the x coordinate of the rectangle
@@ -356,4 +357,42 @@ public Rectangle union (Rectangle rect) {
356357
return new Rectangle (left, top, right - left, bottom - top);
357358
}
358359

360+
/**
361+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle}
362+
* objects along with the context of the monitor in relation to which they are
363+
* placed on the display. The monitor awareness makes it easy to scale and
364+
* translate the rectangles between pixels and points.
365+
*
366+
* @since 3.130
367+
* @noreference This class is not intended to be referenced by clients
368+
*/
369+
public static final class WithMonitor extends Rectangle {
370+
371+
private static final long serialVersionUID = 5041911840525116925L;
372+
373+
private final Monitor monitor;
374+
375+
/**
376+
* Constructs a new Rectangle.WithMonitor
377+
*
378+
* @param x the x coordinate of the top left corner of the rectangle
379+
* @param y the y coordinate of the top left corner of the rectangle
380+
* @param width the width of the rectangle
381+
* @param height the height of the rectangle
382+
* @param monitor the monitor with whose context the rectangle is created
383+
*/
384+
public WithMonitor(int x, int y, int width, int height, Monitor monitor) {
385+
super(x, y, width, height);
386+
this.monitor = monitor;
387+
}
388+
389+
/**
390+
* {@return the monitor with whose context the instance is created}
391+
*/
392+
public Monitor getMonitor() {
393+
return monitor;
394+
}
395+
396+
}
397+
359398
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) {
9898

9999
@Override
100100
public Point translateToDisplayCoordinates(Point point, int zoom) {
101-
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null;
101+
Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null;
102102
return translateLocationInPointsToPixels(point.x, point.y, monitor);
103103
}
104104

105105
@Override
106106
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
107-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
107+
Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null;
108108
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
109109
}
110110

111111
@Override
112112
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
113-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
113+
Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null;
114114
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor);
115115
}
116116

@@ -152,7 +152,7 @@ private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPi
152152
Point topLeft = getPointFromPixels(monitor, x, y);
153153
int width = DPIUtil.scaleDown(widthInPixels, zoom);
154154
int height = DPIUtil.scaleDown(heightInPixels, zoom);
155-
MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor);
155+
Rectangle.WithMonitor rect = new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitor);
156156
return rect;
157157
}
158158

@@ -264,7 +264,7 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
264264
int zoom = getApplicableMonitorZoom(monitor);
265265
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
266266
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
267-
return new MonitorAwarePoint(mappedX, mappedY, monitor);
267+
return new Point.WithMonitor(mappedX, mappedY, monitor);
268268
}
269269

270270
private int getApplicableMonitorZoom(Monitor monitor) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8176,8 +8176,8 @@ private LRESULT positionTooltip(NMHDR hdr, long wParam, long lParam, boolean man
81768176
// of positioning and re-scaling events.
81778177
// Refer: https://github.com/eclipse-platform/eclipse.platform.swt/issues/557
81788178
Point cursorLocation = display.getCursorLocation();
8179-
Rectangle monitorBounds = cursorLocation instanceof MonitorAwarePoint monitorAwarePoint
8180-
? getContainingMonitorBoundsInMultiZoomCoordinateSystem(monitorAwarePoint)
8179+
Rectangle monitorBounds = cursorLocation instanceof Point.WithMonitor pointWithMonitor
8180+
? getContainingMonitorBoundsInMultiZoomCoordinateSystem(pointWithMonitor)
81818181
: getContainingMonitorBoundsInSingleZoomCoordinateSystem(cursorLocation);
81828182
if (monitorBounds != null) {
81838183
Rectangle adjustedTooltipBounds = fitTooltipBoundsIntoMonitor(toolRect, monitorBounds);
@@ -8229,7 +8229,7 @@ private Rectangle getContainingMonitorBoundsInSingleZoomCoordinateSystem(Point p
82298229
return null;
82308230
}
82318231

8232-
private Rectangle getContainingMonitorBoundsInMultiZoomCoordinateSystem(MonitorAwarePoint point) {
8232+
private Rectangle getContainingMonitorBoundsInMultiZoomCoordinateSystem(Point.WithMonitor point) {
82338233
Monitor monitor = point.getMonitor();
82348234
return new Rectangle(monitor.x, monitor.y, DPIUtil.scaleUp(monitor.width, monitor.zoom),
82358235
DPIUtil.scaleUp(monitor.height, monitor.zoom));

0 commit comments

Comments
 (0)