Skip to content

Commit 8422f68

Browse files
amartya4256fedejeanne
authored andcommitted
Reintroduce scaling methods for Geometries
This commit reintroduces scaling methods for plain Rectangle & Point to provide backwards compatibility to the consumers which explicitly create a Rectangle & Point and want to call scaleBounds or scaleUp/scaleDown since they have different behaviour and Rectangle.OfFloat unifies this.
1 parent 1a1f0c2 commit 8422f68

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
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/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ public static Point pixelToPoint(Drawable drawable, Point point, int zoom) {
108108
}
109109

110110
public static Rectangle pixelToPoint(Rectangle rect, int zoom) {
111+
if (zoom == 100 || rect == null) return rect;
112+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pixelToPoint(rectOfFloat, zoom);
113+
Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0);
114+
Point scaledTopLeft = pixelToPoint(new Point (rect.x, rect.y), zoom);
115+
Point scaledBottomRight = pixelToPoint(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
116+
117+
scaledRect.x = scaledTopLeft.x;
118+
scaledRect.y = scaledTopLeft.y;
119+
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
120+
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
121+
return scaledRect;
122+
}
123+
124+
private static Rectangle pixelToPoint(Rectangle.OfFloat rect, int zoom) {
111125
return scaleBounds(rect, 100, zoom);
112126
}
113127

@@ -120,6 +134,21 @@ public static Rectangle pixelToPoint(Drawable drawable, Rectangle rect, int zoom
120134
* Returns a new rectangle as per the scaleFactor.
121135
*/
122136
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
137+
if (rect == null || targetZoom == currentZoom) return rect;
138+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleBounds(rectOfFloat, targetZoom, currentZoom);
139+
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
140+
Rectangle returnRect = new Rectangle.OfFloat (0,0,0,0);
141+
returnRect.x = Math.round (rect.x * scaleFactor);
142+
returnRect.y = Math.round (rect.y * scaleFactor);
143+
returnRect.width = Math.round (rect.width * scaleFactor);
144+
returnRect.height = Math.round (rect.height * scaleFactor);
145+
return returnRect;
146+
}
147+
148+
/**
149+
* Returns a new rectangle as per the scaleFactor.
150+
*/
151+
private static Rectangle scaleBounds (Rectangle.OfFloat rect, int targetZoom, int currentZoom) {
123152
if (rect == null || targetZoom == currentZoom) return rect;
124153
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect);
125154
float scaleFactor = DPIUtil.getScalingFactor(targetZoom, currentZoom);
@@ -185,6 +214,20 @@ public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
185214
}
186215

187216
public static Rectangle pointToPixel(Rectangle rect, int zoom) {
217+
if (zoom == 100 || rect == null) return rect;
218+
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pointToPixel(rectOfFloat, zoom);
219+
Rectangle scaledRect = new Rectangle.OfFloat(0,0,0,0);
220+
Point scaledTopLeft = pointToPixel (new Point(rect.x, rect.y), zoom);
221+
Point scaledBottomRight = pointToPixel (new Point(rect.x + rect.width, rect.y + rect.height), zoom);
222+
223+
scaledRect.x = scaledTopLeft.x;
224+
scaledRect.y = scaledTopLeft.y;
225+
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
226+
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
227+
return scaledRect;
228+
}
229+
230+
private static Rectangle pointToPixel(Rectangle.OfFloat rect, int zoom) {
188231
return scaleBounds(rect, zoom, 100);
189232
}
190233

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/Win32DPIUtilTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void scaleUpPoint() {
202202
@Test
203203
public void scaleUpRectangle() {
204204
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
205-
Rectangle valueAt150 = new Rectangle(75, 113, 8, 11);
205+
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
206206
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);
207207

208208
Rectangle scaledValue = Win32DPIUtils.pointToPixel(valueAt100, 200);
@@ -225,7 +225,7 @@ public void scaleDownscaleUpRectangleInvertible() {
225225
for (int zoom1 : zooms) {
226226
for (int zoom2 : zooms) {
227227
for (int i = 1; i <= 10000; i++) {
228-
Rectangle rect = new Rectangle(0, 0, i, i);
228+
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
229229
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom1);
230230
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2);
231231
scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);

0 commit comments

Comments
 (0)