Skip to content

Commit 185b8f3

Browse files
amartya4256fedejeanne
authored andcommitted
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 124c946 commit 185b8f3

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed

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+
int defaultWhint, defaultHhint, currentWhint, currentHhint;
403+
float defaultWidth = -1, defaultHeight = -1, 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: 33 additions & 47 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,27 +330,23 @@ 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;
338337
for (int k = 0; k < hSpan; k++) {
339-
widths [last=j-k] = Math.max (equalWidth, widths [j-k]);
338+
widths [j-k] = Math.max (equalWidth, widths [j-k]);
340339
}
341-
if (last > -1) widths [last] += remainder;
342340
} else {
343341
if (spanExpandCount == 0) {
344342
widths [j] += w;
345343
} else {
346-
int delta = w / spanExpandCount;
347-
int remainder = w % spanExpandCount, last = -1;
344+
float delta = w / spanExpandCount;
348345
for (int k = 0; k < hSpan; k++) {
349346
if (expandColumn [j-k]) {
350-
widths [last=j-k] += delta;
347+
widths [j-k] += delta;
351348
}
352349
}
353-
if (last > -1) widths [last] += remainder;
354350
}
355351
}
356352
}
@@ -361,14 +357,12 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
361357
if (spanExpandCount == 0) {
362358
minWidths [j] += w;
363359
} else {
364-
int delta = w / spanExpandCount;
365-
int remainder = w % spanExpandCount, last = -1;
360+
float delta = w / spanExpandCount;
366361
for (int k = 0; k < hSpan; k++) {
367362
if (expandColumn [j-k]) {
368-
minWidths [last=j-k] += delta;
363+
minWidths [j-k] += delta;
369364
}
370365
}
371-
if (last > -1) minWidths [last] += remainder;
372366
}
373367
}
374368
}
@@ -377,8 +371,8 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
377371
}
378372
}
379373
if (makeColumnsEqualWidth) {
380-
int minColumnWidth = 0;
381-
int columnWidth = 0;
374+
float minColumnWidth = 0;
375+
float columnWidth = 0;
382376
for (int i=0; i<columnCount; i++) {
383377
minColumnWidth = Math.max (minColumnWidth, minWidths [i]);
384378
columnWidth = Math.max (columnWidth, widths [i]);
@@ -424,20 +418,18 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
424418
spanWidth += widths [j-k];
425419
if (expandColumn [j-k]) spanExpandCount++;
426420
}
427-
int w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
421+
float w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
428422
w += data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
429423
if (w > 0) {
430424
if (spanExpandCount == 0) {
431425
widths [j] += w;
432426
} else {
433-
int delta2 = w / spanExpandCount;
434-
int remainder2 = w % spanExpandCount, last2 = -1;
427+
float delta2 = w / spanExpandCount;
435428
for (int k = 0; k < hSpan; k++) {
436429
if (expandColumn [j-k]) {
437-
widths [last2=j-k] += delta2;
430+
widths [j-k] += delta2;
438431
}
439432
}
440-
if (last2 > -1) widths [last2] += remainder2;
441433
}
442434
}
443435
}
@@ -499,16 +491,16 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
499491
/* Row heights */
500492
int availableHeight = height - verticalSpacing * (rowCount - 1) - (marginTop + marginHeight * 2 + marginBottom);
501493
expandCount = 0;
502-
int [] heights = new int [rowCount];
503-
int [] minHeights = new int [rowCount];
494+
float [] heights = new float [rowCount];
495+
float [] minHeights = new float [rowCount];
504496
boolean [] expandRow = new boolean [rowCount];
505497
for (int i=0; i<rowCount; i++) {
506498
for (int j=0; j<columnCount; j++) {
507499
GridData data = getData (grid, i, j, rowCount, columnCount, true);
508500
if (data != null) {
509501
int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
510502
if (vSpan == 1) {
511-
int h = data.cacheHeight + data.verticalIndent;
503+
float h = data.cacheHeight + data.verticalIndent;
512504
heights [i] = Math.max (heights [i], h);
513505
if (data.grabExcessVerticalSpace) {
514506
if (!expandRow [i]) expandCount++;
@@ -537,19 +529,17 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
537529
expandCount++;
538530
expandRow [i] = true;
539531
}
540-
int h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
532+
float h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
541533
if (h > 0) {
542534
if (spanExpandCount == 0) {
543535
heights [i] += h;
544536
} else {
545-
int delta = h / spanExpandCount;
546-
int remainder = h % spanExpandCount, last = -1;
537+
float delta = h / spanExpandCount;
547538
for (int k = 0; k < vSpan; k++) {
548539
if (expandRow [i-k]) {
549-
heights [last=i-k] += delta;
540+
heights [i-k] += delta;
550541
}
551542
}
552-
if (last > -1) heights [last] += remainder;
553543
}
554544
}
555545
if (!data.grabExcessVerticalSpace || data.minimumHeight != 0) {
@@ -559,14 +549,12 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
559549
if (spanExpandCount == 0) {
560550
minHeights [i] += h;
561551
} else {
562-
int delta = h / spanExpandCount;
563-
int remainder = h % spanExpandCount, last = -1;
552+
float delta = h / spanExpandCount;
564553
for (int k = 0; k < vSpan; k++) {
565554
if (expandRow [i-k]) {
566-
minHeights [last=i-k] += delta;
555+
minHeights [i-k] += delta;
567556
}
568557
}
569-
if (last > -1) minHeights [last] += remainder;
570558
}
571559
}
572560
}
@@ -609,20 +597,18 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
609597
spanHeight += heights [i-k];
610598
if (expandRow [i-k]) spanExpandCount++;
611599
}
612-
int h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
600+
float h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
613601
h += data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
614602
if (h > 0) {
615603
if (spanExpandCount == 0) {
616604
heights [i] += h;
617605
} else {
618-
int delta2 = h / spanExpandCount;
619-
int remainder2 = h % spanExpandCount, last2 = -1;
606+
float delta2 = h / spanExpandCount;
620607
for (int k = 0; k < vSpan; k++) {
621608
if (expandRow [i-k]) {
622-
heights [last2=i-k] += delta2;
609+
heights [i-k] += delta2;
623610
}
624611
}
625-
if (last2 > -1) heights [last2] += remainder2;
626612
}
627613
}
628614
}
@@ -651,7 +637,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
651637
if (data != null) {
652638
int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
653639
int vSpan = Math.max (1, data.verticalSpan);
654-
int cellWidth = 0, cellHeight = 0;
640+
float cellWidth = 0, cellHeight = 0;
655641
for (int k=0; k<hSpan; k++) {
656642
cellWidth += widths [j+k];
657643
}
@@ -660,7 +646,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
660646
}
661647
cellWidth += horizontalSpacing * (hSpan - 1);
662648
int childX = gridX + data.horizontalIndent;
663-
int childWidth = Math.min (data.cacheWidth, cellWidth);
649+
float childWidth = Math.min (data.cacheWidth, cellWidth);
664650
switch (data.horizontalAlignment) {
665651
case SWT.CENTER:
666652
case GridData.CENTER:
@@ -677,7 +663,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
677663
}
678664
cellHeight += verticalSpacing * (vSpan - 1);
679665
int childY = gridY + data.verticalIndent;
680-
int childHeight = Math.min (data.cacheHeight, cellHeight);
666+
float childHeight = Math.min (data.cacheHeight, cellHeight);
681667
switch (data.verticalAlignment) {
682668
case SWT.CENTER:
683669
case GridData.CENTER:
@@ -694,7 +680,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
694680
}
695681
Control child = grid [i][j];
696682
if (child != null) {
697-
child.setBounds (childX, childY, childWidth, childHeight);
683+
child.setBounds (new Rectangle.OfFloat(childX, childY, childWidth, childHeight));
698684
}
699685
}
700686
gridX += widths [j] + horizontalSpacing;
@@ -708,8 +694,8 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
708694
flush [i].cacheWidth = flush [i].cacheHeight = -1;
709695
}
710696

711-
int totalDefaultWidth = 0;
712-
int totalDefaultHeight = 0;
697+
float totalDefaultWidth = 0;
698+
float totalDefaultHeight = 0;
713699
for (int i=0; i<columnCount; i++) {
714700
totalDefaultWidth += widths [i];
715701
}
@@ -718,7 +704,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he
718704
}
719705
totalDefaultWidth += horizontalSpacing * (columnCount - 1) + marginLeft + marginWidth * 2 + marginRight;
720706
totalDefaultHeight += verticalSpacing * (rowCount - 1) + marginTop + marginHeight * 2 + marginBottom;
721-
return new Point (totalDefaultWidth, totalDefaultHeight);
707+
return new Point.OfFloat (totalDefaultWidth, totalDefaultHeight);
722708
}
723709

724710
String getName () {

0 commit comments

Comments
 (0)