Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

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 sealed class Point implements Serializable permits MonitorAwarePoint {
public final class Point implements Serializable {

/**
* the x coordinate of the point
Expand Down Expand Up @@ -78,17 +78,9 @@ public Point (int x, int y) {
*/
@Override
public boolean equals (Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object.getClass() != this.getClass()) {
return false;
}
Point other = (Point) object;
return (other.x == this.x) && (other.y == this.y);
if (object == this) return true;
if (!(object instanceof Point p)) return false;
return (p.x == this.x) && (p.y == this.y);
}

/**
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 sealed class Rectangle implements Serializable permits MonitorAwareRectangle {
public final class Rectangle implements Serializable {

/**
* the x coordinate of the rectangle
Expand Down Expand Up @@ -156,17 +156,9 @@ public boolean contains (Point pt) {
*/
@Override
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object == this) {
return true;
}
if (object.getClass() != this.getClass()) {
return false;
}
Rectangle other = (Rectangle) object;
return (other.x == this.x) && (other.y == this.y) && (other.width == this.width) && (other.height == this.height);
if (object == this) return true;
if (!(object instanceof Rectangle r)) return false;
return (r.x == this.x) && (r.y == this.y) && (r.width == this.width) && (r.height == this.height);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
import org.eclipse.swt.internal.*;

class MultiZoomCoordinateSystemMapper implements CoordinateSystemMapper {
static class WithMonitor<T> {
final T geometry;
final Monitor monitor;
WithMonitor(T geometry, Monitor monitor) {
this.geometry = geometry;
this.monitor = monitor;
}
}

private final Display display;

Expand All @@ -45,42 +53,40 @@ public Rectangle map(Control from, Control to, Rectangle rectangle) {

@Override
public Point map(Control from, Control to, int x, int y) {
Point mappedPointInPoints;
if (from == null) {
Point mappedPointInpixels = display.mapInPixels(from, to,
getPixelsFromPoint(to.getShell().getMonitor(), x, y));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
return DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
} else if (to == null) {
Point mappedPointInpixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x,
WithMonitor<Point> mappedPointInPointsWithMonitor = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x,
mappedPointInpixels.y);
return mappedPointInPointsWithMonitor.geometry;
} else {
Point mappedPointInpixels = display.mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
return DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
}
return mappedPointInPoints;
}

@Override
public Rectangle map(Control from, Control to, int x, int y, int width, int height) {
Rectangle mappedRectangleInPoints;
if (from == null) {
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
translateRectangleInPointsToPixels(x, y, width, height,
to.getShell().getMonitor()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
return 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 = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x,
WithMonitor<Rectangle> mappedRectangleInPoints = translateRectangleInPixelsToPoints(mappedRectangleInPixels.x,
mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height,
from.getShell().getMonitor());
return mappedRectangleInPoints.geometry;
} else {
Rectangle mappedRectangleInPixels = display.mapInPixels(from, to,
DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
return DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
}
return mappedRectangleInPoints;
}

@Override
Expand All @@ -98,19 +104,20 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) {

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's where you loose the information about the monitor that the MonitorAwareRectangle had, which breaks the whole thing. Same for translateToDisplayCoordinates(Point, int)

WithMonitor<Rectangle> value = translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
return value.geometry;
}

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

Expand All @@ -133,7 +140,8 @@ private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) {

private Point translateLocationInPixelsToPoints(int x, int y) {
Monitor monitor = getContainingMonitorForPixels(x, y);
return getPointFromPixels(monitor, x, y);
WithMonitor<Point> value = getPointFromPixels(monitor, x, y);
return value.geometry;
}

private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, int height, Monitor monitor) {
Expand All @@ -145,15 +153,14 @@ private Rectangle translateRectangleInPointsToPixels(int x, int y, int width, in
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
}

private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
private WithMonitor<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);
WithMonitor<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;
return new WithMonitor<>(new Rectangle(topLeft.geometry.x, topLeft.geometry.y, width, height), monitor);
}

private Monitor getValidMonitorIfApplicable(int x, int y, int width, int height, Monitor monitor) {
Expand Down Expand Up @@ -257,11 +264,11 @@ private Point getPixelsFromPoint(Monitor monitor, int x, int y) {
return new Point(mappedX, mappedY);
}

private Point getPointFromPixels(Monitor monitor, int x, int y) {
private WithMonitor<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 MonitorAwarePoint(mappedX, mappedY, monitor);
return new WithMonitor<>(new Point(mappedX, mappedY), monitor);
}

private int getApplicableMonitorZoom(Monitor monitor) {
Expand Down