Skip to content

Commit f51bb3b

Browse files
ShahzaibIbrahimakoch-yatta
authored andcommitted
When computing the size of a widget always return values rounded up
When converting pixels to point there can be depending on the zoom level be a fractional result. Currently in all cases these result is converted into an integer using `Math.round()` that will make values `+/-0.5` resulting in small values to be round towards a smaller value. While it is maybe valid for a _location_, when using points to express a _dimension_ this is not okay as it will result in the reported (integer) value to be to small leading to errors when the SWT API is then used after performing additional computations maybe. See - #2381 - #2166
1 parent ff6bf58 commit f51bb3b

File tree

34 files changed

+156
-103
lines changed

34 files changed

+156
-103
lines changed

bundles/org.eclipse.swt/Eclipse SWT AWT/win32/org/eclipse/swt/awt/SWT_AWT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void componentResized (ComponentEvent e) {
293293
display.syncExec (() -> {
294294
if (shell.isDisposed()) return;
295295
Dimension dim = parent.getSize ();
296-
shell.setSize(Win32DPIUtils.pixelToPoint(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
296+
shell.setSize(Win32DPIUtils.pixelToPointAsSize(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
297297
});
298298
}
299299
};

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/IE.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ void handleDOMEvent (OleEvent e) {
18801880
int screenY = pVarResult.getInt();
18811881
pVarResult.dispose();
18821882

1883-
Point position = Win32DPIUtils.pixelToPoint(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
1883+
Point position = Win32DPIUtils.pixelToPointAsLocation(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
18841884
position = browser.getDisplay().map(null, browser, position);
18851885
newEvent.x = position.x; newEvent.y = position.y;
18861886

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/DropTarget.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
410410
if (this.control == null) {
411411
// If there is no control for context, the behavior remains as before
412412
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.nativeZoom);
413-
return Win32DPIUtils.pixelToPoint(new Point(xInPixels, yInPixels), zoom);
413+
return Win32DPIUtils.pixelToPointAsLocation(new Point(xInPixels, yInPixels), zoom);
414414
}
415415
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.control.nativeZoom);
416416
// There is no API to convert absolute values in pixels to display relative
@@ -419,7 +419,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
419419
POINT pt = new POINT ();
420420
pt.x = xInPixels; pt.y = yInPixels;
421421
OS.ScreenToClient (this.control.handle, pt);
422-
Point p = Win32DPIUtils.pixelToPoint(new Point (pt.x, pt.y), zoom);
422+
Point p = Win32DPIUtils.pixelToPointAsLocation(new Point (pt.x, pt.y), zoom);
423423
return this.control.toDisplay(p);
424424
}
425425

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TableDropTargetEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void dragOver(DropTargetEvent event) {
151151
int effect = checkEffect(event.feedback);
152152
long handle = table.handle;
153153
Point coordinates = new Point(event.x, event.y);
154-
coordinates = Win32DPIUtils.pointToPixel(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
154+
coordinates = Win32DPIUtils.pointToPixelAsLocation(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
155155
LVHITTESTINFO pinfo = new LVHITTESTINFO();
156156
pinfo.x = coordinates.x;
157157
pinfo.y = coordinates.y;

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TreeDropTargetEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void dragOver(DropTargetEvent event) {
165165
int effect = checkEffect(event.feedback);
166166
long handle = tree.handle;
167167
Point coordinates = new Point(event.x, event.y);
168-
coordinates = Win32DPIUtils.pointToPixel(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
168+
coordinates = Win32DPIUtils.pointToPixelAsLocation(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
169169
TVHITTESTINFO lpht = new TVHITTESTINFO ();
170170
lpht.x = coordinates.x;
171171
lpht.y = coordinates.y;

bundles/org.eclipse.swt/Eclipse SWT OLE Win32/win32/org/eclipse/swt/ole/win32/OleClientSite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ private int OnInPlaceDeactivate() {
987987
return COM.S_OK;
988988
}
989989
private int OnPosRectChange(long lprcPosRect) {
990-
Point size = Win32DPIUtils.pointToPixel(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
990+
Point size = Win32DPIUtils.pointToPixelAsSize(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
991991
setExtent(size.x, size.y);
992992
return COM.S_OK;
993993
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,20 @@ public static sealed class OfFloat extends Point permits Point.WithMonitor {
139139
private static final long serialVersionUID = -1862062276431597053L;
140140

141141
public float residualX, residualY;
142+
private final RoundingMode roundingMode;
142143

143144
public OfFloat(int x, int y) {
144145
super(x, y);
146+
this.roundingMode = null;
145147
}
146148

147149
public OfFloat(float x, float y) {
148-
super(Math.round(x), Math.round(y));
150+
this(x, y, RoundingMode.ROUND);
151+
}
152+
153+
public OfFloat(float x, float y, RoundingMode roundingMode) {
154+
super(roundingMode.round(x), roundingMode.round(y));
155+
this.roundingMode = roundingMode;
149156
this.residualX = x - this.x;
150157
this.residualY = y - this.y;
151158
}
@@ -159,12 +166,12 @@ public float getY() {
159166
}
160167

161168
public void setX(float x) {
162-
this.x = Math.round(x);
169+
this.x = roundingMode.round(x);
163170
this.residualX = x - this.x;
164171
}
165172

166173
public void setY(float y) {
167-
this.y = Math.round(y);
174+
this.y = roundingMode.round(y);
168175
this.residualY = y - this.y;
169176
}
170177

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.eclipse.swt.graphics;
2+
/**
3+
* @noreference This class is not intended to be referenced by clients
4+
*/
5+
public enum RoundingMode {
6+
ROUND, UP;
7+
8+
public int round(float x) {
9+
if (this == ROUND) {
10+
return Math.round(x);
11+
}
12+
if (this == UP) {
13+
return (int) Math.ceil(x);
14+
}
15+
return (int) x;
16+
}
17+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public Point getDPI () {
526526
int dpiX = OS.GetDeviceCaps (hDC, OS.LOGPIXELSX);
527527
int dpiY = OS.GetDeviceCaps (hDC, OS.LOGPIXELSY);
528528
internal_dispose_GC (hDC, null);
529-
return Win32DPIUtils.pixelToPoint(new Point (dpiX, dpiY), DPIUtil.getZoomForAutoscaleProperty(getDeviceZoom()));
529+
return Win32DPIUtils.pixelToPointAsLocation(new Point (dpiX, dpiY), DPIUtil.getZoomForAutoscaleProperty(getDeviceZoom()));
530530
}
531531

532532
/**

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ private class DrawImageOperation extends ImageOperation {
10561056

10571057
@Override
10581058
void apply() {
1059-
drawImageInPixels(getImage(), Win32DPIUtils.pointToPixel(drawable, this.location, getZoom()));
1059+
drawImageInPixels(getImage(), Win32DPIUtils.pointToPixelAsLocation(drawable, this.location, getZoom()));
10601060
}
10611061

10621062
private void drawImageInPixels(Image image, Point location) {
@@ -1868,8 +1868,8 @@ private class DrawLineOperation extends Operation {
18681868
@Override
18691869
void apply() {
18701870
int deviceZoom = getZoom();
1871-
Point startInPixels = Win32DPIUtils.pointToPixel (drawable, start, deviceZoom);
1872-
Point endInPixels = Win32DPIUtils.pointToPixel (drawable, end, deviceZoom);
1871+
Point startInPixels = Win32DPIUtils.pointToPixelAsLocation (drawable, start, deviceZoom);
1872+
Point endInPixels = Win32DPIUtils.pointToPixelAsLocation (drawable, end, deviceZoom);
18731873
drawLineInPixels(startInPixels.x, startInPixels.y, endInPixels.x, endInPixels.y);
18741874
}
18751875
}
@@ -2039,7 +2039,7 @@ private class DrawPointOperation extends Operation {
20392039

20402040
@Override
20412041
void apply() {
2042-
Point scaleUpLocation = Win32DPIUtils.pointToPixel(location, getZoom());
2042+
Point scaleUpLocation = Win32DPIUtils.pointToPixelAsLocation(location, getZoom());
20432043
drawPointInPixels(scaleUpLocation.x, scaleUpLocation.y);
20442044
}
20452045
}
@@ -2457,7 +2457,7 @@ private class DrawStringOperation extends Operation {
24572457

24582458
@Override
24592459
void apply() {
2460-
Point scaledLocation = Win32DPIUtils.pointToPixel(drawable, location, getZoom());
2460+
Point scaledLocation = Win32DPIUtils.pointToPixelAsLocation(drawable, location, getZoom());
24612461
drawStringInPixels(string, scaledLocation.x, scaledLocation.y, isTransparent);
24622462
}
24632463
}
@@ -2643,7 +2643,7 @@ private class DrawTextOperation extends Operation {
26432643

26442644
@Override
26452645
void apply() {
2646-
Point scaledLocation = Win32DPIUtils.pointToPixel(drawable, location, getZoom());
2646+
Point scaledLocation = Win32DPIUtils.pointToPixelAsLocation(drawable, location, getZoom());
26472647
drawTextInPixels(string, scaledLocation.x, scaledLocation.y, flags);
26482648
}
26492649
}
@@ -5736,7 +5736,7 @@ void apply() {
57365736
*/
57375737
public Point stringExtent (String string) {
57385738
if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
5739-
return Win32DPIUtils.pixelToPoint(drawable, stringExtentInPixels(string), getZoom());
5739+
return Win32DPIUtils.pixelToPointAsSize(drawable, stringExtentInPixels(string), getZoom());
57405740
}
57415741

57425742
Point stringExtentInPixels (String string) {
@@ -5781,7 +5781,7 @@ Point stringExtentInPixels (String string) {
57815781
* </ul>
57825782
*/
57835783
public Point textExtent (String string) {
5784-
return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom());
5784+
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom());
57855785
}
57865786

57875787
/**
@@ -5816,7 +5816,7 @@ public Point textExtent (String string) {
58165816
* </ul>
58175817
*/
58185818
public Point textExtent (String string, int flags) {
5819-
return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, flags), getZoom());
5819+
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, flags), getZoom());
58205820
}
58215821

58225822
Point textExtentInPixels(String string, int flags) {

0 commit comments

Comments
 (0)