Skip to content

Commit

Permalink
Implements ToolItem styling support via CSS
Browse files Browse the repository at this point in the history
Allows to style ToolItems background color and foreground color.
SWT style-bits can also be used for selecting the tool items which
should be styled.

ToolItemTest contains tests for these elements, an TODO has been added
so enable the styling also via a styling call on the parent, this would
require additional changes in ToolBarElement to handle also its
children.

Pseudo-handing styling similar to the button, e.g. :checked has not yet
been implemented.

ToolItem {
   background-color: pink;
   color:blue;
}

ToolItem [style~='SWT.PUSH'], ToolItem[style~='SWT.CHECK'] {
   background-color: blue;
   color:red;
}
  • Loading branch information
vogella committed Jun 21, 2022
1 parent 46b1d61 commit c07ab53
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 5 deletions.
23 changes: 23 additions & 0 deletions bundles/org.eclipse.e4.ui.css.swt/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<widget
class="org.eclipse.swt.widgets.ToolBar">
</widget>
<widget
class="org.eclipse.swt.widgets.ToolItem">
</widget>

<!-- these should be obviated by 4.2M6 -->
</provider>
Expand Down Expand Up @@ -72,6 +75,18 @@
name="background-image">
</property-name>
</handler>

<handler
adapter="org.eclipse.e4.ui.css.swt.dom.ToolItemElement"
composite="false"
handler="org.eclipse.e4.ui.css.swt.properties.css2.CSSPropertyBackgroundSWTHandler">
<property-name
name="background">
</property-name>
<property-name
name="background-color">
</property-name>
</handler>
<handler
adapter="org.eclipse.e4.ui.css.swt.dom.CTabFolderElement"
composite="false"
Expand Down Expand Up @@ -109,6 +124,14 @@
<property-name
name="text-transform">
</property-name>
</handler>
<handler
adapter="org.eclipse.e4.ui.css.swt.dom.ToolItemElement"
composite="true"
handler="org.eclipse.e4.ui.css.swt.properties.css2.CSSPropertyTextSWTHandler">
<property-name
name="color">
</property-name>
</handler>
<handler
adapter="org.eclipse.e4.ui.css.swt.dom.CTabFolderElement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.swt.helpers.CSSSWTImageHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
Expand All @@ -27,14 +30,48 @@
*
*/
public class ToolItemElement extends ItemElement {

private boolean isSelected = false;

boolean dynamicEnabled = Boolean.getBoolean("org.eclipse.e4.ui.css.dynamic");

private SelectionListener selectionListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ToolItemElement.this.isSelected = getToolItem().getSelection();
doApplyStyles();
}
};

public ToolItemElement(ToolItem toolItem, CSSEngine engine) {
super(toolItem, engine);

if (!dynamicEnabled) {
return;
}

toolItem.addSelectionListener(selectionListener);
}

public ToolItem getToolItem() {
return (ToolItem) getNativeWidget();
}

@Override
public void dispose() {

super.dispose();

if (!dynamicEnabled) {
return;
}

ToolItem toolItem = getToolItem();
if (!toolItem.isDisposed()) {
toolItem.removeSelectionListener(selectionListener);
}
}

@Override
public Node getParentNode() {
ToolItem item = getToolItem();
Expand All @@ -60,13 +97,22 @@ public Node item(int index) {
@Override
public int getLength() {
ToolItem item = getToolItem();
return (item.getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR
&& item.getControl() != null ? 1 : 0;
return (item.getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR && item.getControl() != null ? 1 : 0;
}

@Override
public boolean isPseudoInstanceOf(String s) {
if ("checked".equalsIgnoreCase(s)) {
return this.isSelected;
}
return super.isPseudoInstanceOf(s);
}

@Override
public void reset() {
CSSSWTImageHelper.restoreDefaultImage(getToolItem());
getToolItem().setBackground(null);
getToolItem().setForeground(null);
super.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public static void setBackground(Control control, Color newColor) {
}
}


/** Helper function to avoid setting colors unnecessarily */
public static void setSelectionForeground(CTabFolder folder, Color newColor) {
if (!Objects.equals(folder.getSelectionForeground(), newColor)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
import org.w3c.dom.css.CSSValue;

Expand Down Expand Up @@ -58,7 +59,6 @@ public String retrieveCSSProperty(Object element, String property,
public void applyCSSPropertyBackgroundColor(Object element, CSSValue value,
String pseudo, CSSEngine engine) throws Exception {
Widget widget = (Widget) ((WidgetElement) element).getNativeWidget();

switch (value.getCssValueType()) {
case CSSValue.CSS_PRIMITIVE_VALUE:
Color newColor = (Color) engine.convert(value, Color.class, widget
Expand All @@ -71,6 +71,9 @@ public void applyCSSPropertyBackgroundColor(Object element, CSSValue value,
} else {
CSSSWTColorHelper.setBackground(folder, newColor);
}
} else if (widget instanceof ToolItem) {
// ToolItem prevents itself from repaints if the same color is set
((ToolItem) widget).setBackground(newColor);
} else if (widget instanceof Control) {
GradientBackgroundListener.remove((Control) widget);
CSSSWTColorHelper.setBackground((Control) widget, newColor);
Expand Down Expand Up @@ -137,12 +140,17 @@ public String retrieveCSSPropertyBackgroundColor(Object element,
} else {
color = ((CTabItem) widget).getParent().getBackground();
}
} else if (widget instanceof Control) {

}
else if (widget instanceof ToolItem) {
color = ((ToolItem) widget).getBackground();
}

else if (widget instanceof Control) {
color = ((Control) widget).getBackground();
}
return engine.convert(color, Color.class, null);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
import org.w3c.dom.css.CSSValue;

Expand Down Expand Up @@ -71,6 +72,9 @@ public void applyCSSPropertyColor(Object element, CSSValue value,
} else {
CSSSWTColorHelper.setForeground(folder, newColor);
}
} else if (widget instanceof ToolItem) {
// ToolItem prevents itself from repaints if the same color is set
((ToolItem) widget).setForeground(newColor);
} else if (widget instanceof Control) {
CSSSWTColorHelper.setForeground((Control) widget, newColor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.eclipse.e4.ui.tests.css.swt.TextTextTransformTest;
import org.eclipse.e4.ui.tests.css.swt.ThemeTest;
import org.eclipse.e4.ui.tests.css.swt.ThemesExtensionTest;
import org.eclipse.e4.ui.tests.css.swt.ToolItemTest;
import org.eclipse.e4.ui.tests.css.swt.TreeTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -78,6 +79,7 @@
IdClassLabelColorTest.class,
ShellTest.class,
ButtonTest.class,
ToolItemTest.class,
GradientTest.class,
MarginTest.class,
InnerClassElementTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*******************************************************************************
* Copyright (c) 2022 vogella 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
*
* Contributors:
* Lars Vogel- initial API and implementation
*******************************************************************************/
package org.eclipse.e4.ui.tests.css.swt;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.junit.Ignore;
import org.junit.Test;

public class ToolItemTest extends CSSSWTTestCase {

protected ToolItem createTestToolItem(String styleSheet, int styleBit) {
engine = createEngine(styleSheet, display);

// Create widgets
var shell = new Shell(display, SWT.SHELL_TRIM);
var layout = new FillLayout();
shell.setLayout(layout);

var panel = new Composite(shell, SWT.NONE);
panel.setLayout(new FillLayout());

ToolBar toolBar = new ToolBar(panel, SWT.FLAT);
var toolItemToTest = new ToolItem(toolBar, styleBit);
toolItemToTest.setText("Some text");

// Apply styles
// TODO should call on shell but ToolBar is currently not styling its children
// works in a real application via CSSSWTApplyStylesListener SWT.Skin event
engine.applyStyles(toolItemToTest, true);
shell.pack();
return toolItemToTest;
}

@Test
public void testBackgroundColor() {
var toolItemToTest = createTestToolItem("ToolItem { background-color: #FF0000;}",
SWT.PUSH);
assertEquals(RED, toolItemToTest.getBackground().getRGB());
}

@Test
public void testForegroundColor() {
var toolItemToTest = createTestToolItem("ToolItem { color: #FF0000;}", SWT.PUSH);
assertEquals(RED, toolItemToTest.getForeground().getRGB());
}

@Test
@Ignore("Not yet implemented")
public void testSelectedPseudo() {
var toolItemToTest = createTestToolItem(
"ToolItem { color: #FF0000; }\n" + "ToolItem:checked { color: #0000FF; }", SWT.PUSH);
assertEquals(RED, toolItemToTest.getForeground().getRGB());
toolItemToTest.setSelection(true);
engine.applyStyles(toolItemToTest, false);
assertEquals(BLUE, toolItemToTest.getForeground().getRGB());
}

@Test
public void ensurePseudoAttributeAllowsToSelectionPushButton() {
var toolItemToTest = createTestToolItem(
"ToolItem[style~='SWT.CHECK'] { background-color: #FF0000; color: #0000FF }", SWT.CHECK);

assertEquals(RED, toolItemToTest.getBackground().getRGB());
assertEquals(BLUE, toolItemToTest.getForeground().getRGB());

var unStyledBToolItem = createTestToolItem(
"ToolItem[style~='SWT.PUSH'] { background-color: #FF0000; color: #0000FF }", SWT.CHECK);

assertNotEquals(RED, unStyledBToolItem.getBackground().getRGB());
assertNotEquals(BLUE, unStyledBToolItem.getForeground().getRGB());

}
}

0 comments on commit c07ab53

Please sign in to comment.