Skip to content

Commit 4a11d0b

Browse files
committed
Use MonitorAware Coordinates in multi zoom
This commit contributes to the use of MonitorAware points and rectangles in the multi zoom coordinate system mapper for translation between points and pixels. contributes to #62 and #127
1 parent e0b4fe9 commit 4a11d0b

File tree

6 files changed

+220
-91
lines changed

6 files changed

+220
-91
lines changed

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

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.widgets;
1515

16+
import static org.junit.Assert.assertTrue;
1617
import static org.junit.jupiter.api.Assertions.assertEquals;
1718

1819
import java.util.function.*;
@@ -39,16 +40,24 @@ private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInP
3940
return monitor;
4041
}
4142

42-
void setupMonitors(CoordinateSystemMapper mapper) {
43+
private void setupMonitors(CoordinateSystemMapper mapper) {
4344
Rectangle boundsInPixelsForLeftMonitor = new Rectangle(0, 0, 2000, 2000);
4445
Rectangle boundsInPixelsForRightMonitor = new Rectangle(2000, 0, 2000, 2000);
4546
monitors = new Monitor[] { createMonitor(mapper, boundsInPixelsForLeftMonitor, 200),
4647
createMonitor(mapper, boundsInPixelsForRightMonitor, 100) };
4748
}
4849

49-
Stream<CoordinateSystemMapper> provideCoordinateSystemMappers() {
50-
return Stream.of(new MultiZoomCoordinateSystemMapper(null, () -> monitors),
51-
new SingleZoomCoordinateSystemMapper(null));
50+
private Stream<CoordinateSystemMapper> provideCoordinateSystemMappers() {
51+
return Stream.of(getMultiZoomCoordinateSystemMapper(),
52+
getSingleZoomCoordinateSystemMapper());
53+
}
54+
55+
private MultiZoomCoordinateSystemMapper getMultiZoomCoordinateSystemMapper() {
56+
return new MultiZoomCoordinateSystemMapper(null, () -> monitors);
57+
}
58+
59+
private SingleZoomCoordinateSystemMapper getSingleZoomCoordinateSystemMapper() {
60+
return new SingleZoomCoordinateSystemMapper(null);
5261
}
5362

5463
@ParameterizedTest
@@ -60,16 +69,27 @@ void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper
6069
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
6170
}
6271

63-
@ParameterizedTest
64-
@MethodSource("provideCoordinateSystemMappers")
65-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
66-
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
72+
@Test
73+
void translatePointInGapBackAndForthInSingleZoomShouldBeTheSame() {
74+
SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper();
6775
setupMonitors(mapper);
6876
Point pt = new Point(1900, 400);
6977
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
7078
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
7179
}
7280

81+
@Test
82+
void translatePointInGapBackAndForthInMultiZoomShouldEndInsideTheSameMonitor() {
83+
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
84+
setupMonitors(mapper);
85+
Point pt = new Point(1900, 400);
86+
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
87+
Point translatedPt = mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom());
88+
Point translatedPx = mapper.translateToDisplayCoordinates(translatedPt, monitors[0].getZoom());
89+
assertEquals(translatedPt, translatedPx);
90+
assertEquals(translatedPx, px);
91+
}
92+
7393
@ParameterizedTest
7494
@MethodSource("provideCoordinateSystemMappers")
7595
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
@@ -79,22 +99,46 @@ void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMa
7999
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
80100
}
81101

82-
@ParameterizedTest
83-
@MethodSource("provideCoordinateSystemMappers")
84-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
85-
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
102+
@Test
103+
void translateRectangleInGapBackAndForthInSingleZoomShouldBeTheSame() {
104+
SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper();
86105
setupMonitors(mapper);
87106
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
88107
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
89108
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
90109
}
91110

92-
@ParameterizedTest
93-
@MethodSource("provideCoordinateSystemMappers")
94-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
95-
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
111+
@Test
112+
void translateRectangleInGapBackAndForthInMultiZoomShouldBeInMonitorBounds() {
113+
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
96114
setupMonitors(mapper);
97-
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
115+
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
116+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
117+
Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
118+
boolean isInsideMonitor = false;
119+
for (Monitor monitor : monitors) {
120+
if (monitor.getClientArea().intersects(rectInPtsTranslated)) {
121+
isInsideMonitor = true;
122+
break;
123+
}
124+
}
125+
assertTrue("The translated rectangle in points is inside the monitor bounds in points", isInsideMonitor);
126+
}
127+
128+
@Test
129+
void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheSame() {
130+
SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper();
131+
setupMonitors(mapper);
132+
Rectangle rectInPts = new Rectangle(1950, 400, 150, 100);
133+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
134+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
135+
}
136+
137+
@Test
138+
void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() {
139+
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
140+
setupMonitors(mapper);
141+
Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]);
98142
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
99143
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
100144
}
@@ -108,19 +152,29 @@ void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame(Coordinat
108152
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
109153
}
110154

111-
@ParameterizedTest
112-
@MethodSource("provideCoordinateSystemMappers")
113-
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
155+
@Test
156+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthInSingleZoomShouldBeTheSame() {
157+
SingleZoomCoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper();
114158
setupMonitors(mapper);
115159
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
116160
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
117161
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
118162
}
119163

120164
@Test
121-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
165+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthInMultiZoomShouldNotEndUpInGap() {
166+
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
167+
setupMonitors(mapper);
168+
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
169+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
170+
Rectangle rectInPtsTranslated = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
171+
Rectangle rectInPxsTranslated = mapper.translateToDisplayCoordinates(rectInPtsTranslated, monitors[0].getZoom());
172+
assertEquals(rectInPxs, rectInPxsTranslated);
173+
}
174+
175+
@Test
122176
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
123-
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
177+
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
124178
setupMonitors(mapper);
125179
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
126180
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, monitors[0].getZoom());
@@ -139,11 +193,10 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame()
139193
}
140194

141195
@ParameterizedTest
142-
@MethodSource("provideCoordinateSystemMappers")
143-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
196+
@MethodSource("provideCoordinateSystemMappers")
144197
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
145198
setupMonitors(mapper);
146-
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
199+
Rectangle rectInPxs = new Rectangle(400, 2400, 1000, 1000);
147200
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
148201
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
149202
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.eclipse.swt.graphics;
2+
3+
import org.eclipse.swt.widgets.*;
4+
5+
public final class MonitorAwarePoint extends Point {
6+
7+
/**
8+
*
9+
*/
10+
private static final long serialVersionUID = 1L;
11+
12+
public MonitorAwarePoint(int x, int y, Monitor monitor) {
13+
super(x, y);
14+
this.monitor = monitor;
15+
}
16+
17+
public Monitor getMonitor() {
18+
return monitor;
19+
}
20+
21+
private final Monitor monitor;
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.eclipse.swt.graphics;
2+
3+
import org.eclipse.swt.widgets.*;
4+
5+
public final class MonitorAwareRectangle extends Rectangle {
6+
7+
/**
8+
*
9+
*/
10+
private static final long serialVersionUID = 1L;
11+
12+
public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) {
13+
super(x, y, width, height);
14+
this.monitor = monitor;
15+
}
16+
17+
public Monitor getMonitor() {
18+
return monitor;
19+
}
20+
21+
private final Monitor monitor;
22+
}

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

Lines changed: 3 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.internal.*;
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 final class Point implements Serializable {
46+
public sealed class Point implements Serializable permits MonitorAwarePoint {
4547

4648
/**
4749
* the x coordinate of the point

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4646
*/
4747

48-
public final class Rectangle implements Serializable {
48+
public sealed class Rectangle implements Serializable permits MonitorAwareRectangle {
4949

5050
/**
5151
* the x coordinate of the rectangle

0 commit comments

Comments
 (0)