Skip to content

Commit 6cbb93d

Browse files
committed
WIP : consume the monitor context in pt and rect
1 parent ab28cd8 commit 6cbb93d

File tree

5 files changed

+92
-68
lines changed

5 files changed

+92
-68
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper
6666

6767
@ParameterizedTest
6868
@MethodSource("provideCoordinateSystemMappers")
69-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
7069
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
7170
setupMonitors(mapper);
7271
Point pt = new Point(1900, 400);
@@ -85,7 +84,6 @@ void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMa
8584

8685
@ParameterizedTest
8786
@MethodSource("provideCoordinateSystemMappers")
88-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
8987
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
9088
setupMonitors(mapper);
9189
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
@@ -95,7 +93,6 @@ void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper m
9593

9694
@ParameterizedTest
9795
@MethodSource("provideCoordinateSystemMappers")
98-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
9996
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
10097
setupMonitors(mapper);
10198
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
@@ -122,7 +119,6 @@ void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSam
122119
}
123120

124121
@Test
125-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
126122
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
127123
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
128124
setupMonitors(mapper);
@@ -144,10 +140,9 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame()
144140

145141
@ParameterizedTest
146142
@MethodSource("provideCoordinateSystemMappers")
147-
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
148143
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
149144
setupMonitors(mapper);
150-
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
145+
Rectangle rectInPxs = new Rectangle(400, 2400, 1000, 1000);
151146
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
152147
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
153148
}

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

Lines changed: 4 additions & 0 deletions
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.
@@ -53,6 +55,8 @@ public final class Point implements Serializable {
5355
*/
5456
public int y;
5557

58+
public Monitor monitor;
59+
5660
static final long serialVersionUID = 3257002163938146354L;
5761

5862
/**

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

Lines changed: 3 additions & 0 deletions
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
@@ -67,6 +68,8 @@ public final class Rectangle implements Serializable {
6768
*/
6869
public int height;
6970

71+
public Monitor monitor;
72+
7073
static final long serialVersionUID = 3256439218279428914L;
7174

7275
/**

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
@@ -5391,5 +5391,4 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53915391
}
53925392
return true;
53935393
}
5394-
53955394
}

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

Lines changed: 84 additions & 61 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,94 +93,112 @@ 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+
return translateLocationInPointsToPixels(point.x, point.y, point.monitor);
102102
}
103103

104104
@Override
105105
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
106-
return translateRectangleInPixelsFromDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
107-
rect.height);
106+
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, rect.monitor);
108107
}
109108

110109
@Override
111110
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
112-
return translateRectangleInPointsToDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
113-
rect.height);
111+
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, rect.monitor);
114112
}
115113

116114
@Override
117115
public Point getCursorLocation() {
118116
Point cursorLocationInPixels = display.getCursorLocationInPixels();
119-
return translateLocationInPixelsFromDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
117+
return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y);
120118
}
121119

122120
@Override
123-
public void setCursorLocation(int x, int y) {
124-
Point cursorLocationInPixels = translateLocationInPointsToDisplayCoordinateSystem(x, y);
125-
display.setCursorLocationInPixels(cursorLocationInPixels.x, cursorLocationInPixels.y);
121+
public void setCursorLocation (int x, int y) {
122+
Point cursorLocationInPixels = translateLocationInPointsToPixels(x, y, null);
123+
display.setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
126124
}
127125

128-
private Point translateLocationInPointsToDisplayCoordinateSystem(int x, int y) {
129-
Monitor monitor = getContainingMonitor(x, y);
126+
private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) {
127+
monitor = getValidMonitorIfApplicable(x, y, monitor);
130128
return getPixelsFromPoint(monitor, x, y);
131129
}
132130

133-
private Point translateLocationInPixelsFromDisplayCoordinateSystem(int x, int y) {
134-
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
131+
private Point translateLocationInPixelsToPoints(int x, int y) {
132+
Monitor monitor = getContainingMonitorForPixels(x, y);
135133
return getPointFromPixels(monitor, x, y);
136134
}
137135

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);
136+
private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, int height, Monitor monitor) {
137+
monitor = getValidMonitorIfApplicable(x, y, width, height, monitor);
138+
Point topLeft = getPixelsFromPoint(monitor, x, y);
139+
int zoom = getApplicableMonitorZoom(monitor);
155140
int widthInPixels = DPIUtil.scaleUp(width, zoom);
156141
int heightInPixels = DPIUtil.scaleUp(height, zoom);
157142
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
158143
}
159144

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);
145+
private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
146+
if (monitor == null)
147+
monitor = getContainingMonitorForPixels(x, y, widthInPixels, heightInPixels);
148+
int zoom = getApplicableMonitorZoom(monitor);
149+
Point topLeft = getPointFromPixels(monitor, x, y);
150+
int width = DPIUtil.scaleDown(widthInPixels, zoom);
151+
int height = DPIUtil.scaleDown(heightInPixels, zoom);
152+
Rectangle rect = new Rectangle(topLeft.x, topLeft.y, width, height);
153+
rect.monitor = monitor;
154+
return rect;
166155
}
167156

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);
157+
private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) {
158+
if(monitor != null) {
159+
if (monitor.getClientArea().intersects(x, y, width, height)) {
160+
return monitor;
161+
} else {
162+
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
163+
if (containingMonitor != null) {
164+
return containingMonitor;
165+
} else {
166+
return monitor;
167+
}
168+
}
169+
} else {
170+
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
171+
if (containingMonitor != null) {
172+
return containingMonitor;
173+
} else {
174+
return monitorSupplier.get()[0];
175+
}
176+
}
172177
}
173178

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);
179+
private Monitor getValidMonitorIfApplicable(int x, int y, Monitor monitor) {
180+
if(monitor != null) {
181+
if (monitor.getClientArea().contains(x, y)) {
182+
return monitor;
183+
} else {
184+
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
185+
if (containingMonitor != null) {
186+
return containingMonitor;
187+
} else {
188+
return monitor;
189+
}
190+
}
191+
} else {
192+
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
193+
if (containingMonitor != null) {
194+
return containingMonitor;
195+
} else {
196+
return monitorSupplier.get()[0];
197+
}
198+
}
181199
}
182200

183-
private Monitor getContainingMonitor(int x, int y) {
201+
private Monitor getContainingMonitorForPoints(int x, int y) {
184202
Monitor[] monitors = monitorSupplier.get();
185203
for (Monitor currentMonitor : monitors) {
186204
Rectangle clientArea = currentMonitor.getClientArea();
@@ -191,24 +209,27 @@ private Monitor getContainingMonitor(int x, int y) {
191209
return monitors[0];
192210
}
193211

194-
private Monitor getContainingMonitor(int x, int y, int width, int height) {
212+
private Monitor getContainingMonitorForPoints(int x, int y, int width, int height) {
195213
Rectangle rectangle = new Rectangle(x, y, width, height);
196214
Monitor[] monitors = monitorSupplier.get();
197215
Monitor selectedMonitor = monitors[0];
198-
int highestArea = 0;
216+
int highestIntersectionRatio = 0;
199217
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) {
218+
Rectangle clientArea = getMonitorClientAreaInPixels(currentMonitor);
219+
Rectangle boundsInPixel = DPIUtil.scaleUp(rectangle, currentMonitor.zoom);
220+
Rectangle intersection = clientArea.intersection(boundsInPixel);
221+
int intersectionArea = intersection.width * intersection.height;
222+
int boundsArea = boundsInPixel.width * boundsInPixel.height;
223+
int intersectionRatio = (intersectionArea * 100) / boundsArea;
224+
if (intersectionRatio > highestIntersectionRatio) {
204225
selectedMonitor = currentMonitor;
205-
highestArea = area;
226+
highestIntersectionRatio = intersectionRatio;
206227
}
207228
}
208229
return selectedMonitor;
209230
}
210231

211-
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
232+
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels) {
212233
Monitor[] monitors = monitorSupplier.get();
213234
for (Monitor current : monitors) {
214235
Rectangle clientArea = getMonitorClientAreaInPixels(current);
@@ -219,7 +240,7 @@ private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPix
219240
return monitors[0];
220241
}
221242

222-
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels, int widthInPixels,
243+
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels, int widthInPixels,
223244
int heightInPixels) {
224245
Rectangle rectangle = new Rectangle(xInPixels, yInPixels, widthInPixels, heightInPixels);
225246
Monitor[] monitors = monitorSupplier.get();
@@ -255,7 +276,9 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
255276
int zoom = getApplicableMonitorZoom(monitor);
256277
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
257278
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
258-
return new Point(mappedX, mappedY);
279+
Point pt = new Point(mappedX, mappedY);
280+
pt.monitor = monitor;
281+
return pt;
259282
}
260283

261284
private int getApplicableMonitorZoom(Monitor monitor) {

0 commit comments

Comments
 (0)