Skip to content

Commit f19d02a

Browse files
committed
Reintroduce scaling methods for Rectangle
This commit reintroduces scaling methods for plain Rectangle to provide backwards compatibility to the consumers which explicitly create a Rectangle and want to call scaleBounds or scaleUp/scaleDown since they have different behaviour and Rectangle.OfFloat unifies this.
1 parent 648a444 commit f19d02a

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/ControlWin32Tests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
100100

101101
button.setBounds(new Rectangle(0, 47, 200, 47));
102102
assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly",
103-
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
103+
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
104104

105105
button.setBounds(0, 47, 200, 47);
106106
assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly",
107-
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
107+
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
108108
}
109109

110110
record FontComparison(int originalFontHeight, int currentFontHeight) {

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ public static Rectangle autoScaleDown(Rectangle rect) {
255255
}
256256

257257
public static Rectangle scaleDown(Rectangle rect, int zoom) {
258+
if (zoom == 100 || rect == null) return rect;
259+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleDown(rectOfFloat, zoom);
260+
Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0);
261+
Point scaledTopLeft = scaleDown(new Point (rect.x, rect.y), zoom);
262+
Point scaledBottomRight = scaleDown(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
263+
264+
scaledRect.x = scaledTopLeft.x;
265+
scaledRect.y = scaledTopLeft.y;
266+
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
267+
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
268+
return scaledRect;
269+
}
270+
271+
public static Rectangle scaleDown(Rectangle.OfFloat rect, int zoom) {
258272
return scaleBounds(rect, 100, zoom);
259273
}
260274
/**
@@ -323,6 +337,21 @@ public static boolean isSmoothScalingEnabled() {
323337
* Returns a new rectangle as per the scaleFactor.
324338
*/
325339
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
340+
if (rect == null || targetZoom == currentZoom) return rect;
341+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleBounds(rectOfFloat, targetZoom, currentZoom);
342+
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
343+
Rectangle returnRect = new Rectangle.OfFloat (0,0,0,0);
344+
returnRect.x = Math.round (rect.x * scaleFactor);
345+
returnRect.y = Math.round (rect.y * scaleFactor);
346+
returnRect.width = Math.round (rect.width * scaleFactor);
347+
returnRect.height = Math.round (rect.height * scaleFactor);
348+
return returnRect;
349+
}
350+
351+
/**
352+
* Returns a new rectangle as per the scaleFactor.
353+
*/
354+
public static Rectangle scaleBounds (Rectangle.OfFloat rect, int targetZoom, int currentZoom) {
326355
if (rect == null || targetZoom == currentZoom) return rect;
327356
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect);
328357
float scaleFactor = getScalingFactor(targetZoom, currentZoom);
@@ -454,6 +483,20 @@ public static Rectangle autoScaleUp(Rectangle rect) {
454483
}
455484

456485
public static Rectangle scaleUp(Rectangle rect, int zoom) {
486+
if (zoom == 100 || rect == null) return rect;
487+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleUp(rectOfFloat, zoom);
488+
Rectangle scaledRect = new Rectangle.OfFloat(0,0,0,0);
489+
Point scaledTopLeft = scaleUp (new Point(rect.x, rect.y), zoom);
490+
Point scaledBottomRight = scaleUp (new Point(rect.x + rect.width, rect.y + rect.height), zoom);
491+
492+
scaledRect.x = scaledTopLeft.x;
493+
scaledRect.y = scaledTopLeft.y;
494+
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
495+
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
496+
return scaledRect;
497+
}
498+
499+
public static Rectangle scaleUp(Rectangle.OfFloat rect, int zoom) {
457500
return scaleBounds(rect, zoom, 100);
458501
}
459502

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/DPIUtilTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void scaleUpPoint() {
296296
@Test
297297
public void scaleUpRectangle() {
298298
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
299-
Rectangle valueAt150 = new Rectangle(75, 113, 8, 11);
299+
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
300300
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);
301301

302302
Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100);
@@ -321,12 +321,12 @@ public void scaleUpRectangle() {
321321
}
322322

323323
@Test
324-
public void scaleDownscaleUpRectangleInvertible() {
324+
public void scaleDownscaleUpRectangleOfFloatInvertible() {
325325
int[] zooms = new int[] {25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400};
326326
for (int zoom1 : zooms) {
327327
for (int zoom2 : zooms) {
328328
for (int i = 1; i <= 10000; i++) {
329-
Rectangle rect = new Rectangle(0, 0, i, i);
329+
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
330330
Rectangle scaleDown = DPIUtil.scaleDown(rect, zoom1);
331331
Rectangle scaleUp = DPIUtil.scaleUp(scaleDown, zoom2);
332332
scaleDown = DPIUtil.scaleDown(scaleUp, zoom2);

0 commit comments

Comments
 (0)