Skip to content

Commit d4caabf

Browse files
akoch-yattaHeikoKlare
authored andcommitted
Preserved width/height on scaleUp/Down a Rectangle
This commit extends the logic when a Rectangle is scaled up/down from points to pixels regarding width and height. Fixes #2003
1 parent a76fb54 commit d4caabf

File tree

2 files changed

+23
-2
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal
  • tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit

2 files changed

+23
-2
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ public static Rectangle scaleDown(Rectangle rect, int zoom) {
257257
scaledRect.y = scaledTopLeft.y;
258258
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
259259
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
260+
261+
int scaledDownWidth = DPIUtil.scaleDown(rect.width, zoom);
262+
int scaledDownHeight = DPIUtil.scaleDown(rect.height, zoom);
263+
264+
// It must be ensured, that a scaled down width or height
265+
// based on Rectangle x or y is not bigger than directly
266+
// scaling down the width or height. Therefore the min
267+
// value is used
268+
scaledRect.width = Math.min(scaledRect.width, scaledDownWidth);
269+
scaledRect.height = Math.min(scaledRect.height, scaledDownHeight);
270+
260271
return scaledRect;
261272
}
262273
/**
@@ -464,6 +475,16 @@ public static Rectangle scaleUp(Rectangle rect, int zoom) {
464475
scaledRect.y = scaledTopLeft.y;
465476
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
466477
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
478+
479+
int scaledUpWidth = DPIUtil.scaleUp(rect.width, zoom);
480+
int scaledUpHeight = DPIUtil.scaleUp(rect.height, zoom);
481+
482+
// It must be ensured, that a scaled up width or height
483+
// based on Rectangle x or y is not smaller that directly
484+
// scaling up the width or height. Therefore the max
485+
// value is used
486+
scaledRect.width = Math.max(scaledRect.width, scaledUpWidth);
487+
scaledRect.height = Math.max(scaledRect.height, scaledUpHeight);
467488
return scaledRect;
468489
}
469490

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void scaleDownPoint() {
160160
@Test
161161
public void scaleDownRectangle() {
162162
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
163-
Rectangle valueAt150 = new Rectangle(75, 113, 7, 10);
163+
Rectangle valueAt150 = new Rectangle(75, 113, 7, 11);
164164
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);
165165

166166
Rectangle scaledValue = DPIUtil.autoScaleDown(valueAt200);
@@ -295,7 +295,7 @@ public void scaleUpPoint() {
295295
@Test
296296
public void scaleUpRectangle() {
297297
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
298-
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
298+
Rectangle valueAt150 = new Rectangle(75, 113, 8, 11);
299299
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);
300300

301301
Rectangle scaledValue = DPIUtil.autoScaleUp(valueAt100);

0 commit comments

Comments
 (0)