Skip to content

Commit 452141c

Browse files
committed
Translate points in display coordinate space gap
This contribution fixes the current behaviour of the display coordinate system of eclipse where a point can be manually set to somewhere in the gap between the 2 monitor in the coordinate space (in the points system). This fix translates those points to inside the scaled down space of a monitor providing it the right coordinates in the display coordinate space. contributes to #62 and #127
1 parent 209d75a commit 452141c

File tree

1 file changed

+39
-17
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+39
-17
lines changed

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,7 +3006,8 @@ public Point map (Control from, Control to, int x, int y) {
30063006
checkDevice ();
30073007
Point mappedPointInPoints;
30083008
if (from == null) {
3009-
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y));
3009+
Point translatedPoint = translatePointIfInDisplayCoordinateGap(x, y);
3010+
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), translatedPoint.x, translatedPoint.y));
30103011
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
30113012
} else if (to == null) {
30123013
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
@@ -3119,7 +3120,8 @@ public Rectangle map (Control from, Control to, int x, int y, int width, int hei
31193120
checkDevice ();
31203121
Rectangle mappedRectangleInPoints;
31213122
if (from == null) {
3122-
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getShell().getMonitor()));
3123+
Point translatedPoint = translatePointIfInDisplayCoordinateGap(x, y);
3124+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(translatedPoint.x, translatedPoint.y, width, height, to.getShell().getMonitor()));
31233125
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
31243126
} else if (to == null) {
31253127
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
@@ -3147,19 +3149,21 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31473149
}
31483150

31493151
Point translateLocationInPixelsInDisplayCoordinateSystem(int x, int y) {
3150-
Monitor monitor = getContainingMonitor(x, y);
3151-
return getPixelsFromPoint(monitor, x, y);
3152+
Point translatedPoint = translatePointIfInDisplayCoordinateGap(x, y);
3153+
Monitor monitor = getContainingMonitorOrPrimaryMonitor(translatedPoint.x, translatedPoint.y);
3154+
return getPixelsFromPoint(monitor, translatedPoint.x, translatedPoint.y);
31523155
}
31533156

31543157
Point translateLocationInPointInDisplayCoordinateSystem(int x, int y) {
3155-
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
3158+
Monitor monitor = getContainingMonitorInPixelsCoordinateOrPrimaryMonitor(x, y);
31563159
return getPointFromPixels(monitor, x, y);
31573160
}
31583161

31593162
Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment(int x, int y, int width, int height) {
3160-
Monitor monitorByLocation = getContainingMonitor(x, y);
3161-
Monitor monitorByContainment = getContainingMonitor(x, y, width, height);
3162-
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitorByLocation, monitorByContainment);
3163+
Point translatedPoint = translatePointIfInDisplayCoordinateGap(x, y);
3164+
Monitor monitorByLocation = getContainingMonitorOrPrimaryMonitor(translatedPoint.x, translatedPoint.y);
3165+
Monitor monitorByContainment = getContainingMonitorOrPrimaryMonitor(translatedPoint.x, translatedPoint.y, width, height);
3166+
return translateRectangleInPixelsInDisplayCoordinateSystem(translatedPoint.x, translatedPoint.y, width, height, monitorByLocation, monitorByContainment);
31633167
}
31643168

31653169
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitor) {
@@ -3175,8 +3179,8 @@ private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int
31753179
}
31763180

31773181
Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment(int x, int y, int widthInPixels, int heightInPixels) {
3178-
Monitor monitorByLocation = getContainingMonitor(x, y);
3179-
Monitor monitorByContainment = getContainingMonitor(x, y, widthInPixels, heightInPixels);
3182+
Monitor monitorByLocation = getContainingMonitorOrPrimaryMonitor(x, y);
3183+
Monitor monitorByContainment = getContainingMonitorOrPrimaryMonitor(x, y, widthInPixels, heightInPixels);
31803184
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitorByLocation, monitorByContainment);
31813185
}
31823186

@@ -5458,18 +5462,32 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
54585462
return true;
54595463
}
54605464

5461-
private Monitor getContainingMonitor(int x, int y) {
5465+
private Monitor getContainingMonitorOrPrimaryMonitor(int x, int y) {
5466+
return getContainingMonitor(x, y).orElse(getPrimaryMonitor());
5467+
}
5468+
5469+
private Optional<Monitor> getContainingMonitor(int x, int y) {
54625470
Monitor[] monitors = getMonitors();
54635471
for (Monitor currentMonitor : monitors) {
54645472
Rectangle clientArea = currentMonitor.getClientArea();
54655473
if (clientArea.contains(x, y)) {
5466-
return currentMonitor;
5474+
return Optional.of(currentMonitor);
54675475
}
54685476
}
5469-
return getPrimaryMonitor();
5477+
return Optional.empty();
54705478
}
54715479

5472-
private Monitor getContainingMonitor(int x, int y, int width, int height) {
5480+
private Point translatePointIfInDisplayCoordinateGap(int x, int y) {
5481+
// Translate only if the point doesn't lie in the point coordinate space but in
5482+
// the pixel coordinate space (the gap between the point coordinate spaces of
5483+
// the monitors)
5484+
if(getContainingMonitor(x, y).isEmpty() && getContainingMonitorInPixelsCoordinate(x, y).isPresent()) {
5485+
return translateLocationInPointInDisplayCoordinateSystem(x, y);
5486+
}
5487+
return new Point(x, y);
5488+
}
5489+
5490+
private Monitor getContainingMonitorOrPrimaryMonitor(int x, int y, int width, int height) {
54735491
Rectangle rectangle = new Rectangle(x, y, width, height);
54745492
Monitor[] monitors = getMonitors();
54755493
Monitor selectedMonitor = getPrimaryMonitor();
@@ -5486,15 +5504,19 @@ private Monitor getContainingMonitor(int x, int y, int width, int height) {
54865504
return selectedMonitor;
54875505
}
54885506

5489-
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
5507+
private Monitor getContainingMonitorInPixelsCoordinateOrPrimaryMonitor(int xInPixels, int yInPixels) {
5508+
return getContainingMonitorInPixelsCoordinate(xInPixels, yInPixels).orElse(getPrimaryMonitor());
5509+
}
5510+
5511+
private Optional<Monitor> getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
54905512
Monitor[] monitors = getMonitors();
54915513
for (Monitor current : monitors) {
54925514
Rectangle clientArea = getMonitorClientAreaInPixels(current);
54935515
if (clientArea.contains(xInPixels, yInPixels)) {
5494-
return current;
5516+
return Optional.of(current);
54955517
}
54965518
}
5497-
return getPrimaryMonitor();
5519+
return Optional.empty();
54985520
}
54995521

55005522
private Rectangle getMonitorClientAreaInPixels(Monitor monitor) {

0 commit comments

Comments
 (0)