Skip to content

Commit 86cedfc

Browse files
committed
[Win32] Extract mask-specific cursor handle creation to new provider
Currently, the distinction of how to create a cursor handle inside image-data-based providers depends on state of a given cursor instance and requires dummy values to be passed (such as `mask==null`). This change extracts the according behaviors into separate classes that encapsulate the required data and functionality.
1 parent 97c24b9 commit 86cedfc

File tree

1 file changed

+34
-26
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+34
-26
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Cursor.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public final class Cursor extends Resource {
120120
public Cursor(Device device, int style) {
121121
super(device);
122122
this.cursorHandleProvider = new StyleCursorHandleProvider(style);
123-
this.handle = this.cursorHandleProvider.createHandle(this, DEFAULT_ZOOM);
123+
this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM);
124124
init();
125125
this.device.registerResourceWithZoomSupport(this);
126126
}
@@ -160,8 +160,8 @@ public Cursor(Device device, int style) {
160160
*/
161161
public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY) {
162162
super(device);
163-
this.cursorHandleProvider = new ImageDataCursorHandleProvider(source, mask, hotspotX, hotspotY);
164-
this.handle = this.cursorHandleProvider.createHandle(this, DEFAULT_ZOOM);
163+
this.cursorHandleProvider = new ImageDataWithMaskCursorHandleProvider(source, mask, hotspotX, hotspotY);
164+
this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM);
165165
init();
166166
this.device.registerResourceWithZoomSupport(this);
167167
}
@@ -229,9 +229,9 @@ private static long setupCursorFromImageData(ImageData source, ImageData mask, i
229229
*/
230230
public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
231231
super(device);
232-
this.cursorHandleProvider = new ImageDataCursorHandleProvider(source, null, hotspotX, hotspotY);
232+
this.cursorHandleProvider = new ImageDataCursorHandleProvider(source, hotspotX, hotspotY);
233233
isIcon = true;
234-
this.handle = this.cursorHandleProvider.createHandle(this, DEFAULT_ZOOM);
234+
this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM);
235235
init();
236236
this.device.registerResourceWithZoomSupport(this);
237237
}
@@ -344,7 +344,7 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
344344
if (imageDataProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
345345
this.cursorHandleProvider = new ImageDataProviderCursorHandleProvider(imageDataProvider, hotspotX, hotspotY);
346346
isIcon = true;
347-
this.handle = this.cursorHandleProvider.createHandle(this, DEFAULT_ZOOM);
347+
this.handle = this.cursorHandleProvider.createHandle(device, DEFAULT_ZOOM);
348348
init();
349349
this.device.registerResourceWithZoomSupport(this);
350350
}
@@ -372,7 +372,7 @@ public static Long win32_getHandle (Cursor cursor, int zoom) {
372372
return cursor.zoomLevelToHandle.get(zoom);
373373
}
374374

375-
long handle = cursor.cursorHandleProvider.createHandle(cursor, zoom);
375+
long handle = cursor.cursorHandleProvider.createHandle(cursor.device, zoom);
376376
cursor.setHandleForZoomLevel(handle, zoom);
377377

378378
return cursor.zoomLevelToHandle.get(zoom);
@@ -502,7 +502,7 @@ void destroyHandlesExcept(Set<Integer> zoomLevels) {
502502
}
503503

504504
private static interface CursorHandleProvider {
505-
long createHandle(Cursor cursor, int zoom);
505+
long createHandle(Device device, int zoom);
506506
}
507507

508508
private static class StyleCursorHandleProvider implements CursorHandleProvider {
@@ -513,7 +513,7 @@ public StyleCursorHandleProvider(int style) {
513513
}
514514

515515
@Override
516-
public long createHandle(Cursor cursor, int zoom) {
516+
public long createHandle(Device device, int zoom) {
517517
// zoom ignored, LoadCursor handles scaling internally
518518
return setupCursorFromStyle(this.style);
519519
}
@@ -625,41 +625,49 @@ public ImageDataProviderCursorHandleProvider(ImageDataProvider provider, int hot
625625
}
626626

627627
@Override
628-
public long createHandle(Cursor cursor, int zoom) {
628+
public long createHandle(Device device, int zoom) {
629629
ImageData source;
630630
if (zoom == DEFAULT_ZOOM) {
631631
source = this.provider.getImageData(DEFAULT_ZOOM);
632632
} else {
633-
Image tempImage = new Image(cursor.device, this.provider);
633+
Image tempImage = new Image(device, this.provider);
634634
source = tempImage.getImageData(zoom);
635635
tempImage.dispose();
636636
}
637-
return setupCursorFromImageData(cursor.device, source, getHotpotXInPixels(zoom), getHotpotYInPixels(zoom));
637+
return setupCursorFromImageData(device, source, getHotpotXInPixels(zoom), getHotpotYInPixels(zoom));
638638
}
639639
}
640640

641641
private static class ImageDataCursorHandleProvider extends HotspotAwareCursorHandleProvider {
642-
private final ImageData source;
643-
private final ImageData mask;
642+
protected final ImageData source;
644643

645-
public ImageDataCursorHandleProvider(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
644+
public ImageDataCursorHandleProvider(ImageData source, int hotspotX, int hotspotY) {
646645
super(hotspotX, hotspotY);
647646
this.source = source;
647+
}
648+
649+
@Override
650+
public long createHandle(Device device, int zoom) {
651+
ImageData scaledSource = DPIUtil.scaleImageData(device, this.source, zoom, DEFAULT_ZOOM);
652+
return setupCursorFromImageData(device, scaledSource, getHotpotXInPixels(zoom),
653+
getHotpotYInPixels(zoom));
654+
}
655+
}
656+
657+
private static class ImageDataWithMaskCursorHandleProvider extends ImageDataCursorHandleProvider {
658+
private final ImageData mask;
659+
660+
public ImageDataWithMaskCursorHandleProvider(ImageData source, ImageData mask, int hotspotX, int hotspotY) {
661+
super(source, hotspotX, hotspotY);
648662
this.mask = mask;
649663
}
650664

651665
@Override
652-
public long createHandle(Cursor cursor, int zoom) {
653-
ImageData scaledSource = DPIUtil.scaleImageData(cursor.device, this.source, zoom, DEFAULT_ZOOM);
654-
if (cursor.isIcon) {
655-
return setupCursorFromImageData(cursor.device, scaledSource, getHotpotXInPixels(zoom),
656-
getHotpotYInPixels(zoom));
657-
} else {
658-
ImageData scaledMask = this.mask != null ? DPIUtil.scaleImageData(cursor.device, mask, zoom, DEFAULT_ZOOM)
659-
: null;
660-
return setupCursorFromImageData(scaledSource, scaledMask, getHotpotXInPixels(zoom),
661-
getHotpotYInPixels(zoom));
662-
}
666+
public long createHandle(Device device, int zoom) {
667+
ImageData scaledSource = DPIUtil.scaleImageData(device, this.source, zoom, DEFAULT_ZOOM);
668+
ImageData scaledMask = this.mask != null ? DPIUtil.scaleImageData(device, mask, zoom, DEFAULT_ZOOM)
669+
: null;
670+
return setupCursorFromImageData(scaledSource, scaledMask, getHotpotXInPixels(zoom), getHotpotYInPixels(zoom));
663671
}
664672
}
665673

0 commit comments

Comments
 (0)