diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java index 6a1ea88ee53..033b07d714f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java @@ -253,6 +253,9 @@ public class CTabFolder extends Composite { Point oldSize; Font oldFont; + // client area as of the last Resize event + private Rectangle lastClientArea; + // internal constants static final int DEFAULT_WIDTH = 64; static final int DEFAULT_HEIGHT = 64; @@ -2162,10 +2165,16 @@ void onPaint(Event event) { void onResize(Event event) { if (inDispose) return; + // tab controls' resizes come here too, but folder listeners never see them + boolean folderResized = event.widget == this; + // a pending update that updateItems() runs must compare with the new size + if (folderResized && updateFlags != 0) lastClientArea = getClientArea(); if (ignoreResize) return; if (updateItems()) { redrawTabs(); } + // the new item layout can wrap the top right control differently + if (folderResized) lastClientArea = getClientArea(); Point size = getSize(); if (oldSize == null) { redraw(); @@ -4040,7 +4049,8 @@ void runUpdate() { if (updateFlags == 0) return; int flags = updateFlags; updateFlags = 0; - Rectangle rectBefore = getClientArea(); + // setters may already have changed the client area, so compare with what listeners last saw + Rectangle rectBefore = lastClientArea != null ? lastClientArea : getClientArea(); updateButtons(); updateTabHeight(false); updateItems(selectedIndex); diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CTabFolder.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CTabFolder.java index d51ea49edb2..c3ca0f8f972 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CTabFolder.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_CTabFolder.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -1686,6 +1687,205 @@ public void test_topRightControl_gradientWithBottomTabs_beforeLayout_doesNotFail assertNull(topRight.getBackgroundImage()); } +@Test +public void test_setTabHeight_notifiesClientAreaChange() { + createTabFolder(null, 1); + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 0)); + layOutShell(400, 300); + Point sizeBefore = ctabFolder.getSize(); + Rectangle clientAreaBefore = ctabFolder.getClientArea(); + List reportedClientAreas = recordResizes(); + + ctabFolder.setTabHeight(ctabFolder.getTabHeight() + 20); + processEvents(); + + assertEquals(sizeBefore, ctabFolder.getSize(), "the folder itself must not have been resized"); + Rectangle clientAreaAfter = ctabFolder.getClientArea(); + assertEquals(clientAreaBefore.y + 20, clientAreaAfter.y); + assertEquals(clientAreaBefore.height - 20, clientAreaAfter.height); + assertEquals(List.of(clientAreaAfter), reportedClientAreas, + "exactly one Resize must report the changed client area"); + assertEquals(clientAreaAfter, ctabFolder.getItem(0).getControl().getBounds(), + "the selected control must follow the client area"); +} + +@Test +public void test_setRenderer_notifiesClientAreaChange() { + createTabFolder(null, 1); + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 0)); + layOutShell(400, 300); + Point sizeBefore = ctabFolder.getSize(); + Rectangle clientAreaBefore = ctabFolder.getClientArea(); + List reportedClientAreas = recordResizes(); + + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 10)); + processEvents(); + + assertEquals(sizeBefore, ctabFolder.getSize(), "the folder itself must not have been resized"); + Rectangle clientAreaAfter = ctabFolder.getClientArea(); + assertEquals(new Rectangle(clientAreaBefore.x + 10, clientAreaBefore.y + 10, clientAreaBefore.width - 20, + clientAreaBefore.height - 20), clientAreaAfter); + assertEquals(List.of(clientAreaAfter), reportedClientAreas, + "exactly one Resize must report the changed client area"); + assertEquals(clientAreaAfter, ctabFolder.getItem(0).getControl().getBounds(), + "the selected control must follow the client area"); +} + +@Test +public void test_resize_notifiesOnce() { + createTabFolder(null, 1); + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 0)); + layOutShell(400, 300); + List reportedClientAreas = recordResizes(); + + ctabFolder.setSize(ctabFolder.getSize().x - 50, ctabFolder.getSize().y - 30); + processEvents(); + assertEquals(List.of(ctabFolder.getClientArea()), reportedClientAreas, + "resizing the folder must send exactly one Resize"); +} + +@Test +public void test_unchangedClientArea_sendsNoResize() { + createTabFolder(null, 1); + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 0)); + layOutShell(400, 300); + List reportedClientAreas = recordResizes(); + + ctabFolder.setTabHeight(ctabFolder.getTabHeight()); + ctabFolder.setRenderer(new TabHeightDependentRenderer(ctabFolder, 0)); + processEvents(); + assertEquals(List.of(), reportedClientAreas, "an unchanged client area must not send a Resize"); +} + +@Test +public void test_resizeWithPendingUpdateAndToolTip_notifiesOnce() { + createTabFolder(null, 1); + layOutShell(400, 300); + List reportedClientAreas = recordResizes(); + + // as after hovering a tab; updates reset it while the cursor is elsewhere + ctabFolder.setToolTipText("tooltip"); + ctabFolder.setTabPosition(SWT.BOTTOM); + ctabFolder.setSize(ctabFolder.getSize().x - 50, ctabFolder.getSize().y - 30); + processEvents(); + + assertEquals(List.of(ctabFolder.getClientArea()), reportedClientAreas, + "resizing the folder while an update is pending must send exactly one Resize"); + assertEquals(ctabFolder.getClientArea(), ctabFolder.getItem(0).getControl().getBounds(), + "the selected control must follow the client area"); +} + +@Test +public void test_setTabPosition_notifiesClientAreaChange() { + createTabFolder(null, 1); + assertClientAreaChangeIsReported(() -> ctabFolder.setTabPosition(SWT.BOTTOM)); +} + +@Test +public void test_setBorderVisible_notifiesClientAreaChange() { + createTabFolder(null, 1); + assertClientAreaChangeIsReported(() -> ctabFolder.setBorderVisible(true)); +} + +@Test +public void test_setTabPosition_withResizedTopRight_notifiesClientAreaChange() { + createTabFolder(null, 1); + Composite topRight = new Composite(ctabFolder, SWT.NONE); + topRight.setLayout(new FixedSizeLayout(16, 8)); + ctabFolder.setTopRight(topRight, SWT.RIGHT); + assertClientAreaChangeIsReported(() -> { + ctabFolder.setTabPosition(SWT.BOTTOM); + topRight.setSize(20, 10); + }); +} + +private void assertClientAreaChangeIsReported(Runnable change) { + layOutShell(400, 300); + Point sizeBefore = ctabFolder.getSize(); + Rectangle clientAreaBefore = ctabFolder.getClientArea(); + List reportedClientAreas = recordResizes(); + + change.run(); + processEvents(); + + assertEquals(sizeBefore, ctabFolder.getSize(), "the folder itself must not have been resized"); + Rectangle clientAreaAfter = ctabFolder.getClientArea(); + assertNotEquals(clientAreaBefore, clientAreaAfter, "the change must affect the client area"); + assertEquals(List.of(clientAreaAfter), reportedClientAreas, + "exactly one Resize must report the changed client area"); + assertEquals(clientAreaAfter, ctabFolder.getItem(0).getControl().getBounds(), + "the selected control must follow the client area"); +} + +@Test +public void test_resizeWrappingTopRight_laterUpdateSendsNoResize() { + createTabFolder(null, 3); + ctabFolder.setRenderer(new FixedItemWidthRenderer(ctabFolder)); + Composite topRight = new Composite(ctabFolder, SWT.NONE); + topRight.setLayout(new FixedSizeLayout(60, 20)); + ctabFolder.setTopRight(topRight, SWT.RIGHT | SWT.WRAP); + layOutShell(800, 300); + ctabFolder.setSize(300, ctabFolder.getSize().y); + processEvents(); + assertTrue(topRight.getBounds().y >= ctabFolder.getTabHeight(), "the compressed tabs must wrap the top right control"); + + // the compressed tabs only grow into the new width while the folder handles the resize + ctabFolder.setSize(400, ctabFolder.getSize().y); + processEvents(); + assertTrue(topRight.getBounds().y >= ctabFolder.getTabHeight(), "the grown tabs must wrap the top right control again"); + List reportedClientAreas = recordResizes(); + ctabFolder.getItem(0).setText("changed"); + processEvents(); + + assertEquals(List.of(), reportedClientAreas, "an update after the resize must not report the client area again"); +} + +private void layOutShell(int width, int height) { + shell.setSize(width, height); + shell.layout(true, true); + processEvents(); +} + +private List recordResizes() { + List reportedClientAreas = new ArrayList<>(); + ctabFolder.addListener(SWT.Resize, e -> reportedClientAreas.add(ctabFolder.getClientArea())); + return reportedClientAreas; +} + +private static final class TabHeightDependentRenderer extends CTabFolderRenderer { + private final int margin; + + TabHeightDependentRenderer(CTabFolder parent, int margin) { + super(parent); + this.margin = margin; + } + + @Override + protected Rectangle computeTrim(int part, int state, int x, int y, int width, int height) { + if (part == PART_BODY) { + int tabHeight = parent.getTabHeight(); + return new Rectangle(x - margin, y - tabHeight - margin, width + 2 * margin, + height + tabHeight + 2 * margin); + } + return super.computeTrim(part, state, x, y, width, height); + } +} + +private static final class FixedItemWidthRenderer extends CTabFolderRenderer { + FixedItemWidthRenderer(CTabFolder parent) { + super(parent); + } + + @Override + protected Point computeSize(int part, int state, GC gc, int wHint, int hHint) { + Point size = super.computeSize(part, state, gc, wHint, hHint); + if (part >= 0) { + size.x = (state & MINIMUM_SIZE) != 0 ? 20 : 200; + } + return size; + } +} + /** Layout with a preferred size the test can change at will. */ private static final class FixedSizeLayout extends Layout { int width;