From 1d3166b932422b1c110f5fb34265e74994421f89 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Mon, 9 Nov 2026 23:09:58 +0100 Subject: [PATCH] Do not paint the selected tab fill in a CTabFolder without selected tab When all editors are closed, the empty editor area on Windows shows a band in the selected tab color below its empty tab row and around the content area, as if an invisible tab were selected. With themes whose selected tab fill differs from the folder background (e.g. white tab cards on a gray folder), this makes an empty editor area look like a broken, half-drawn editor. The cause is the custom tab content background (swt-draw-custom-tab-content-background, enabled for the editor area by the default Windows theme): CTabRendering extends the selected tab fill over the whole area below the tab row, regardless of whether there is a selected tab at all. A folder without selected tab, in particular an empty one, now keeps its folder background below the tab row. The tab row itself is drawn as before, and folders with a selected tab are unchanged. The selected tab fill is the background of the selected tab's content, so it is only drawn when the folder has a selection. Assisted-by: Claude Opus 5.5 --- .../renderers/swt/CTabRendering.java | 9 +- .../org/eclipse/e4/ui/tests/UIAllTests.java | 2 + .../renderers/swt/CTabRenderingTest.java | 121 ++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRenderingTest.java diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java index d38097ded1d..de40a6f0265 100644 --- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java +++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRendering.java @@ -695,7 +695,10 @@ public void setDrawCustomTabContentBackground(boolean drawCustomTabContentBackgr } /** - * Draws tab content background, deriving the colors from the tab colors. + * Draws tab content background, deriving the colors from the tab colors. The + * content area below the tabs only gets the selected tab fill if there is a + * selected tab whose content it belongs to; a folder without selected tab + * (e.g., an empty editor area) keeps the folder background there. */ private void drawCustomBackground(GC gc, Rectangle bounds, int state) { boolean selected = (state & SWT.SELECTED) != 0; @@ -703,7 +706,9 @@ private void drawCustomBackground(GC gc, Rectangle bounds, int state) { Rectangle partHeaderBounds = computeTrim(PART_HEADER, state, bounds.x, bounds.y, bounds.width, bounds.height); drawUnselectedTabBackground(gc, partHeaderBounds, state, vertical, parent.getBackground()); - drawSelectedTabBackground(gc, partHeaderBounds, state, vertical, parent.getBackground()); + if (parent.getSelectionIndex() >= 0) { + drawSelectedTabBackground(gc, partHeaderBounds, state, vertical, parent.getBackground()); + } } private void drawUnselectedTabBackground(GC gc, Rectangle partHeaderBounds, int state, boolean vertical, diff --git a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java index 9a9ef8c92ac..19c30cf7339 100644 --- a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java +++ b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java @@ -52,6 +52,7 @@ import org.eclipse.e4.ui.tests.workbench.SWTPartRendererTest; import org.eclipse.e4.ui.tests.workbench.SashRendererTest; import org.eclipse.e4.ui.tests.workbench.TopoSortTests; +import org.eclipse.e4.ui.workbench.renderers.swt.CTabRenderingTest; import org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRendererTest; import org.eclipse.e4.ui.workbench.renderers.swt.StackRendererTest; import org.eclipse.e4.ui.workbench.renderers.swt.TabStateHandlerTest; @@ -94,6 +95,7 @@ PartFocusTest.class, ModelElementTest.class, StackRendererTest.class, + CTabRenderingTest.class, TabStateHandlerTest.class, ThemeDefinitionChangedHandlerTest.class, ToolBarManagerRendererTest.class, diff --git a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRenderingTest.java b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRenderingTest.java new file mode 100644 index 00000000000..eb99891d76c --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/CTabRenderingTest.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2026 Vector Informatik GmbH and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.e4.ui.workbench.renderers.swt; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CTabFolder; +import org.eclipse.swt.custom.CTabFolderRenderer; +import org.eclipse.swt.custom.CTabItem; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CTabRenderingTest { + + private static final int WIDTH = 300; + private static final int HEIGHT = 200; + + private Shell shell; + private CTabFolder folder; + private TestableCTabRendering renderer; + private Color folderBackground; + private Color selectedTabFill; + + @BeforeEach + public void setUp() { + Display display = Display.getDefault(); + folderBackground = display.getSystemColor(SWT.COLOR_BLUE); + selectedTabFill = display.getSystemColor(SWT.COLOR_RED); + + shell = new Shell(display); + folder = new CTabFolder(shell, SWT.NONE); + folder.setBounds(0, 0, WIDTH, HEIGHT); + folder.setBackground(folderBackground); + renderer = new TestableCTabRendering(folder); + folder.setRenderer(renderer); + renderer.setSelectedTabFill(selectedTabFill); + renderer.setDrawCustomTabContentBackground(true); + } + + @AfterEach + public void tearDown() { + shell.dispose(); + } + + @Test + public void testFolderWithoutTabsKeepsFolderBackgroundInContentArea() { + assertEquals(folderBackground.getRGB(), paintAndGetContentAreaColor()); + } + + @Test + public void testFolderWithTabsButNoSelectedTabKeepsFolderBackgroundInContentArea() { + new CTabItem(folder, SWT.NONE).setText("Tab"); + assertEquals(-1, folder.getSelectionIndex()); + + assertEquals(folderBackground.getRGB(), paintAndGetContentAreaColor()); + } + + @Test + public void testFolderWithSelectedTabPaintsSelectedTabFillInContentArea() { + new CTabItem(folder, SWT.NONE).setText("Tab"); + folder.setSelection(0); + + assertEquals(selectedTabFill.getRGB(), paintAndGetContentAreaColor()); + } + + /** + * Paints the folder body and header the way {@link CTabFolder} does on a + * paint event and returns the color in the middle of the content area below + * the tab row. + */ + private RGB paintAndGetContentAreaColor() { + Image image = new Image(folder.getDisplay(), WIDTH, HEIGHT); + try { + GC gc = new GC(image); + try { + gc.setBackground(folder.getBackground()); + Rectangle bodyBounds = new Rectangle(0, 0, WIDTH, HEIGHT); + renderer.draw(CTabFolderRenderer.PART_BODY, SWT.BACKGROUND | SWT.FOREGROUND, bodyBounds, gc); + renderer.draw(CTabFolderRenderer.PART_HEADER, SWT.BACKGROUND | SWT.FOREGROUND, bodyBounds, gc); + } finally { + gc.dispose(); + } + ImageData imageData = image.getImageData(); + return imageData.palette.getRGB(imageData.getPixel(WIDTH / 2, HEIGHT * 3 / 4)); + } finally { + image.dispose(); + } + } + + private static class TestableCTabRendering extends CTabRendering { + + TestableCTabRendering(CTabFolder parent) { + super(parent); + } + + // Re-declared so that the test, loaded by a different bundle class loader + // than CTabRendering, may call the protected draw method + @Override + protected void draw(int part, int state, Rectangle bounds, GC gc) { + super.draw(part, state, bounds, gc); + } + } +}