Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.e4.ui.css.swt.dom;

import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper;
import org.eclipse.e4.ui.css.swt.helpers.CSSSWTImageHelper;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void reset() {
CTabItem item = getItem();
CSSSWTImageHelper.restoreDefaultImage(item);
item.setFont(null); // in such case the parent's font will be taken
CSSSWTColorHelper.resetForeground(item);
super.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
Expand All @@ -53,6 +54,8 @@ public class CSSSWTColorHelper {

private static final String APPLIED_FOREGROUND = "org.eclipse.e4.ui.css.swt.appliedForeground"; //$NON-NLS-1$

private static final String APPLIED_SELECTION_FOREGROUND = "org.eclipse.e4.ui.css.swt.appliedSelectionForeground"; //$NON-NLS-1$

/** Recorded when a control that had its own color is reset. */
private static final Object NOT_OWNED = new Object();

Expand Down Expand Up @@ -405,4 +408,46 @@ public static void setSelectionBackground(CTabFolder folder, Color newColor) {
folder.setSelectionBackground(newColor);
}
}

/**
* Sets the per-item foreground color on a {@link CTabItem} and records it so
* {@link #resetForeground(CTabItem)} only resets CSS-applied colors.
*/
public static void setForeground(CTabItem item, Color newColor) {
item.setForeground(newColor);
item.setData(APPLIED_FOREGROUND, newColor != null ? newColor.getRGB() : null);
}

/**
* Sets the per-item selection foreground color on a {@link CTabItem} and
* records it so {@link #resetForeground(CTabItem)} only resets CSS-applied
* colors.
*/
public static void setSelectionForeground(CTabItem item, Color newColor) {
item.setSelectionForeground(newColor);
item.setData(APPLIED_SELECTION_FOREGROUND, newColor != null ? newColor.getRGB() : null);
}

/**
* Resets per-item foreground and selection foreground colors that were applied
* by CSS, preserving colors set programmatically.
*/
public static void resetForeground(CTabItem item) {
Object appliedForeground = item.getData(APPLIED_FOREGROUND);
if (appliedForeground instanceof RGB rgb) {
Color current = item.getForeground();
if (current != null && !current.isDisposed() && rgb.equals(current.getRGB())) {
item.setForeground(null);
}
item.setData(APPLIED_FOREGROUND, null);
}
Object appliedSelectionForeground = item.getData(APPLIED_SELECTION_FOREGROUND);
if (appliedSelectionForeground instanceof RGB rgb) {
Color current = item.getSelectionForeground();
if (current != null && !current.isDisposed() && rgb.equals(current.getRGB())) {
item.setSelectionForeground(null);
}
item.setData(APPLIED_SELECTION_FOREGROUND, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.e4.ui.css.swt.CSSSWTConstants;
import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper;
import org.eclipse.e4.ui.css.swt.helpers.SWTElementHelpers;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
Expand Down Expand Up @@ -66,12 +65,11 @@ public void applyCSSPropertyColor(Object element, CSSValue value,
return;
}

if (widget instanceof CTabItem) {
CTabFolder folder = ((CTabItem) widget).getParent();
if (widget instanceof CTabItem item) {
if ("selected".equals(pseudo)) {
CSSSWTColorHelper.setSelectionForeground(folder, newColor);
CSSSWTColorHelper.setSelectionForeground(item, newColor);
} else {
CSSSWTColorHelper.setForeground(folder, newColor);
CSSSWTColorHelper.setForeground(item, newColor);
}
} else if (widget instanceof ToolItem) {
// ToolItem prevents itself from repaints if the same color is set
Expand Down Expand Up @@ -114,11 +112,11 @@ public String retrieveCSSPropertyColor(Object element, String pseudo,
CSSEngine engine) throws Exception {
Widget widget = (Widget) element;
Color color = null;
if (widget instanceof CTabItem) {
if (widget instanceof CTabItem item) {
if ("selected".equals(pseudo)) {
color = ((CTabItem) widget).getParent().getSelectionForeground();
color = item.getSelectionForeground();
} else {
color = ((CTabItem) widget).getParent().getForeground();
color = item.getForeground();
}
} else if (widget instanceof Control) {
color = ((Control) widget).getForeground();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ protected void setTabPosition() {
private void setColorsAndFonts() {
folder.setSelectionForeground(theme.getColorRegistry().get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR));
folder.setForeground(theme.getColorRegistry().get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR));
// CSS colors the items themselves, which would hide the previewed folder colors
for (CTabItem item : folder.getItems()) {
item.setSelectionForeground(null);
item.setForeground(null);
}

Color[] colors = new Color[2];
colors[0] = theme.getColorRegistry().get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.swt.dom.WidgetElement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
Expand All @@ -43,6 +45,7 @@ public class CTabItemSelectionTest {

@AfterEach
public void tearDown() {
WidgetElement.setEngine(css.getDisplay(), null);
if (shell != null && !shell.isDisposed()) {
shell.dispose();
shell = null;
Expand Down Expand Up @@ -82,21 +85,25 @@ private CTabFolder createFolderWithTwoItems(String styleSheet) {

@Test
void testSelectedTabReceivesStyledSelectionForeground() {
// :selected for CTabItem maps to CTabFolder#getSelectionForeground(),
// not the per-item CTabItem#getForeground(). The matched declaration
// is intentionally observable on the parent folder.
// :selected for CTabItem maps to per-item CTabItem#getSelectionForeground()
// without mutating the parent CTabFolder.
CTabFolder folder = createFolderWithTwoItems("CTabItem:selected { color: #FF0000 }");
spinEventLoop();

assertEquals(RED, folder.getSelectionForeground().getRGB());
for (CTabItem item : folder.getItems()) {
assertEquals(RED, item.getSelectionForeground().getRGB());
}
}

@Test
void testSelectionListenerReappliesStylesOnSelectionEvent() {
CTabFolder folder = createFolderWithTwoItems(
"CTabItem { color: #0000FF }\n" + "CTabItem:selected { color: #FF0000 }");
spinEventLoop();
assertEquals(RED, folder.getSelectionForeground().getRGB());
for (CTabItem item : folder.getItems()) {
assertEquals(RED, item.getSelectionForeground().getRGB());
assertEquals(new RGB(0, 0, 255), item.getForeground().getRGB());
}

// Switch the selection. CTabFolder#setSelection on its own does not
// fire the SWT selection listener, so we explicitly dispatch the
Expand All @@ -111,11 +118,10 @@ void testSelectionListenerReappliesStylesOnSelectionEvent() {
folder.notifyListeners(SWT.Selection, event);
spinEventLoop();

// The folder's selection-foreground stays red because the rule still
// matches whichever item is currently selected.
assertEquals(RED, folder.getSelectionForeground().getRGB());
// Non-selected items inherit the plain CTabItem rule.
assertEquals(new RGB(0, 0, 255), folder.getForeground().getRGB());
for (CTabItem item : folder.getItems()) {
assertEquals(RED, item.getSelectionForeground().getRGB());
assertEquals(new RGB(0, 0, 255), item.getForeground().getRGB());
}
}

@Test
Expand All @@ -125,7 +131,94 @@ void testNonSelectedRuleColorIsNotRed() {
spinEventLoop();

// The non-selected foreground (plain CTabItem rule) is blue, not red.
assertNotEquals(RED, folder.getForeground().getRGB());
assertEquals(new RGB(0, 0, 255), folder.getForeground().getRGB());
for (CTabItem item : folder.getItems()) {
assertNotEquals(RED, item.getForeground().getRGB());
assertEquals(new RGB(0, 0, 255), item.getForeground().getRGB());
}
}

@Test
void testSelectionListenerPreservesDistinctCssItemForegrounds() {
Display display = css.getDisplay();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());

CTabFolder folder = new CTabFolder(shell, SWT.NONE);
CTabItem item0 = new CTabItem(folder, SWT.NONE);
item0.setText("Busy");
WidgetElement.setCSSClass(item0, "busy");
CTabItem item1 = new CTabItem(folder, SWT.NONE);
item1.setText("Accent");
WidgetElement.setCSSClass(item1, "accent");
folder.setSelection(0);

RGB originalFolderFg = folder.getForeground().getRGB();
RGB originalFolderSelFg = folder.getSelectionForeground().getRGB();

CSSEngine engine = css.createEngine("""
CTabItem.busy { color: #888888; }
CTabItem.busy:selected { color: #aaaaaa; }
CTabItem.accent { color: #0000ff; }
CTabItem.accent:selected { color: #ff0000; }
""");
WidgetElement.setEngine(display, engine);
engine.applyStyles(shell, true);
shell.open();
spinEventLoop();

folder.setSelection(item1);
Event event = new Event();
event.widget = folder;
event.item = item1;
folder.notifyListeners(SWT.Selection, event);
spinEventLoop();

assertEquals(new RGB(0x88, 0x88, 0x88), item0.getForeground().getRGB());
assertEquals(new RGB(0xaa, 0xaa, 0xaa), item0.getSelectionForeground().getRGB());
assertEquals(new RGB(0, 0, 255), item1.getForeground().getRGB());
assertEquals(RED, item1.getSelectionForeground().getRGB());
assertEquals(originalFolderFg, folder.getForeground().getRGB());
assertEquals(originalFolderSelFg, folder.getSelectionForeground().getRGB());
}

@Test
void testSelectionListenerPreservesProgrammaticItemForegrounds() {
Display display = css.getDisplay();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());

CTabFolder folder = new CTabFolder(shell, SWT.NONE);
CTabItem item0 = new CTabItem(folder, SWT.NONE);
item0.setText("Item 0");
CTabItem item1 = new CTabItem(folder, SWT.NONE);
item1.setText("Item 1");
folder.setSelection(0);

Color progFg = new Color(10, 120, 10);
Color progSelFg = new Color(20, 180, 20);
try {
item0.setForeground(progFg);
item0.setSelectionForeground(progSelFg);

CSSEngine engine = css.createEngine("CTabFolder { color: #333333; }");
WidgetElement.setEngine(display, engine);
engine.applyStyles(shell, true);
shell.open();
spinEventLoop();

folder.setSelection(item1);
Event event = new Event();
event.widget = folder;
event.item = item1;
folder.notifyListeners(SWT.Selection, event);
spinEventLoop();

assertEquals(progFg.getRGB(), item0.getForeground().getRGB());
assertEquals(progSelFg.getRGB(), item0.getSelectionForeground().getRGB());
assertEquals(new RGB(0x33, 0x33, 0x33), item1.getForeground().getRGB());
} finally {
progFg.dispose();
progSelFg.dispose();
}
}
}
Loading
Loading