Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @noreference This class is not intended to be referenced by clients
*/
public enum RoundingMode {
ROUND, UP;
ROUND, UP, DOWN;

public int round(float x) {
if (this == ROUND) {
Expand All @@ -12,6 +12,9 @@ public int round(float x) {
if (this == UP) {
return (int) Math.ceil(x);
}
if (this == DOWN) {
return (int) Math.floor(x);
}
return (int) x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@ public static Point pixelToPointAsLocation(Drawable drawable, Point point, int z
}

public static Point pixelToPointAsSize(Point point, int zoom) {
return pixelToPoint(point, zoom, RoundingMode.UP);
return pixelToPoint(point, zoom, RoundingMode.DOWN);
}

public static Point pixelToPointAsLocation(Point point, int zoom) {
return pixelToPoint(point, zoom, RoundingMode.ROUND);
}

public static Point pixelToPointAsConservativeSize(Point point, int zoom) {
return pixelToPoint(point, zoom, RoundingMode.UP);
}

private static Point pixelToPoint(Point point, int zoom, RoundingMode mode) {
if (zoom == 100 || point == null) return point;
Point.OfFloat fPoint = Point.OfFloat.from(point);
Expand All @@ -145,7 +149,7 @@ public static Rectangle pixelToPoint(Rectangle rect, int zoom) {
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pixelToPoint(rectOfFloat, zoom);
Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0);
Point scaledTopLeft = pixelToPointAsLocation(new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = pixelToPointAsLocation(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
Point scaledBottomRight = pixelToPointAsSize(new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public Point computeSize (int wHint, int hHint, boolean changed){
hHint = (hHint != SWT.DEFAULT ? DPIUtil.pointToPixel(hHint, zoom) : hHint);
//We should never return a size that is to small, RoundingMode.UP ensures we at worst case report
//a size that is a bit too large by half a point
return Win32DPIUtils.pixelToPointAsSize(computeSizeInPixels(wHint, hHint, changed), zoom);
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(wHint, hHint, changed), zoom);
}

Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public Point computeSize (int wHint, int hHint) {
int zoom = getZoom();
wHint = (wHint != SWT.DEFAULT ? DPIUtil.pointToPixel(wHint, zoom) : wHint);
hHint = (hHint != SWT.DEFAULT ? DPIUtil.pointToPixel(hHint, zoom) : hHint);
return Win32DPIUtils.pixelToPointAsSize(computeSizeInPixels(wHint, hHint), zoom);
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(wHint, hHint), zoom);
}
Point computeSizeInPixels (int wHint, int hHint) {
int index = parent.indexOf (this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void scaleDownPoint() {
@Test
public void scaleDownRectangle() {
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
Rectangle valueAt150 = new Rectangle(75, 113, 7, 10);
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this aligns the test values with the opposite scaleUpRectangle() method, which also makes the behavior appear more consistent again.

Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);

Rectangle scaledValue = Win32DPIUtils.pixelToPoint(valueAt200, 200);
Expand Down
Loading