Skip to content

Commit f826b62

Browse files
committed
Cursors in GEF uses Image instead of ImageData
This is required for the cursor images to be scaled appropriately according to the given zoom
1 parent 9d92673 commit f826b62

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
*******************************************************************************/
1313
package org.eclipse.gef;
1414

15+
import java.lang.reflect.Constructor;
16+
import java.lang.reflect.InvocationTargetException;
17+
1518
import org.eclipse.swt.graphics.Cursor;
19+
import org.eclipse.swt.graphics.Device;
20+
import org.eclipse.swt.graphics.ImageDataProvider;
1621

1722
import org.eclipse.jface.resource.ImageDescriptor;
1823

@@ -45,15 +50,24 @@ public class SharedCursors extends Cursors {
4550
public static final Cursor CURSOR_TREE_MOVE;
4651

4752
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$
53+
CURSOR_PLUG = createCursor("icons/plug-cursor.svg"); //$NON-NLS-1$
54+
CURSOR_PLUG_NOT = createCursor("icons/plugnot-cursor.svg"); //$NON-NLS-1$
55+
CURSOR_TREE_ADD = createCursor("icons/tree_add-cursor.svg"); //$NON-NLS-1$
56+
CURSOR_TREE_MOVE = createCursor("icons/tree_move-cursor.svg"); //$NON-NLS-1$
5257
}
5358

5459
private static Cursor createCursor(String sourceName) {
5560
ImageDescriptor src = InternalImages.createDescriptor(sourceName);
56-
return new Cursor(null, src.getImageData(100), 0, 0);
61+
try {
62+
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
63+
int.class);
64+
return ctor.newInstance(null, (ImageDataProvider) src::getImageData, 0, 0);
65+
} catch (NoSuchMethodException e) {
66+
// SWT version < 3.131.0 (no ImageDataProvider-based constructor)
67+
return new Cursor(null, src.getImageData(100), 0, 0); // older constructor
68+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
69+
throw new RuntimeException("Failed to instantiate Cursor", e); //$NON-NLS-1$
70+
}
5771
}
5872

5973
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package org.eclipse.gef.ui.palette;
1414

1515
import java.beans.PropertyChangeSupport;
16+
import java.lang.reflect.Constructor;
17+
import java.lang.reflect.InvocationTargetException;
1618
import java.util.ArrayList;
1719
import java.util.List;
1820
import java.util.function.BiConsumer;
@@ -32,9 +34,10 @@
3234
import org.eclipse.swt.events.MouseTrackAdapter;
3335
import org.eclipse.swt.events.MouseTrackListener;
3436
import org.eclipse.swt.graphics.Cursor;
37+
import org.eclipse.swt.graphics.Device;
3538
import org.eclipse.swt.graphics.Font;
3639
import org.eclipse.swt.graphics.GC;
37-
import org.eclipse.swt.graphics.ImageData;
40+
import org.eclipse.swt.graphics.ImageDataProvider;
3841
import org.eclipse.swt.graphics.Point;
3942
import org.eclipse.swt.graphics.Rectangle;
4043
import org.eclipse.swt.widgets.Canvas;
@@ -88,7 +91,6 @@
8891
import org.eclipse.gef.GraphicalViewer;
8992
import org.eclipse.gef.dnd.TemplateTransfer;
9093
import org.eclipse.gef.internal.GEFMessages;
91-
import org.eclipse.gef.internal.InternalGEFPlugin;
9294
import org.eclipse.gef.internal.InternalImages;
9395
import org.eclipse.gef.ui.views.palette.PaletteView;
9496

@@ -1608,13 +1610,19 @@ public static Cursor getCursor(int code) {
16081610
*/
16091611
private static Cursor createCursor(String sourceName) {
16101612
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);
16141613
// 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);
1614+
int hotspotX = source.getImageData(100).width / 2;
1615+
int hotspotY = source.getImageData(100).height / 2;
1616+
try {
1617+
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
1618+
int.class);
1619+
return ctor.newInstance(null, (ImageDataProvider) source::getImageData, hotspotX, hotspotY);
1620+
} catch (NoSuchMethodException e) {
1621+
// SWT version < 3.131.0 (no ImageDataProvider-based constructor)
1622+
return new Cursor(null, source.getImageData(100), hotspotX, hotspotY); // older constructor
1623+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
1624+
throw new RuntimeException("Failed to instantiate Cursor", e); //$NON-NLS-1$
1625+
}
16181626
}
16191627
}
16201628
}

0 commit comments

Comments
 (0)