Skip to content

Commit 136b375

Browse files
HeikoKlareakoch-yatta
authored andcommitted
[Win32] Adapt further places to round up when computing control size
Several places in the computations of preferred sizes of controls used commercial rounding instead of rounding up in pixel/point calculations. This change further adapts additional places to ensure that the calculated size will be large enough and and subsequent point-to-pixel calculation when setting the bounds according the computations does not lead to smaller results.
1 parent 9e2bdb5 commit 136b375

File tree

23 files changed

+61
-43
lines changed

23 files changed

+61
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public OfFloat(float x, float y, float width, float height) {
426426
this(x, y, width, height, RoundingMode.ROUND, RoundingMode.ROUND);
427427
}
428428

429-
private OfFloat(float x, float y, float width, float height, RoundingMode locationRounding, RoundingMode sizeRounding) {
429+
public OfFloat(float x, float y, float width, float height, RoundingMode locationRounding, RoundingMode sizeRounding) {
430430
this.locationRounding = locationRounding;
431431
this.sizeRounding = sizeRounding;
432432
setX(x);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static Point pixelToPointAsLocation(Point point, int zoom) {
131131
return pixelToPoint(point, zoom, RoundingMode.ROUND);
132132
}
133133

134-
public static Point pixelToPointAsConservativeSize(Point point, int zoom) {
134+
public static Point pixelToPointAsSufficientlyLargeSize(Point point, int zoom) {
135135
return pixelToPoint(point, zoom, RoundingMode.UP);
136136
}
137137

@@ -150,16 +150,23 @@ private static Point.OfFloat pixelToPoint(Point.OfFloat point, int zoom) {
150150
}
151151

152152
public static Rectangle pixelToPoint(Rectangle rect, int zoom) {
153+
return pixelToPoint(rect, zoom, RoundingMode.ROUND);
154+
}
155+
156+
public static Rectangle pixelToPointWithSufficientlyLargeSize(Rectangle rect, int zoom) {
157+
return pixelToPoint(rect, zoom, RoundingMode.UP);
158+
}
159+
160+
private static Rectangle pixelToPoint(Rectangle rect, int zoom, RoundingMode sizeRounding) {
153161
if (zoom == 100 || rect == null) return rect;
154162
Rectangle.OfFloat floatRect = Rectangle.OfFloat.from(rect);
155163
Point.OfFloat scaledTopLeft = pixelToPoint(floatRect.getTopLeft(), zoom);
156164
Point.OfFloat scaledBottomRight = pixelToPoint(floatRect.getBottomRight(), zoom);
157-
Rectangle.OfFloat scaledRect = floatRect.clone();
158-
scaledRect.setX(scaledTopLeft.getX());
159-
scaledRect.setY(scaledTopLeft.getY());
160-
scaledRect.setWidth(scaledBottomRight.getX() - scaledTopLeft.getX());
161-
scaledRect.setHeight(scaledBottomRight.getY() - scaledTopLeft.getY());
162-
return scaledRect;
165+
float scaledX = scaledTopLeft.getX();
166+
float scaleyY = scaledTopLeft.getY();
167+
float scaledWidth = scaledBottomRight.getX() - scaledTopLeft.getX();
168+
float scaledHeight = scaledBottomRight.getY() - scaledTopLeft.getY();
169+
return new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
163170
}
164171

165172
public static Rectangle pixelToPoint(Drawable drawable, Rectangle rect, int zoom) {
@@ -252,6 +259,10 @@ public static Point pointToPixelAsLocation(Drawable drawable, Point point, int z
252259
}
253260

254261
public static Point pointToPixelAsSize(Point point, int zoom) {
262+
return pointToPixel(point, zoom, RoundingMode.ROUND);
263+
}
264+
265+
public static Point pointToPixelAsSufficientlyLargeSize(Point point, int zoom) {
255266
return pointToPixel(point, zoom, RoundingMode.UP);
256267
}
257268

@@ -260,16 +271,23 @@ public static Point pointToPixelAsLocation(Point point, int zoom) {
260271
}
261272

262273
public static Rectangle pointToPixel(Rectangle rect, int zoom) {
274+
return pointToPixel(rect, zoom, RoundingMode.ROUND);
275+
}
276+
277+
public static Rectangle pointToPixelWithSufficientlyLargeSize(Rectangle rect, int zoom) {
278+
return pointToPixel(rect, zoom, RoundingMode.UP);
279+
}
280+
281+
private static Rectangle pointToPixel(Rectangle rect, int zoom, RoundingMode sizeRounding) {
263282
if (zoom == 100 || rect == null) return rect;
264283
Rectangle.OfFloat floatRect = Rectangle.OfFloat.from(rect);
265284
Point.OfFloat scaledTopLeft = pointToPixel(floatRect.getTopLeft(), zoom);
266285
Point.OfFloat scaledBottomRight = pointToPixel(floatRect.getBottomRight(), zoom);
267-
Rectangle.OfFloat scaledRect = floatRect.clone();
268-
scaledRect.setX(scaledTopLeft.getX());
269-
scaledRect.setY(scaledTopLeft.getY());
270-
scaledRect.setWidth(scaledBottomRight.getX() - scaledTopLeft.getX());
271-
scaledRect.setHeight(scaledBottomRight.getY() - scaledTopLeft.getY());
272-
return scaledRect;
286+
float scaledX = scaledTopLeft.getX();
287+
float scaleyY = scaledTopLeft.getY();
288+
float scaledWidth = scaledBottomRight.getX() - scaledTopLeft.getX();
289+
float scaledHeight = scaledBottomRight.getY() - scaledTopLeft.getY();
290+
return new Rectangle.OfFloat(scaledX, scaleyY, scaledWidth, scaledHeight, RoundingMode.ROUND, sizeRounding);
273291
}
274292

275293
public static Rectangle pointToPixel(Drawable drawable, Rectangle rect, int zoom) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ else if ((style & SWT.RIGHT) != 0) {
326326
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
327327
checkWidget ();
328328
int zoom = getZoom();
329-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
329+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
330330
int width = 0, height = 0, border = getBorderWidthInPixels ();
331331
if ((style & SWT.ARROW) != 0) {
332332
if ((style & (SWT.UP | SWT.DOWN)) != 0) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Combo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public void clearSelection () {
596596
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
597597
checkWidget ();
598598
int zoom = getZoom();
599-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
599+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
600600
int width = 0, height = 0;
601601
if (hintInPoints.x == SWT.DEFAULT) {
602602
long newFont, oldFont = 0;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Composite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Point computeSizeInPixels (Point hintInPoints, boolean changed) {
229229
* Since computeTrim can be overridden by subclasses, we cannot
230230
* call computeTrimInPixels directly.
231231
*/
232-
Rectangle trim = Win32DPIUtils.pointToPixel(computeTrim (0, 0, sizeInPoints.x, sizeInPoints.y), getZoom());
232+
Rectangle trim = Win32DPIUtils.pointToPixelWithSufficientlyLargeSize(computeTrim (0, 0, sizeInPoints.x, sizeInPoints.y), getZoom());
233233
return new Point (trim.width, trim.height);
234234
}
235235

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Control.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,14 @@ public Point computeSize (int wHint, int hHint, boolean changed){
621621
int zoom = getZoom();
622622
//We should never return a size that is to small, RoundingMode.UP ensures we at worst case report
623623
//a size that is a bit too large by half a point
624-
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(new Point.OfFloat(wHint, hHint, RoundingMode.UP), changed), zoom);
624+
return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(computeSizeInPixels(new Point(wHint, hHint), changed), zoom);
625625
}
626626

627627
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
628628
int width = DEFAULT_WIDTH;
629629
int height = DEFAULT_HEIGHT;
630630
int zoom = getZoom();
631-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
631+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
632632
if (hintInPoints.x != SWT.DEFAULT) width = hintInPixels.x;
633633
if (hintInPoints.y != SWT.DEFAULT) height = hintInPixels.y;
634634
int border = getBorderWidthInPixels ();

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected void checkSubclass () {
143143
@Override
144144
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
145145
int zoom = getZoom();
146-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
146+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
147147
int width = 0, height = 0;
148148
int border = getBorderWidthInPixels ();
149149
int newWidth = hintInPoints.x == SWT.DEFAULT ? 0x3FFF : hintInPixels.x + (border * 2);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoolItem.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,16 @@ protected void checkSubclass () {
186186
public Point computeSize (int wHint, int hHint) {
187187
checkWidget ();
188188
int zoom = getZoom();
189-
wHint = (wHint != SWT.DEFAULT ? DPIUtil.pointToPixel(wHint, zoom) : wHint);
190-
hHint = (hHint != SWT.DEFAULT ? DPIUtil.pointToPixel(hHint, zoom) : hHint);
191-
return Win32DPIUtils.pixelToPointAsConservativeSize(computeSizeInPixels(wHint, hHint), zoom);
189+
return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(computeSizeInPixels(new Point(wHint, hHint)), zoom);
192190
}
193-
Point computeSizeInPixels (int wHint, int hHint) {
191+
Point computeSizeInPixels (Point sizeHintInPoints) {
192+
int zoom = getZoom();
193+
Point sizeHintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(sizeHintInPoints, zoom);
194194
int index = parent.indexOf (this);
195195
if (index == -1) return new Point (0, 0);
196-
int width = wHint, height = hHint;
197-
if (wHint == SWT.DEFAULT) width = 32;
198-
if (hHint == SWT.DEFAULT) height = 32;
196+
int width = sizeHintInPixels.x, height = sizeHintInPixels.y;
197+
if (sizeHintInPoints.x == SWT.DEFAULT) width = 32;
198+
if (sizeHintInPoints.y == SWT.DEFAULT) height = 32;
199199
if ((parent.style & SWT.VERTICAL) != 0) {
200200
height += parent.getMargin (index);
201201
} else {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/DateTime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected void checkSubclass () {
222222
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
223223
checkWidget ();
224224
int zoom = getZoom();
225-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
225+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
226226
int width = 0, height = 0;
227227
if (hintInPoints.x == SWT.DEFAULT || hintInPoints.y == SWT.DEFAULT) {
228228
if ((style & SWT.CALENDAR) != 0) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ExpandBar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static int checkStyle (int style) {
129129
@Override
130130
Point computeSizeInPixels (Point hintInPoints, boolean changed) {
131131
int zoom = getZoom();
132-
Point hintInPixels = Win32DPIUtils.pointToPixelAsSize(hintInPoints, zoom);
132+
Point hintInPixels = Win32DPIUtils.pointToPixelAsSufficientlyLargeSize(hintInPoints, zoom);
133133
int height = 0, width = 0;
134134
if (hintInPoints.x == SWT.DEFAULT || hintInPoints.y == SWT.DEFAULT) {
135135
if (itemCount > 0) {

0 commit comments

Comments
 (0)