Skip to content

Commit

Permalink
WIP : consume the monitor context in pt and rect
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya4256 committed Jan 16, 2025
1 parent e0b4fe9 commit 144227b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
@MethodSource("provideCoordinateSystemMappers")
void translatePointInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Point pt = new Point(1900, 400);
Expand All @@ -80,8 +79,7 @@ void translateRectangleInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMa
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(1800, 400, 100, 100);
Expand All @@ -90,8 +88,7 @@ void translateRectangleInGapBackAndForthShouldBeTheSame(CoordinateSystemMapper m
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInGapPartiallyInRightBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPts = new Rectangle(1950, 400, 100, 100);
Expand All @@ -118,7 +115,6 @@ void translateRectangleInPointsInBothMonitorsPartiallyBackAndForthShouldBeTheSam
}

@Test
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame() {
CoordinateSystemMapper mapper = provideCoordinateSystemMappers().findFirst().get();
setupMonitors(mapper);
Expand All @@ -139,11 +135,10 @@ void moveRectangleInPixelsInRightMonitorsPartiallyBackAndForthShouldBeTheSame()
}

@ParameterizedTest
@MethodSource("provideCoordinateSystemMappers")
@Disabled("Disabled due to current limitations of MultiZoomCoordinateSystemMapper")
@MethodSource("provideCoordinateSystemMappers")
void translateRectangleInPixelsOutisdeMonitorsBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {
setupMonitors(mapper);
Rectangle rectInPxs = new Rectangle(4400, 400, 1000, 1000);
Rectangle rectInPxs = new Rectangle(400, 2400, 1000, 1000);
Rectangle rectInPts = mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom());
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/

public final class Point implements Serializable {
public class Point implements Serializable {

/**
* the x coordinate of the point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/

public final class Rectangle implements Serializable {
public class Rectangle implements Serializable {

/**
* the x coordinate of the rectangle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5324,5 +5324,4 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig
Rectangle mappedRectangleInPoints;
if (from == null) {
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height,
translateRectangleInPointsToPixels(x, y, width, height,
to.getShell().getMonitor()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
} else if (to == null) {
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x,
mappedRectangleInPoints = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x,
mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height,
from.getShell().getMonitor());
} else {
Expand All @@ -93,122 +93,145 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {

@Override
public Point translateFromDisplayCoordinates(Point point, int zoom) {
return translateLocationInPixelsFromDisplayCoordinateSystem(point.x, point.y);
return translateLocationInPixelsToPoints(point.x, point.y);
}

@Override
public Point translateToDisplayCoordinates(Point point, int zoom) {
return translateLocationInPointsToDisplayCoordinateSystem(point.x, point.y);
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.monitor : null;
return translateLocationInPointsToPixels(point.x, point.y, monitor);
}

@Override
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
return translateRectangleInPixelsFromDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
rect.height);
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.monitor : null;
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
}

@Override
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
return translateRectangleInPointsToDisplayCoordinateSystemByContainment(rect.x, rect.y, rect.width,
rect.height);
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.monitor : null;
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor);
}

@Override
public Point getCursorLocation() {
Point cursorLocationInPixels = display.getCursorLocationInPixels();
return translateLocationInPixelsFromDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y);
}

@Override
public void setCursorLocation(int x, int y) {
Point cursorLocationInPixels = translateLocationInPointsToDisplayCoordinateSystem(x, y);
display.setCursorLocationInPixels(cursorLocationInPixels.x, cursorLocationInPixels.y);
public void setCursorLocation (int x, int y) {
Point cursorLocationInPixels = translateLocationInPointsToPixels(x, y, null);
display.setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
}

private Point translateLocationInPointsToDisplayCoordinateSystem(int x, int y) {
Monitor monitor = getContainingMonitor(x, y);
private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) {
monitor = getValidMonitorIfApplicable(x, y, monitor);
return getPixelsFromPoint(monitor, x, y);
}

private Point translateLocationInPixelsFromDisplayCoordinateSystem(int x, int y) {
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
private Point translateLocationInPixelsToPoints(int x, int y) {
Monitor monitor = getContainingMonitorForPixels(x, y);
return getPointFromPixels(monitor, x, y);
}

private Rectangle translateRectangleInPointsToDisplayCoordinateSystemByContainment(int x, int y, int width,
int height) {
Monitor monitorByLocation = getContainingMonitor(x, y);
Monitor monitorByContainment = getContainingMonitor(x, y, width, height);
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitorByLocation,
monitorByContainment);
}

private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height,
Monitor monitor) {
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitor, monitor);
}

private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height,
Monitor monitorOfLocation, Monitor monitorOfArea) {
Point topLeft = getPixelsFromPoint(monitorOfLocation, x, y);
int zoom = getApplicableMonitorZoom(monitorOfArea);
private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, int height, Monitor monitor) {
monitor = getValidMonitorIfApplicable(x, y, width, height, monitor);
Point topLeft = getPixelsFromPoint(monitor, x, y);
int zoom = getApplicableMonitorZoom(monitor);
int widthInPixels = DPIUtil.scaleUp(width, zoom);
int heightInPixels = DPIUtil.scaleUp(height, zoom);
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
}

private Rectangle translateRectangleInPixelsFromDisplayCoordinateSystemByContainment(int x, int y,
int widthInPixels, int heightInPixels) {
Monitor monitorByLocation = getContainingMonitor(x, y);
Monitor monitorByContainment = getContainingMonitorInPixelsCoordinate(x, y, widthInPixels, heightInPixels);
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels,
monitorByLocation, monitorByContainment);
private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
if (monitor == null)
monitor = getContainingMonitorForPixels(x, y, widthInPixels, heightInPixels);
int zoom = getApplicableMonitorZoom(monitor);
Point topLeft = getPointFromPixels(monitor, x, y);
int width = DPIUtil.scaleDown(widthInPixels, zoom);
int height = DPIUtil.scaleDown(heightInPixels, zoom);
MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor);
return rect;
}

private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels,
int heightInPixels, Monitor monitor) {
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitor,
monitor);
private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) {
if(monitor != null) {
if (monitor.getClientArea().intersects(x, y, width, height)) {
return monitor;
} else {
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
if (containingMonitor != null) {
return containingMonitor;
} else {
return monitor;
}
}
} else {
Monitor containingMonitor = getContainingMonitorForPoints(x, y, width, height);
if (containingMonitor != null) {
return containingMonitor;
} else {
return monitorSupplier.get()[0];
}
}
}

private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels,
int heightInPixels, Monitor monitorOfLocation, Monitor monitorOfArea) {
Point topLeft = getPointFromPixels(monitorOfLocation, x, y);
int zoom = getApplicableMonitorZoom(monitorOfArea);
int width = DPIUtil.scaleDown(widthInPixels, zoom);
int height = DPIUtil.scaleDown(heightInPixels, zoom);
return new Rectangle(topLeft.x, topLeft.y, width, height);
private Monitor getValidMonitorIfApplicable(int x, int y, Monitor monitor) {
if(monitor != null) {
if (monitor.getClientArea().contains(x, y)) {
return monitor;
} else {
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
if (containingMonitor != null) {
return containingMonitor;
} else {
return monitor;
}
}
} else {
Monitor containingMonitor = getContainingMonitorForPoints(x, y);
if (containingMonitor != null) {
return containingMonitor;
} else {
return monitorSupplier.get()[0];
}
}
}

private Monitor getContainingMonitor(int x, int y) {
private Monitor getContainingMonitorForPoints(int x, int y) {
Monitor[] monitors = monitorSupplier.get();
for (Monitor currentMonitor : monitors) {
Rectangle clientArea = currentMonitor.getClientArea();
if (clientArea.contains(x, y)) {
return currentMonitor;
}
}
return monitors[0];
return null;
}

private Monitor getContainingMonitor(int x, int y, int width, int height) {
private Monitor getContainingMonitorForPoints(int x, int y, int width, int height) {
Rectangle rectangle = new Rectangle(x, y, width, height);
Monitor[] monitors = monitorSupplier.get();
Monitor selectedMonitor = monitors[0];
int highestArea = 0;
Monitor selectedMonitor = null;
int highestIntersectionRatio = 0;
for (Monitor currentMonitor : monitors) {
Rectangle clientArea = currentMonitor.getClientArea();
Rectangle intersection = clientArea.intersection(rectangle);
int area = intersection.width * intersection.height;
if (area > highestArea) {
Rectangle clientArea = getMonitorClientAreaInPixels(currentMonitor);
Rectangle boundsInPixel = DPIUtil.scaleUp(rectangle, currentMonitor.zoom);
Rectangle intersection = clientArea.intersection(boundsInPixel);
int intersectionArea = intersection.width * intersection.height;
int boundsArea = boundsInPixel.width * boundsInPixel.height;
int intersectionRatio = (intersectionArea * 100) / boundsArea;
if (intersectionRatio > highestIntersectionRatio) {
selectedMonitor = currentMonitor;
highestArea = area;
highestIntersectionRatio = intersectionRatio;
}
}
return selectedMonitor;
}

private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels) {
Monitor[] monitors = monitorSupplier.get();
for (Monitor current : monitors) {
Rectangle clientArea = getMonitorClientAreaInPixels(current);
Expand All @@ -219,7 +242,7 @@ private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPix
return monitors[0];
}

private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels, int widthInPixels,
private Monitor getContainingMonitorForPixels(int xInPixels, int yInPixels, int widthInPixels,
int heightInPixels) {
Rectangle rectangle = new Rectangle(xInPixels, yInPixels, widthInPixels, heightInPixels);
Monitor[] monitors = monitorSupplier.get();
Expand Down Expand Up @@ -255,11 +278,32 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
int zoom = getApplicableMonitorZoom(monitor);
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
return new Point(mappedX, mappedY);
Point pt = new MonitorAwarePoint(mappedX, mappedY, monitor);
return pt;
}

private int getApplicableMonitorZoom(Monitor monitor) {
return DPIUtil.getZoomForAutoscaleProperty(monitor.zoom);
}

private class MonitorAwareRectangle extends Rectangle {

public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) {
super(x, y, width, height);
this.monitor = monitor;
}

Monitor monitor;
}

private class MonitorAwarePoint extends Point {

MonitorAwarePoint(int x, int y, Monitor monitor) {
super(x, y);
this.monitor = monitor;
}

Monitor monitor;
}

}

0 comments on commit 144227b

Please sign in to comment.