Skip to content

Commit ab28cd8

Browse files
committed
Refactored CoordinateSystemMapper and added tests
This commit refactors the Display to separate single zoom and multi zoom coordinate system mappers in separate classes. Additionally, it adds some tests for validating the multi zoom coordinate system mapper. A few tests are disabled because of the current state of multi zoom coordinate system limitations. contributes to #62 and #127
1 parent d7febc4 commit ab28cd8

File tree

5 files changed

+578
-303
lines changed

5 files changed

+578
-303
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta Solutions and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta Solutions - initial tests
13+
*******************************************************************************/
14+
package org.eclipse.swt.widgets;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
import java.util.function.*;
19+
import java.util.stream.*;
20+
21+
import org.eclipse.swt.graphics.*;
22+
import org.eclipse.swt.internal.*;
23+
import org.junit.jupiter.api.*;
24+
import org.junit.jupiter.params.*;
25+
import org.junit.jupiter.params.provider.*;
26+
27+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
28+
public class CoordinateSystemMapperTests {
29+
30+
Supplier<Monitor[]> getMonitorConsumer;
31+
Monitor[] monitors;
32+
33+
private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInPixels, int nativeZoom) {
34+
Monitor monitor = new Monitor();
35+
Rectangle bounds = mapper.mapMonitorBounds(boundsInPixels, DPIUtil.getZoomForAutoscaleProperty(nativeZoom));
36+
monitor.setBounds(bounds);
37+
monitor.setClientArea(bounds);
38+
monitor.zoom = nativeZoom;
39+
return monitor;
40+
}
41+
42+
void setupMonitors(CoordinateSystemMapper mapper) {
43+
Rectangle boundsInPixelsForLeftMonitor = new Rectangle(0, 0, 2000, 2000);
44+
Rectangle boundsInPixelsForRightMonitor = new Rectangle(2000, 0, 2000, 2000);
45+
monitors = new Monitor [] {
46+
createMonitor(mapper, boundsInPixelsForLeftMonitor, 200),
47+
createMonitor(mapper, boundsInPixelsForRightMonitor, 100)
48+
};
49+
}
50+
51+
Stream<CoordinateSystemMapper> provideCoordinateSystemMappers() {
52+
return Stream.of(
53+
new MultiZoomCoordinateSystemMapper(null, () -> monitors),
54+
new SingleZoomCoordinateSystemMapper(null)
55+
);
56+
}
57+
58+
@ParameterizedTest
59+
@MethodSource("provideCoordinateSystemMappers")
60+
void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
61+
setupMonitors(mapper);
62+
Point pt = new Point(5000, -400);
63+
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
64+
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
65+
}
66+
67+
@ParameterizedTest
68+
@MethodSource("provideCoordinateSystemMappers")
69+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
70+
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
71+
setupMonitors(mapper);
72+
Point pt = new Point(1900, 400);
73+
Point px = mapper.translateToDisplayCoordinates(pt, monitors[0].getZoom());
74+
assertEquals(pt, mapper.translateFromDisplayCoordinates(px, monitors[0].getZoom()));
75+
}
76+
77+
@ParameterizedTest
78+
@MethodSource("provideCoordinateSystemMappers")
79+
void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
80+
setupMonitors(mapper);
81+
Rectangle rectInPts = new Rectangle(5000, -400, 200, 200);
82+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
83+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
84+
}
85+
86+
@ParameterizedTest
87+
@MethodSource("provideCoordinateSystemMappers")
88+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
89+
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
90+
setupMonitors(mapper);
91+
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
92+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
93+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
94+
}
95+
96+
@ParameterizedTest
97+
@MethodSource("provideCoordinateSystemMappers")
98+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
99+
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
100+
setupMonitors(mapper);
101+
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
102+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
103+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
104+
}
105+
106+
@ParameterizedTest
107+
@MethodSource("provideCoordinateSystemMappers")
108+
void translateRectangleInGapPartiallyInLeftBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
109+
setupMonitors(mapper);
110+
Rectangle rectInPts = new Rectangle(750, 400, 100, 100);
111+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
112+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
113+
}
114+
115+
@ParameterizedTest
116+
@MethodSource("provideCoordinateSystemMappers")
117+
void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
118+
setupMonitors(mapper);
119+
Rectangle rectInPts = new Rectangle(950, 400, 1500, 100);
120+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
121+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
122+
}
123+
124+
@Test
125+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
126+
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
127+
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
128+
setupMonitors(mapper);
129+
Rectangle rectInPxs = new Rectangle(1990, -10, 2000, 2000);
130+
Rectangle expectedSmallRectInPxs = new Rectangle(0, 0, 0, monitors[0].getZoom());
131+
expectedSmallRectInPxs.x = rectInPxs.x + (rectInPxs.width/2) - 200;
132+
expectedSmallRectInPxs.y = rectInPxs.y + (rectInPxs.height/2) - 200;
133+
expectedSmallRectInPxs.width = 400;
134+
expectedSmallRectInPxs.height = 400;
135+
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
136+
Rectangle smallRectInPts = new Rectangle(0, 0, 0, monitors[0].getZoom());
137+
smallRectInPts.x = rectInPts.x + (rectInPts.width/2) - 200;
138+
smallRectInPts.y = rectInPts.y + (rectInPts.height/2) - 200;
139+
smallRectInPts.width = 400;
140+
smallRectInPts.height = 400;
141+
Rectangle smallRectInPxs = mapper.translateToDisplayCoordinates(smallRectInPts, monitors[0].getZoom());
142+
assertEquals(expectedSmallRectInPxs, smallRectInPxs);
143+
}
144+
145+
@ParameterizedTest
146+
@MethodSource("provideCoordinateSystemMappers")
147+
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
148+
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
149+
setupMonitors(mapper);
150+
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
151+
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
152+
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
153+
}
154+
155+
@ParameterizedTest
156+
@MethodSource("provideCoordinateSystemMappers")
157+
void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
158+
setupMonitors(mapper);
159+
Rectangle rectInPxs = new Rectangle(1500, 400, 502, 500);
160+
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
161+
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
162+
}
163+
164+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta Solutions and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta Solutions - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.widgets;
15+
16+
import org.eclipse.swt.graphics.*;
17+
18+
interface CoordinateSystemMapper {
19+
20+
Rectangle map(Control from, Control to, Rectangle rectangle);
21+
22+
Rectangle map(Control from, Control to, int x, int y, int width, int height);
23+
24+
Point map(Control from, Control to, Point point);
25+
26+
Point map(Control from, Control to, int x, int y);
27+
28+
Rectangle mapMonitorBounds(Rectangle rectangle, int zoom);
29+
30+
Point translateFromDisplayCoordinates(Point point, int zoom);
31+
32+
Point translateToDisplayCoordinates(Point point, int zoom);
33+
34+
Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom);
35+
36+
Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom);
37+
38+
void setCursorLocation(int x, int y);
39+
40+
Point getCursorLocation();
41+
}

0 commit comments

Comments
 (0)