Skip to content

Commit e2ba6cd

Browse files
arunjose696ptziegler
authored andcommitted
Cursors in GEF uses ImageDataProvider instead of ImageData
This is required for the cursor images to be scaled appropriately according to the given zoom
1 parent 9d92673 commit e2ba6cd

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

org.eclipse.gef/src/org/eclipse/gef/SharedCursors.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.eclipse.draw2d.Cursors;
2020

21+
import org.eclipse.gef.internal.InternalGEFPlugin;
2122
import org.eclipse.gef.internal.InternalImages;
2223

2324
/**
@@ -45,15 +46,15 @@ public class SharedCursors extends Cursors {
4546
public static final Cursor CURSOR_TREE_MOVE;
4647

4748
static {
48-
CURSOR_PLUG = createCursor("icons/plug-cursor.png"); //$NON-NLS-1$
49-
CURSOR_PLUG_NOT = createCursor("icons/plugnot-cursor.png"); //$NON-NLS-1$
50-
CURSOR_TREE_ADD = createCursor("icons/tree_add-cursor.png"); //$NON-NLS-1$
51-
CURSOR_TREE_MOVE = createCursor("icons/tree_move-cursor.png"); //$NON-NLS-1$
49+
CURSOR_PLUG = createCursor("icons/plug-cursor.svg"); //$NON-NLS-1$
50+
CURSOR_PLUG_NOT = createCursor("icons/plugnot-cursor.svg"); //$NON-NLS-1$
51+
CURSOR_TREE_ADD = createCursor("icons/tree_add-cursor.svg"); //$NON-NLS-1$
52+
CURSOR_TREE_MOVE = createCursor("icons/tree_move-cursor.svg"); //$NON-NLS-1$
5253
}
5354

5455
private static Cursor createCursor(String sourceName) {
5556
ImageDescriptor src = InternalImages.createDescriptor(sourceName);
56-
return new Cursor(null, src.getImageData(100), 0, 0);
57+
return InternalGEFPlugin.createCursor(src, 0, 0);
5758
}
5859

5960
}

org.eclipse.gef/src/org/eclipse/gef/internal/InternalGEFPlugin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
import java.io.ByteArrayInputStream;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.lang.reflect.Constructor;
20+
import java.lang.reflect.InvocationTargetException;
1921
import java.nio.charset.StandardCharsets;
2022

2123
import org.eclipse.swt.SWTException;
24+
import org.eclipse.swt.graphics.Cursor;
25+
import org.eclipse.swt.graphics.Device;
2226
import org.eclipse.swt.graphics.Image;
2327
import org.eclipse.swt.graphics.ImageData;
28+
import org.eclipse.swt.graphics.ImageDataProvider;
2429
import org.eclipse.swt.graphics.ImageLoader;
2530

2631
import org.eclipse.core.runtime.Platform;
@@ -146,4 +151,23 @@ public static boolean isSvgSupported() {
146151
}
147152
return isSvgSupported;
148153
}
154+
155+
/**
156+
* This method attempts to create the cursor using a constructor introduced in
157+
* SWT 3.131.0 that takes an {@link ImageDataProvider}. If this constructor is
158+
* not available (SWT versions prior to 3.131.0), it falls back to using the
159+
* older constructor that accepts {@link ImageData}.
160+
*/
161+
public static Cursor createCursor(ImageDescriptor source, int hotspotX, int hotspotY) {
162+
try {
163+
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
164+
int.class);
165+
return ctor.newInstance(null, (ImageDataProvider) source::getImageData, hotspotX, hotspotY);
166+
} catch (NoSuchMethodException e) {
167+
// SWT version < 3.131.0 (no ImageDataProvider-based constructor)
168+
return new Cursor(null, source.getImageData(100), hotspotX, hotspotY); // older constructor
169+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
170+
throw new RuntimeException("Failed to instantiate Cursor", e); //$NON-NLS-1$
171+
}
172+
}
149173
}

org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.eclipse.swt.graphics.Cursor;
3535
import org.eclipse.swt.graphics.Font;
3636
import org.eclipse.swt.graphics.GC;
37-
import org.eclipse.swt.graphics.ImageData;
3837
import org.eclipse.swt.graphics.Point;
3938
import org.eclipse.swt.graphics.Rectangle;
4039
import org.eclipse.swt.widgets.Canvas;
@@ -1608,13 +1607,10 @@ public static Cursor getCursor(int code) {
16081607
*/
16091608
private static Cursor createCursor(String sourceName) {
16101609
ImageDescriptor source = PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(sourceName);
1611-
int deviceZoom = InternalGEFPlugin.getOrDefaultDeviceZoom();
1612-
// Scales the image if the display is using neither 100% nor 200% zoom
1613-
ImageData sourceData = InternalGEFPlugin.scaledImageData(source, deviceZoom);
16141610
// Hotspot should be the center of the image. e.g. (16, 16) on 100% zoom
1615-
int hotspotX = sourceData.width / 2;
1616-
int hotspotY = sourceData.height / 2;
1617-
return new Cursor(null, sourceData, hotspotX, hotspotY);
1611+
int hotspotX = source.getImageData(100).width / 2;
1612+
int hotspotY = source.getImageData(100).height / 2;
1613+
return InternalGEFPlugin.createCursor(source, hotspotX, hotspotY);
16181614
}
16191615
}
16201616
}

0 commit comments

Comments
 (0)