Skip to content

Commit 5c11f0b

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[win32] Fix scaling inside Region::intersect
This commit adresses possibly invalid result when using Region::intersects on a zoom not dividable by 100. Reason is that scaling up all attributes of the rectangle separately can lead to rounding errors. Scaling them together as a rectangle solves this limitation.
1 parent 56767ec commit 5c11f0b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/RegionWin32Tests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package org.eclipse.swt.graphics;
1515

1616
import static org.junit.Assert.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1719

1820
import org.eclipse.swt.internal.*;
1921
import org.eclipse.swt.internal.win32.*;
@@ -55,4 +57,31 @@ public void testRegionMustBeScaledOnHandleOfScaledZoomLevel() {
5557
assertEquals("scaled region's y position should be double of unscaled region", bounds.y * scalingFactor, scaledBounds.y);
5658
}
5759

60+
61+
@Test
62+
public void testRegionMustIntersectProperlyOn175Zoom() {
63+
Display display = Display.getDefault();
64+
Shell shell = new Shell(display);
65+
DPITestUtil.changeDPIZoom(shell, 150);
66+
67+
Region region = new Region(display);
68+
region.add(0, 0, 100, 51);
69+
boolean intersectWhenOverlapping = region.intersects(0, 50, 100, 50);
70+
assertTrue(intersectWhenOverlapping);
71+
72+
region = new Region(display);
73+
region.add(0, 0, 100, 50);
74+
boolean dontIntersectWhenNotOverlapping = region.intersects(0, 50, 100, 50);
75+
assertFalse(dontIntersectWhenNotOverlapping);
76+
77+
region = new Region(display);
78+
// 58 * 150 equals to 87
79+
region.add(0, 58, 100, 20);
80+
// (27 + 31) should equal to 87, but will be 88 if 27 and 31 will
81+
// be rounded independently
82+
boolean shouldNotIntersect = region.intersects(0, 27, 100, 31);
83+
assertFalse(shouldNotIntersect);
84+
85+
}
86+
5887
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public void intersect (Region region) {
389389
*/
390390
public boolean intersects (int x, int y, int width, int height) {
391391
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
392-
return intersectsInPixels(DPIUtil.scaleUp(x, initialZoom), DPIUtil.scaleUp(y, initialZoom), DPIUtil.scaleUp(width, initialZoom), DPIUtil.scaleUp(height, initialZoom));
392+
return intersects(new Rectangle(x, y, width, height));
393393
}
394394

395395
boolean intersectsInPixels (int x, int y, int width, int height) {

0 commit comments

Comments
 (0)