Skip to content

Commit d1645d4

Browse files
committed
WIP : consume the monitor context in pt and rect
1 parent e0b4fe9 commit d1645d4

File tree

7 files changed

+147
-76
lines changed

7 files changed

+147
-76
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper
6161
}
6262

6363
@ParameterizedTest
64-
@MethodSource("provideCoordinateSystemMappers")
65-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
64+
65+
@MethodSource("provideCoordinateSystemMappers")
6666
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
6767
setupMonitors(mapper);
6868
Point pt = new Point(1900, 400);
@@ -80,8 +80,7 @@ void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMa
8080
}
8181

8282
@ParameterizedTest
83-
@MethodSource("provideCoordinateSystemMappers")
84-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
83+
@MethodSource("provideCoordinateSystemMappers")
8584
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
8685
setupMonitors(mapper);
8786
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
@@ -90,8 +89,7 @@ void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper m
9089
}
9190

9291
@ParameterizedTest
93-
@MethodSource("provideCoordinateSystemMappers")
94-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
92+
@MethodSource("provideCoordinateSystemMappers")
9593
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
9694
setupMonitors(mapper);
9795
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
@@ -118,7 +116,6 @@ void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSam
118116
}
119117

120118
@Test
121-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
122119
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
123120
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
124121
setupMonitors(mapper);
@@ -139,11 +136,10 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame()
139136
}
140137

141138
@ParameterizedTest
142-
@MethodSource("provideCoordinateSystemMappers")
143-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
139+
@MethodSource("provideCoordinateSystemMappers")
144140
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
145141
setupMonitors(mapper);
146-
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
142+
Rectangle rectInPxs = new Rectangle(400, 2400, 1000, 1000);
147143
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
148144
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
149145
}

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

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

44-
public final class Point implements Serializable {
44+
public class Point implements Serializable {
4545

4646
/**
4747
* 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 class Rectangle implements Serializable {
4949

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5324,5 +5324,4 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53245324
}
53255325
return true;
53265326
}
5327-
53285327
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
class MonitorAwarePoint extends Point {
19+
20+
MonitorAwarePoint(int x, int y, Monitor monitor) {
21+
super(x, y);
22+
this.monitor = monitor;
23+
}
24+
25+
Monitor monitor;
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
public class MonitorAwareRectangle extends Rectangle {
19+
20+
public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) {
21+
super(x, y, width, height);
22+
this.monitor = monitor;
23+
}
24+
25+
Monitor monitor;
26+
}

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

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig
6666
Rectangle mappedRectangleInPoints;
6767
if (from == null) {
6868
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
69-
translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height,
69+
translateRectangleInPointsToPixels(x, y, width, height,
7070
to.getShell().getMonitor()));
7171
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
7272
} else if (to == null) {
7373
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
7474
DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
75-
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x,
75+
mappedRectangleInPoints = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x,
7676
mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height,
7777
from.getShell().getMonitor());
7878
} else {
@@ -93,122 +93,145 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {
9393

9494
@Override
9595
public Point translateFromDisplayCoordinates(Point point, int zoom) {
96-
return translateLocationInPixelsFromDisplayCoordinateSystem(point.x, point.y);
96+
return translateLocationInPixelsToPoints(point.x, point.y);
9797
}
9898

9999
@Override
100100
public Point translateToDisplayCoordinates(Point point, int zoom) {
101-
return translateLocationInPointsToDisplayCoordinateSystem(point.x, point.y);
101+
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.monitor : null;
102+
return translateLocationInPointsToPixels(point.x, point.y, monitor);
102103
}
103104

104105
@Override
105106
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
106-
return translateRectangleInPixelsFromDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
107-
rect.height);
107+
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.monitor : null;
108+
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
108109
}
109110

110111
@Override
111112
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
112-
return translateRectangleInPointsToDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
113-
rect.height);
113+
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.monitor : null;
114+
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor);
114115
}
115116

116117
@Override
117118
public Point getCursorLocation() {
118119
Point cursorLocationInPixels = display.getCursorLocationInPixels();
119-
return translateLocationInPixelsFromDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
120+
return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y);
120121
}
121122

122123
@Override
123-
public void setCursorLocation(int x, int y) {
124-
Point cursorLocationInPixels = translateLocationInPointsToDisplayCoordinateSystem(x, y);
125-
display.setCursorLocationInPixels(cursorLocationInPixels.x, cursorLocationInPixels.y);
124+
public void setCursorLocation (int x, int y) {
125+
Point cursorLocationInPixels = translateLocationInPointsToPixels(x, y, null);
126+
display.setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
126127
}
127128

128-
private Point translateLocationInPointsToDisplayCoordinateSystem(int x, int y) {
129-
Monitor monitor = getContainingMonitor(x, y);
129+
private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) {
130+
monitor = getValidMonitorIfApplicable(x, y, monitor);
130131
return getPixelsFromPoint(monitor, x, y);
131132
}
132133

133-
private Point translateLocationInPixelsFromDisplayCoordinateSystem(int x, int y) {
134-
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
134+
private Point translateLocationInPixelsToPoints(int x, int y) {
135+
Monitor monitor = getContainingMonitorForPixels(x, y);
135136
return getPointFromPixels(monitor, x, y);
136137
}
137138

138-
private Rectangle translateRectangleInPointsToDisplayCoordinateSystemByContainment(int x, int y, int width,
139-
int height) {
140-
Monitor monitorByLocation = getContainingMonitor(x, y);
141-
Monitor monitorByContainment = getContainingMonitor(x, y, width, height);
142-
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitorByLocation,
143-
monitorByContainment);
144-
}
145-
146-
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height,
147-
Monitor monitor) {
148-
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitor, monitor);
149-
}
150-
151-
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height,
152-
Monitor monitorOfLocation, Monitor monitorOfArea) {
153-
Point topLeft = getPixelsFromPoint(monitorOfLocation, x, y);
154-
int zoom = getApplicableMonitorZoom(monitorOfArea);
139+
private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, int height, Monitor monitor) {
140+
monitor = getValidMonitorIfApplicable(x, y, width, height, monitor);
141+
Point topLeft = getPixelsFromPoint(monitor, x, y);
142+
int zoom = getApplicableMonitorZoom(monitor);
155143
int widthInPixels = DPIUtil.scaleUp(width, zoom);
156144
int heightInPixels = DPIUtil.scaleUp(height, zoom);
157145
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
158146
}
159147

160-
private Rectangle translateRectangleInPixelsFromDisplayCoordinateSystemByContainment(int x, int y,
161-
int widthInPixels, int heightInPixels) {
162-
Monitor monitorByLocation = getContainingMonitor(x, y);
163-
Monitor monitorByContainment = getContainingMonitorInPixelsCoordinate(x, y, widthInPixels, heightInPixels);
164-
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels,
165-
monitorByLocation, monitorByContainment);
148+
private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
149+
if (monitor == null)
150+
monitor = getContainingMonitorForPixels(x, y, widthInPixels, heightInPixels);
151+
int zoom = getApplicableMonitorZoom(monitor);
152+
Point topLeft = getPointFromPixels(monitor, x, y);
153+
int width = DPIUtil.scaleDown(widthInPixels, zoom);
154+
int height = DPIUtil.scaleDown(heightInPixels, zoom);
155+
MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor);
156+
return rect;
166157
}
167158

168-
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels,
169-
int heightInPixels, Monitor monitor) {
170-
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitor,
171-
monitor);
159+
private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) {
160+
if(monitor != null) {
161+
if (monitor.getClientArea().intersects(x, y, width, height)) {
162+
return monitor;
163+
} else {
164+
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
165+
if (containingMonitor != null) {
166+
return containingMonitor;
167+
} else {
168+
return monitor;
169+
}
170+
}
171+
} else {
172+
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
173+
if (containingMonitor != null) {
174+
return containingMonitor;
175+
} else {
176+
return monitorSupplier.get()[0];
177+
}
178+
}
172179
}
173180

174-
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels,
175-
int heightInPixels, Monitor monitorOfLocation, Monitor monitorOfArea) {
176-
Point topLeft = getPointFromPixels(monitorOfLocation, x, y);
177-
int zoom = getApplicableMonitorZoom(monitorOfArea);
178-
int width = DPIUtil.scaleDown(widthInPixels, zoom);
179-
int height = DPIUtil.scaleDown(heightInPixels, zoom);
180-
return new Rectangle(topLeft.x, topLeft.y, width, height);
181+
private Monitor getValidMonitorIfApplicable(int x, int y, Monitor monitor) {
182+
if(monitor != null) {
183+
if (monitor.getClientArea().contains(x, y)) {
184+
return monitor;
185+
} else {
186+
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
187+
if (containingMonitor != null) {
188+
return containingMonitor;
189+
} else {
190+
return monitor;
191+
}
192+
}
193+
} else {
194+
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
195+
if (containingMonitor != null) {
196+
return containingMonitor;
197+
} else {
198+
return monitorSupplier.get()[0];
199+
}
200+
}
181201
}
182202

183-
private Monitor getContainingMonitor(int x, int y) {
203+
private Monitor getContainingMonitorForPoints(int x, int y) {
184204
Monitor[] monitors = monitorSupplier.get();
185205
for (Monitor currentMonitor : monitors) {
186206
Rectangle clientArea = currentMonitor.getClientArea();
187207
if (clientArea.contains(x, y)) {
188208
return currentMonitor;
189209
}
190210
}
191-
return monitors[0];
211+
return null;
192212
}
193213

194-
private Monitor getContainingMonitor(int x, int y, int width, int height) {
214+
private Monitor getContainingMonitorForPoints(int x, int y, int width, int height) {
195215
Rectangle rectangle = new Rectangle(x, y, width, height);
196216
Monitor[] monitors = monitorSupplier.get();
197-
Monitor selectedMonitor = monitors[0];
198-
int highestArea = 0;
217+
Monitor selectedMonitor = null;
218+
int highestIntersectionRatio = 0;
199219
for (Monitor currentMonitor : monitors) {
200-
Rectangle clientArea = currentMonitor.getClientArea();
201-
Rectangle intersection = clientArea.intersection(rectangle);
202-
int area = intersection.width * intersection.height;
203-
if (area > highestArea) {
220+
Rectangle clientArea = getMonitorClientAreaInPixels(currentMonitor);
221+
Rectangle boundsInPixel = DPIUtil.scaleUp(rectangle, currentMonitor.zoom);
222+
Rectangle intersection = clientArea.intersection(boundsInPixel);
223+
int intersectionArea = intersection.width * intersection.height;
224+
int boundsArea = boundsInPixel.width * boundsInPixel.height;
225+
int intersectionRatio = (intersectionArea * 100) / boundsArea;
226+
if (intersectionRatio > highestIntersectionRatio) {
204227
selectedMonitor = currentMonitor;
205-
highestArea = area;
228+
highestIntersectionRatio = intersectionRatio;
206229
}
207230
}
208231
return selectedMonitor;
209232
}
210233

211-
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
234+
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels) {
212235
Monitor[] monitors = monitorSupplier.get();
213236
for (Monitor current : monitors) {
214237
Rectangle clientArea = getMonitorClientAreaInPixels(current);
@@ -219,7 +242,7 @@ private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPix
219242
return monitors[0];
220243
}
221244

222-
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels, int widthInPixels,
245+
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels, int widthInPixels,
223246
int heightInPixels) {
224247
Rectangle rectangle = new Rectangle(xInPixels, yInPixels, widthInPixels, heightInPixels);
225248
Monitor[] monitors = monitorSupplier.get();
@@ -255,7 +278,8 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
255278
int zoom = getApplicableMonitorZoom(monitor);
256279
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
257280
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
258-
return new Point(mappedX, mappedY);
281+
Point pt = new MonitorAwarePoint(mappedX, mappedY, monitor);
282+
return pt;
259283
}
260284

261285
private int getApplicableMonitorZoom(Monitor monitor) {

0 commit comments

Comments
 (0)