Skip to content

Commit 8e938bd

Browse files
committed
[90] Fix cursor initialization when using fractional scaling
Recent Eclipse version support fractional zoom level as opposed to 100% or 200% zoom. Because no icons exist for those levels, an IllegalArgumentException is thrown when trying to create the corresponding cursors. In order to solve this, one has to consider the following cases: 1) Newer SWT versions provide a Cursor constructor that accepts an ImageDataProvider. This constructor always creates image data matching the device zoom. 2) Win32: Older SWT versions automatically upscale the image data to match the device zoom. Therefore the original image data must be at 100% zoom. 3) On other operating systems, the image data should be pre-scaled, as no further scaling is done by SWT.
1 parent 0be4aeb commit 8e938bd

File tree

1 file changed

+41
-4
lines changed
  • bundles/org.eclipse.gmf.runtime.gef.ui/src/org/eclipse/gmf/runtime/gef/ui/internal/l10n

1 file changed

+41
-4
lines changed

bundles/org.eclipse.gmf.runtime.gef.ui/src/org/eclipse/gmf/runtime/gef/ui/internal/l10n/Cursors.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************************
2-
* Copyright (c) 2002, 2003 IBM Corporation and others.
2+
* Copyright (c) 2002, 2025 IBM Corporation and others.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -12,7 +12,15 @@
1212

1313
package org.eclipse.gmf.runtime.gef.ui.internal.l10n;
1414

15+
import java.lang.reflect.Constructor;
16+
17+
import org.eclipse.jface.resource.ImageDescriptor;
18+
import org.eclipse.swt.SWT;
1519
import org.eclipse.swt.graphics.Cursor;
20+
import org.eclipse.swt.graphics.Device;
21+
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageData;
23+
import org.eclipse.swt.graphics.ImageDataProvider;
1624

1725

1826
/**
@@ -37,11 +45,31 @@ public class Cursors {
3745
private static int deviceZoom = -1;
3846

3947
static {
40-
CURSOR_SEG_ADD = new Cursor(null, GefUIPluginImages.DESC_SEG_ADD.getImageData(getDeviceZoom()), 0, 0);
41-
CURSOR_SEG_MOVE = new Cursor(null, GefUIPluginImages.DESC_SEG_MOVE.getImageData(getDeviceZoom()), 0, 0);
48+
CURSOR_SEG_ADD = createCursor(GefUIPluginImages.DESC_SEG_ADD, 0, 0);
49+
CURSOR_SEG_MOVE = createCursor(GefUIPluginImages.DESC_SEG_MOVE, 0, 0);
4250
}
4351

44-
// Taken from org.eclipse.gef.SharedCursors.java
52+
// Taken from org.eclipse.gef.internal.InternalGEFPlugin.java
53+
private static Cursor createCursor(ImageDescriptor source, int hotspotX, int hotspotY) {
54+
ImageDataProvider imageDataProvider = zoom -> getScaledImageData(source, zoom);
55+
try {
56+
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
57+
int.class);
58+
return ctor.newInstance(null, imageDataProvider, hotspotX, hotspotY);
59+
} catch (NoSuchMethodException e) {
60+
// SWT version < 3.131.0
61+
} catch (ReflectiveOperationException e) {
62+
throw new RuntimeException("Failed to instantiate Cursor", e); //$NON-NLS-1$
63+
}
64+
// Note: On Windows, the image data is automatically scaled to match the device zoom
65+
int deviceZoom = 100;
66+
if (!"win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
67+
deviceZoom = getDeviceZoom();
68+
}
69+
return new Cursor(null, imageDataProvider.getImageData(deviceZoom), hotspotX, hotspotY);
70+
}
71+
72+
// Taken from org.eclipse.gef.SharedCursors.java
4573
private static int getDeviceZoom() {
4674
if (deviceZoom == -1) {
4775
deviceZoom = 100; // default value
@@ -56,4 +84,13 @@ private static int getDeviceZoom() {
5684
}
5785
return deviceZoom;
5886
}
87+
88+
private static ImageData getScaledImageData(ImageDescriptor descriptor, int zoom) {
89+
Image image = descriptor.createImage();
90+
try {
91+
return image.getImageData(zoom);
92+
} finally {
93+
image.dispose();
94+
}
95+
}
5996
}

0 commit comments

Comments
 (0)