Skip to content

Commit 5fa04e4

Browse files
committed
Always set disabled icons in AbstractContributionItem
AbstractionContributionItems only set a disabled icon on the ToolItem in case such a disabled icon is specified via an explicit URI to that icon. If no such URI is specified, the ToolItem creates a disabled icon on the fly. Since the ToolItem creates a new image every time the enablement state of the ToolItem changes, this leads to unnecessary repeated creation of images. In contrast, ActionContributionItems always set the disabled icon for their ToolItem, even if no disabled image descriptor is specified for them, to avoid that the ToolItem repeatedly created that icon. This change align the AbstractContributionItem handling for disabled icons with the one of ActionContributionItems: in case no URI to a disabled icon is specified, the SWT disablement mechanism is used to create a disabled version and to explicitly set it at the ToolItem. This avoids unnecessary repeated creation of the disabled version of an icon for those items.
1 parent 208faf9 commit 5fa04e4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/AbstractContributionItem.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,17 @@ void setResourceUtils(IResourceUtilities<ImageDescriptor> utils) {
149149
}
150150

151151
protected Image getImage(String iconURI, LocalResourceManager resourceManager) {
152+
return createImage(iconURI, resourceManager, false);
153+
}
154+
155+
private Image createImage(String iconURI, LocalResourceManager resourceManager, boolean disabled) {
152156
Image image = null;
153157

154158
if (iconURI != null && iconURI.length() > 0) {
155159
ImageDescriptor iconDescriptor = resUtils.imageDescriptorFromURI(URI.createURI(iconURI));
160+
if (disabled) {
161+
iconDescriptor = ImageDescriptor.createWithFlags(iconDescriptor, SWT.IMAGE_DISABLE);
162+
}
156163
if (iconDescriptor != null) {
157164
try {
158165
image = resourceManager.create(iconDescriptor);
@@ -169,6 +176,10 @@ protected Image getImage(String iconURI, LocalResourceManager resourceManager) {
169176
return image;
170177
}
171178

179+
private Image getDisabledImage(String iconURI, LocalResourceManager resourceManager) {
180+
return createImage(iconURI, resourceManager, true);
181+
}
182+
172183
protected void updateIcons() {
173184
if (!(widget instanceof Item)) {
174185
return;
@@ -184,14 +195,23 @@ protected void updateIcons() {
184195
Image iconImage = getImage(iconURI, resourceManager);
185196
item.setImage(iconImage);
186197
item.setData(ICON_URI, iconURI);
187-
if (item instanceof ToolItem) {
198+
if (item instanceof ToolItem toolItem) {
188199
iconImage = getImage(disabledURI, resourceManager);
189-
((ToolItem) item).setDisabledImage(iconImage);
190-
item.setData(DISABLED_URI, disabledURI);
200+
toolItem.setDisabledImage(iconImage);
201+
toolItem.setData(DISABLED_URI, disabledURI);
191202
}
192203
disposeOldImages();
193204
localResourceManager = resourceManager;
194205
}
206+
// if there is no explicit disabled icon and the element becomes disabled,
207+
// create it
208+
boolean hasExplicitDisabledImage = "".equals(disabledURI); //$NON-NLS-1$
209+
if (!modelItem.isEnabled() && !hasExplicitDisabledImage && item instanceof ToolItem toolItem) {
210+
if (toolItem.getDisabledImage() == null) {
211+
Image iconImage = getDisabledImage(iconURI, localResourceManager);
212+
toolItem.setDisabledImage(iconImage);
213+
}
214+
}
195215
}
196216

197217
private String getDisabledIconURI(MItem toolItem) {

0 commit comments

Comments
 (0)