Skip to content

Commit f627f8a

Browse files
HeikoKlareakoch-yatta
authored andcommitted
[Win32] Partially revert rectangle size rounding
With a recent change, the rounding of rectangles width/height was adapted to be based on the precise (float) coordinates of the top left and bottom right corner of the rectangle instead of rounding top left location and width/height individually in the rectangle. The reason was that otherwise two rectangles placed next to each other and scaled from pixel to point or vice versa could overlap or have a gap between them when the resulting values were rounded. So the change was beneficial (i.e., improved precision) when scaling values from pixel to point or vice versa if they are all defined in the same coordinate system (with the same origin), as then equal coordinates are rounded equally. However, the coordinates used in SWT are usually in different coordinate systems with the origin often being the position of the parent control. So one consequence of the change was that a parent with a child, both of the same size, may not fit into each other anymore if the parent has an offset (with respect to its parent) that is taken into account when rounding the Rectangle values. For that reason, this change reverts the behavioral change of Rectangle and the point/pixel conversion logic to the previous state. Fixes #2746
1 parent 2df129f commit f627f8a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@ public void setY(float y) {
464464
}
465465

466466
public void setWidth(float width) {
467-
this.width = sizeRounding.round(width + getX()) - x;
467+
this.width = sizeRounding.round(width);
468468
this.residualWidth = width - this.width;
469469
}
470470

471471
public void setHeight(float height) {
472-
this.height = sizeRounding.round(height + getY()) - y;
472+
this.height = sizeRounding.round(height);
473473
this.residualHeight = height - this.height;
474474
}
475475

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,16 @@ public static Rectangle pixelToPointWithSufficientlyLargeSize(Rectangle rect, in
159159

160160
private static Rectangle pixelToPoint(Rectangle rect, int zoom, RoundingMode sizeRounding) {
161161
if (zoom == 100 || rect == null) return rect;
162+
if (rect instanceof Rectangle.OfFloat) {
163+
return scaleBounds(rect, 100, zoom);
164+
}
162165
Rectangle.OfFloat floatRect = Rectangle.OfFloat.from(rect);
163166
Point.OfFloat scaledTopLeft = pixelToPoint(floatRect.getTopLeft(), zoom);
164167
Point.OfFloat scaledBottomRight = pixelToPoint(floatRect.getBottomRight(), zoom);
165-
float scaledX = scaledTopLeft.getX();
166-
float scaleyY = scaledTopLeft.getY();
167-
float scaledWidth = scaledBottomRight.getX() - scaledTopLeft.getX();
168-
float scaledHeight = scaledBottomRight.getY() - scaledTopLeft.getY();
168+
float scaledX = scaledTopLeft.x;
169+
float scaleyY = scaledTopLeft.y;
170+
float scaledWidth = scaledBottomRight.x - scaledTopLeft.x;
171+
float scaledHeight = scaledBottomRight.y - scaledTopLeft.y;
169172
return new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
170173
}
171174

@@ -280,13 +283,16 @@ public static Rectangle pointToPixelWithSufficientlyLargeSize(Rectangle rect, in
280283

281284
private static Rectangle pointToPixel(Rectangle rect, int zoom, RoundingMode sizeRounding) {
282285
if (zoom == 100 || rect == null) return rect;
286+
if (rect instanceof Rectangle.OfFloat) {
287+
return scaleBounds(rect, zoom, 100);
288+
}
283289
Rectangle.OfFloat floatRect = Rectangle.OfFloat.from(rect);
284290
Point.OfFloat scaledTopLeft = pointToPixel(floatRect.getTopLeft(), zoom);
285291
Point.OfFloat scaledBottomRight = pointToPixel(floatRect.getBottomRight(), zoom);
286-
float scaledX = scaledTopLeft.getX();
287-
float scaleyY = scaledTopLeft.getY();
288-
float scaledWidth = scaledBottomRight.getX() - scaledTopLeft.getX();
289-
float scaledHeight = scaledBottomRight.getY() - scaledTopLeft.getY();
292+
float scaledX = scaledTopLeft.x;
293+
float scaleyY = scaledTopLeft.y;
294+
float scaledWidth = scaledBottomRight.x - scaledTopLeft.x;
295+
float scaledHeight = scaledBottomRight.y - scaledTopLeft.y;
290296
return new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
291297
}
292298

0 commit comments

Comments
 (0)