Skip to content

Commit 6e665a6

Browse files
committed
Fix size of tab items when large text padding is used
The recently introduced option to not show images in tab folders currently works as follows: A large padding is applied to the tab item as a separator with the text being centered within this padding. For tabs showing a close icon (which in particular is the currently selected on), this icon is added right at the right end of the tab item, such that the text is centered in between the left end of the tab item and the close button. This does not look perfect and while the padding is necessary as a separator for tabs only containing a text, having close icons and, in case of the selected tab, also a different background is sufficient as a separator. This change adapts the appearance of tab items having a close icon when having images disabled. It uses the same size for the tab as if no close icon was drawn and then reduces the area in which the text is centered to the remaining space left to the close icon. This looks cleaner and has the effect that tabs within a tab folder have a fixed with, such that when changing the selection all tabs keep their size and positions. Only the texts within the tab items change their positions depending on whether the close icon is shown or not.
1 parent 75da5aa commit 6e665a6

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,19 +2972,17 @@ boolean setItemSize(GC gc) {
29722972
for (int i = 0; i < items.length; i++) {
29732973
CTabItem tab = items[i];
29742974
int width = widths[i];
2975-
if (tab.height != tabHeight || tab.width != width) {
2976-
changed = true;
2977-
tab.shortenedText = null;
2978-
tab.shortenedTextWidth = 0;
2979-
tab.height = tabHeight;
2980-
tab.width = width;
2981-
tab.closeRect.width = tab.closeRect.height = 0;
2982-
if (showClose || tab.showClose) {
2983-
if (i == selectedIndex || showUnselectedClose) {
2984-
Point closeSize = renderer.computeSize(CTabFolderRenderer.PART_CLOSE_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT);
2985-
tab.closeRect.width = closeSize.x;
2986-
tab.closeRect.height = closeSize.y;
2987-
}
2975+
changed = true;
2976+
tab.shortenedText = null;
2977+
tab.shortenedTextWidth = 0;
2978+
tab.height = tabHeight;
2979+
tab.width = width;
2980+
tab.closeRect.width = tab.closeRect.height = 0;
2981+
if (showClose || tab.showClose) {
2982+
if (i == selectedIndex || showUnselectedClose) {
2983+
Point closeSize = renderer.computeSize(CTabFolderRenderer.PART_CLOSE_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT);
2984+
tab.closeRect.width = closeSize.x;
2985+
tab.closeRect.height = closeSize.y;
29882986
}
29892987
}
29902988
}

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolderRenderer.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,10 @@ protected Point computeSize (int part, int state, GC gc, int wHint, int hHint) {
358358
}
359359
}
360360

361-
width += getTextPadding(item, state) * 2;
362-
363-
if (shouldDrawCloseIcon(item)) {
364-
if (!applyLargeTextPadding(parent)) {
365-
if (width > 0) width += INTERNAL_SPACING;
366-
} else {
367-
if (width > 0) width -= INTERNAL_SPACING;
368-
}
361+
if (shouldApplyLargeTextPadding(parent)) {
362+
width += getLargeTextPadding(item) * 2;
363+
} else if (shouldDrawCloseIcon(item)) {
364+
if (width > 0) width += INTERNAL_SPACING;
369365
width += computeSize(PART_CLOSE_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT).x;
370366
}
371367
}
@@ -385,26 +381,19 @@ private boolean shouldDrawCloseIcon(CTabItem item) {
385381
}
386382

387383
/**
388-
* Returns padding for the text of a tab when image is not available or is hidden.
389-
*
390-
* @param item CTabItem
391-
* @param state current state
392-
*
384+
* Returns padding for the text of a tab item when showing images is disabled for the tab folder.
393385
*/
394-
private int getTextPadding(CTabItem item, int state) {
386+
private int getLargeTextPadding(CTabItem item) {
395387
CTabFolder parent = item.getParent();
396388
String text = item.getText();
397389

398390
if (text != null && parent.getMinimumCharacters() != 0) {
399-
if (applyLargeTextPadding(parent)) {
400-
return TABS_WITHOUT_ICONS_PADDING;
401-
}
391+
return TABS_WITHOUT_ICONS_PADDING;
402392
}
403-
404393
return 0;
405394
}
406395

407-
private boolean applyLargeTextPadding(CTabFolder tabFolder) {
396+
private boolean shouldApplyLargeTextPadding(CTabFolder tabFolder) {
408397
return !tabFolder.showSelectedImage && !tabFolder.showUnselectedImage;
409398
}
410399

@@ -1438,7 +1427,7 @@ void drawSelected(int itemIndex, GC gc, Rectangle bounds, int state ) {
14381427
}
14391428

14401429
// draw Text
1441-
xDraw += getTextPadding(item, state);
1430+
xDraw += getLeftTextMargin(item);
14421431
int textWidth = rightEdge - xDraw - (trim.width + trim.x);
14431432
if (!parent.single && item.closeRect.width > 0) textWidth -= item.closeRect.width + INTERNAL_SPACING;
14441433
if (textWidth > 0) {
@@ -1478,6 +1467,17 @@ void drawSelected(int itemIndex, GC gc, Rectangle bounds, int state ) {
14781467
}
14791468
}
14801469

1470+
private int getLeftTextMargin(CTabItem item) {
1471+
int margin = 0;
1472+
if (shouldApplyLargeTextPadding(parent)) {
1473+
margin += getLargeTextPadding(item);
1474+
if (shouldDrawCloseIcon(item)) {
1475+
margin -= item.closeRect.width / 2;
1476+
}
1477+
}
1478+
return margin;
1479+
}
1480+
14811481
void drawTabArea(GC gc, Rectangle bounds, int state) {
14821482
Point size = parent.getSize();
14831483
int[] shape = null;
@@ -1652,7 +1652,7 @@ void drawUnselected(int index, GC gc, Rectangle bounds, int state) {
16521652
}
16531653
}
16541654
// draw Text
1655-
xDraw += getTextPadding(item, state);
1655+
xDraw += getLeftTextMargin(item);
16561656
int textWidth = x + width - xDraw - (trim.width + trim.x);
16571657
if (shouldDrawCloseIcon(item)) {
16581658
textWidth -= item.closeRect.width + INTERNAL_SPACING;

0 commit comments

Comments
 (0)