Skip to content

Commit 7653450

Browse files
committed
Use Float in GridLayout for precise scaling
This commit contributes to enable GridLayout:layout to utilize float values in GridData to calculate precise size on scaling using Point.OfFloat APIs. contributes to #2166
1 parent 3c7a3f7 commit 7653450

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public void setY(float y) {
158158
this.y = Math.round(y);
159159
this.residualY = y - this.y;
160160
}
161+
162+
public static Point.OfFloat from(Point point) {
163+
if (point instanceof Point.OfFloat pointOfFloat) {
164+
return new Point.OfFloat(pointOfFloat.getX(), pointOfFloat.getY());
165+
}
166+
return new Point.OfFloat(point.x, point.y);
167+
}
161168
}
162169

163170
/**

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/GridData.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,9 @@ public final class GridData {
398398
*/
399399
public static final int FILL_BOTH = FILL_VERTICAL | FILL_HORIZONTAL;
400400

401-
int cacheWidth = -1, cacheHeight = -1;
402-
int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
403-
int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
401+
float cacheWidth = -1, cacheHeight = -1;
402+
float defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
403+
float currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
404404

405405
/**
406406
* Constructs a new instance of GridData using
@@ -493,19 +493,20 @@ void computeSize (Control control, int wHint, int hHint, boolean flushCache) {
493493
Point size = control.computeSize (wHint, hHint, flushCache);
494494
defaultWhint = wHint;
495495
defaultHhint = hHint;
496-
defaultWidth = size.x;
497-
defaultHeight = size.y;
496+
Point.OfFloat defaultSize = Point.OfFloat.from(size);
497+
defaultWidth = defaultSize.getX();
498+
defaultHeight = defaultSize.getY();
498499
}
499500
cacheWidth = defaultWidth;
500501
cacheHeight = defaultHeight;
501502
return;
502503
}
503504
if (currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) {
504-
Point size = control.computeSize (wHint, hHint, flushCache);
505+
Point.OfFloat size = Point.OfFloat.from(control.computeSize (wHint, hHint, flushCache));
505506
currentWhint = wHint;
506507
currentHhint = hHint;
507-
currentWidth = size.x;
508-
currentHeight = size.y;
508+
currentWidth = size.getX();
509+
currentHeight = size.getY();
509510
}
510511
cacheWidth = currentWidth;
511512
cacheHeight = currentHeight;

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/GridLayout.java

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,16 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
292292
/* Column widths */
293293
int availableWidth = width - horizontalSpacing * (columnCount - 1) - (marginLeft + marginWidth * 2 + marginRight);
294294
int expandCount = 0;
295-
int [] widths = new int [columnCount];
296-
int [] minWidths = new int [columnCount];
295+
float [] widths = new float [columnCount];
296+
float [] minWidths = new float [columnCount];
297297
boolean [] expandColumn = new boolean [columnCount];
298298
for (int j=0; j<columnCount; j++) {
299299
for (int i=0; i<rowCount; i++) {
300300
GridData data = getData (grid, i, j, rowCount, columnCount, true);
301301
if (data != null) {
302302
int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
303303
if (hSpan == 1) {
304-
int w = data.cacheWidth + data.horizontalIndent;
304+
float w = data.cacheWidth + data.horizontalIndent;
305305
widths [j] = Math.max (widths [j], w);
306306
if (data.grabExcessHorizontalSpace) {
307307
if (!expandColumn [j]) expandCount++;
@@ -330,11 +330,12 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
330330
expandCount++;
331331
expandColumn [j] = true;
332332
}
333-
int w = data.cacheWidth + data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
333+
float w = data.cacheWidth + data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
334334
if (w > 0) {
335335
if (makeColumnsEqualWidth) {
336-
int equalWidth = (w + spanWidth) / hSpan;
337-
int remainder = (w + spanWidth) % hSpan, last = -1;
336+
float equalWidth = (w + spanWidth) / hSpan;
337+
float remainder = (w + spanWidth) % hSpan;
338+
int last = -1;
338339
for (int k = 0; k < hSpan; k++) {
339340
widths [last=j-k] = Math.max (equalWidth, widths [j-k]);
340341
}
@@ -343,8 +344,9 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
343344
if (spanExpandCount == 0) {
344345
widths [j] += w;
345346
} else {
346-
int delta = w / spanExpandCount;
347-
int remainder = w % spanExpandCount, last = -1;
347+
float delta = w / spanExpandCount;
348+
float remainder = w % spanExpandCount;
349+
int last = -1;
348350
for (int k = 0; k < hSpan; k++) {
349351
if (expandColumn [j-k]) {
350352
widths [last=j-k] += delta;
@@ -361,8 +363,9 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
361363
if (spanExpandCount == 0) {
362364
minWidths [j] += w;
363365
} else {
364-
int delta = w / spanExpandCount;
365-
int remainder = w % spanExpandCount, last = -1;
366+
float delta = w / spanExpandCount;
367+
float remainder = w % spanExpandCount;
368+
int last = -1;
366369
for (int k = 0; k < hSpan; k++) {
367370
if (expandColumn [j-k]) {
368371
minWidths [last=j-k] += delta;
@@ -377,8 +380,8 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
377380
}
378381
}
379382
if (makeColumnsEqualWidth) {
380-
int minColumnWidth = 0;
381-
int columnWidth = 0;
383+
float minColumnWidth = 0;
384+
float columnWidth = 0;
382385
for (int i=0; i<columnCount; i++) {
383386
minColumnWidth = Math.max (minColumnWidth, minWidths [i]);
384387
columnWidth = Math.max (columnWidth, widths [i]);
@@ -424,14 +427,15 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
424427
spanWidth += widths [j-k];
425428
if (expandColumn [j-k]) spanExpandCount++;
426429
}
427-
int w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
430+
float w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
428431
w += data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
429432
if (w > 0) {
430433
if (spanExpandCount == 0) {
431434
widths [j] += w;
432435
} else {
433-
int delta2 = w / spanExpandCount;
434-
int remainder2 = w % spanExpandCount, last2 = -1;
436+
float delta2 = w / spanExpandCount;
437+
float remainder2 = w % spanExpandCount;
438+
int last2 = -1;
435439
for (int k = 0; k < hSpan; k++) {
436440
if (expandColumn [j-k]) {
437441
widths [last2=j-k] += delta2;
@@ -499,16 +503,16 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
499503
/* Row heights */
500504
int availableHeight = height - verticalSpacing * (rowCount - 1) - (marginTop + marginHeight * 2 + marginBottom);
501505
expandCount = 0;
502-
int [] heights = new int [rowCount];
503-
int [] minHeights = new int [rowCount];
506+
float [] heights = new float [rowCount];
507+
float [] minHeights = new float [rowCount];
504508
boolean [] expandRow = new boolean [rowCount];
505509
for (int i=0; i<rowCount; i++) {
506510
for (int j=0; j<columnCount; j++) {
507511
GridData data = getData (grid, i, j, rowCount, columnCount, true);
508512
if (data != null) {
509513
int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
510514
if (vSpan == 1) {
511-
int h = data.cacheHeight + data.verticalIndent;
515+
float h = data.cacheHeight + data.verticalIndent;
512516
heights [i] = Math.max (heights [i], h);
513517
if (data.grabExcessVerticalSpace) {
514518
if (!expandRow [i]) expandCount++;
@@ -537,13 +541,14 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
537541
expandCount++;
538542
expandRow [i] = true;
539543
}
540-
int h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
544+
float h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
541545
if (h > 0) {
542546
if (spanExpandCount == 0) {
543547
heights [i] += h;
544548
} else {
545-
int delta = h / spanExpandCount;
546-
int remainder = h % spanExpandCount, last = -1;
549+
float delta = h / spanExpandCount;
550+
float remainder = h % spanExpandCount;
551+
int last = -1;
547552
for (int k = 0; k < vSpan; k++) {
548553
if (expandRow [i-k]) {
549554
heights [last=i-k] += delta;
@@ -559,8 +564,9 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
559564
if (spanExpandCount == 0) {
560565
minHeights [i] += h;
561566
} else {
562-
int delta = h / spanExpandCount;
563-
int remainder = h % spanExpandCount, last = -1;
567+
float delta = h / spanExpandCount;
568+
float remainder = h % spanExpandCount;
569+
int last = -1;
564570
for (int k = 0; k < vSpan; k++) {
565571
if (expandRow [i-k]) {
566572
minHeights [last=i-k] += delta;
@@ -609,14 +615,15 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
609615
spanHeight += heights [i-k];
610616
if (expandRow [i-k]) spanExpandCount++;
611617
}
612-
int h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
618+
float h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
613619
h += data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
614620
if (h > 0) {
615621
if (spanExpandCount == 0) {
616622
heights [i] += h;
617623
} else {
618-
int delta2 = h / spanExpandCount;
619-
int remainder2 = h % spanExpandCount, last2 = -1;
624+
float delta2 = h / spanExpandCount;
625+
float remainder2 = h % spanExpandCount;
626+
int last2 = -1;
620627
for (int k = 0; k < vSpan; k++) {
621628
if (expandRow [i-k]) {
622629
heights [last2=i-k] += delta2;
@@ -660,7 +667,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
660667
}
661668
cellWidth += horizontalSpacing * (hSpan - 1);
662669
int childX = gridX + data.horizontalIndent;
663-
int childWidth = Math.min (data.cacheWidth, cellWidth);
670+
float childWidth = Math.min (data.cacheWidth, cellWidth);
664671
switch (data.horizontalAlignment) {
665672
case SWT.CENTER:
666673
case GridData.CENTER:
@@ -677,7 +684,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
677684
}
678685
cellHeight += verticalSpacing * (vSpan - 1);
679686
int childY = gridY + data.verticalIndent;
680-
int childHeight = Math.min (data.cacheHeight, cellHeight);
687+
float childHeight = Math.min (data.cacheHeight, cellHeight);
681688
switch (data.verticalAlignment) {
682689
case SWT.CENTER:
683690
case GridData.CENTER:
@@ -694,7 +701,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
694701
}
695702
Control child = grid [i][j];
696703
if (child != null) {
697-
child.setBounds (childX, childY, childWidth, childHeight);
704+
child.setBounds (new Rectangle.OfFloat(childX, childY, childWidth, childHeight));
698705
}
699706
}
700707
gridX += widths [j] + horizontalSpacing;

0 commit comments

Comments
 (0)