Skip to content

Commit 49d2fcc

Browse files
committed
[FIXUP] Rework ImageDrawer too
1 parent 5756e3d commit 49d2fcc

File tree

7 files changed

+140
-31
lines changed

7 files changed

+140
-31
lines changed

binaries/org.eclipse.swt.win32.win32.x86_64/.settings/.api_filters

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,11 @@
582582
</filter>
583583
</resource>
584584
<resource path="Eclipse SWT/common/org/eclipse/swt/graphics/ImageGcDrawer.java" type="org.eclipse.swt.graphics.ImageGcDrawer">
585-
<filter id="404000815">
585+
<filter id="403984517">
586586
<message_arguments>
587587
<message_argument value="org.eclipse.swt.graphics.ImageGcDrawer"/>
588-
<message_argument value="getGcStyle()"/>
588+
<message_argument value="org.eclipse.swt.graphics.ImageDrawer"/>
589+
<message_argument value="withGCStyle(int, ImageDrawer)"/>
589590
</message_arguments>
590591
</filter>
591592
</resource>

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Image.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public final class Image extends Resource implements Drawable {
142142
/**
143143
* ImageGcDrawer to provide a callback to draw on a GC for various zoom levels
144144
*/
145-
private ImageGcDrawer imageGcDrawer;
145+
private ImageDrawer imageGcDrawer;
146146

147147
/**
148148
* Style flag used to differentiate normal, gray-scale and disabled images based
@@ -880,7 +880,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
880880
* </ul>
881881
* @since 3.130
882882
*/
883-
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
883+
public Image(Device device, int width, int height, ImageDrawer imageGcDrawer) {
884884
super(device);
885885
if (imageGcDrawer == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
886886
this.imageGcDrawer = imageGcDrawer;
@@ -914,13 +914,13 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
914914
this(device, width, height, imageGcDrawer);
915915
}
916916

917-
private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, int height, int zoom) {
917+
private ImageData drawWithImageGcDrawer(ImageDrawer imageGcDrawer, int width, int height, int zoom) {
918918
Image image = new Image(device, width, height);
919-
GC gc = new GC(image);
919+
GC gc = new GC(image, ExtendedImageDrawer.getGCStyle(imageGcDrawer));
920920
try {
921921
imageGcDrawer.drawOn(gc, width, height);
922922
ImageData imageData = image.getImageData(zoom);
923-
imageGcDrawer.postProcess(imageData);
923+
ExtendedImageDrawer.postProcess(imageGcDrawer, imageData);
924924
return imageData;
925925
} finally {
926926
gc.dispose();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.graphics;
15+
16+
import java.util.function.*;
17+
18+
import org.eclipse.swt.*;
19+
import org.eclipse.swt.internal.image.*;
20+
21+
/**
22+
* Interface to provide a callback mechanism to draw on different GC instances
23+
* depending on the zoom the image will be used for. A common use case is when
24+
* the application is moved from a low DPI monitor to a high DPI monitor. This
25+
* provides API which will be called by SWT during the image rendering.
26+
*
27+
* This interface needs to be implemented by client code to provide logic that
28+
* draws on the empty GC on demand.
29+
*
30+
* @since 3.130
31+
*/
32+
@FunctionalInterface
33+
public interface ImageDrawer {
34+
35+
/**
36+
* Draws an image on a GC for a requested zoom level.
37+
*
38+
* @param gc The GC will draw on the underlying Image and is configured
39+
* for the targeted zoom
40+
* @param imageWidth The width of the image in points to draw on
41+
* @param imageHeight The height of the image in points to draw on
42+
*/
43+
void drawOn(GC gc, int imageWidth, int imageHeight);
44+
45+
/**
46+
* @since 3.130
47+
*/
48+
public static ImageDrawer withGCStyle(int style, ImageDrawer drawer) {
49+
if (drawer instanceof ExtendedImageDrawer extendedDrawer) {
50+
return new ExtendedImageDrawer(extendedDrawer.original(), extendedDrawer.postProcessor(), style);
51+
}
52+
return new ExtendedImageDrawer(drawer, null, style);
53+
}
54+
55+
/**
56+
* @since 3.130
57+
*/
58+
public static ImageDrawer create(ImageDrawer drawer, Consumer<ImageData> postProcessor) {
59+
if (drawer instanceof ExtendedImageDrawer extendedDrawer) {
60+
return new ExtendedImageDrawer(extendedDrawer.original(), postProcessor, extendedDrawer.style());
61+
}
62+
return new ExtendedImageDrawer(drawer, postProcessor, SWT.NONE);
63+
}
64+
65+
// TODO: or use some kind of builder pattern?
66+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageGcDrawer.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.graphics;
1515

16-
import org.eclipse.swt.*;
17-
1816
/**
1917
* Interface to provide a callback mechanism to draw on different GC instances
2018
* depending on the zoom the image will be used for. A common use case is when
@@ -26,7 +24,8 @@
2624
*
2725
* @since 3.129
2826
*/
29-
public interface ImageGcDrawer {
27+
@Deprecated(forRemoval = true, since = "2025-06 (removal in 2027-06 or later)")
28+
public interface ImageGcDrawer extends ImageDrawer {
3029

3130
/**
3231
* Draws an image on a GC for a requested zoom level.
@@ -36,6 +35,7 @@ public interface ImageGcDrawer {
3635
* @param imageWidth The width of the image in points to draw on
3736
* @param imageHeight The height of the image in points to draw on
3837
*/
38+
@Override
3939
void drawOn(GC gc, int imageWidth, int imageHeight);
4040

4141
/**
@@ -47,15 +47,4 @@ public interface ImageGcDrawer {
4747
default void postProcess(ImageData imageData) {
4848
}
4949

50-
/**
51-
* Returns the GC style used when creating the GC instance. Default
52-
* implementation returns <code>SWT.NONE</code>.
53-
*
54-
* @return the GC style constant
55-
*
56-
* @since 3.130
57-
*/
58-
default int getGcStyle() {
59-
return SWT.NONE;
60-
}
6150
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Hannes Wellmann and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Hannes Wellmann - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.swt.internal.image;
16+
17+
import java.util.*;
18+
import java.util.function.*;
19+
20+
import org.eclipse.swt.*;
21+
import org.eclipse.swt.graphics.*;
22+
23+
public record ExtendedImageDrawer(ImageDrawer original, Consumer<ImageData> postProcessor, int style)
24+
implements ImageDrawer {
25+
26+
public ExtendedImageDrawer {
27+
Objects.requireNonNull(original);
28+
}
29+
30+
@Override
31+
public void drawOn(GC gc, int imageWidth, int imageHeight) {
32+
original.drawOn(gc, imageWidth, imageHeight);
33+
}
34+
35+
public static int getGCStyle(ImageDrawer drawer) {
36+
if (drawer instanceof ExtendedImageDrawer extendedDrawer) {
37+
return extendedDrawer.style();
38+
}
39+
return SWT.NONE;
40+
}
41+
42+
public static void postProcess(ImageDrawer drawer, ImageData data) {
43+
if (drawer instanceof @SuppressWarnings("removal") ImageGcDrawer gcDrawer) {
44+
gcDrawer.postProcess(data);
45+
} else if (drawer instanceof ExtendedImageDrawer extendedDrawer) {
46+
Consumer<ImageData> postProcessor2 = extendedDrawer.postProcessor();
47+
if (postProcessor2 != null) {
48+
postProcessor2.accept(data);
49+
}
50+
}
51+
}
52+
53+
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public final class Image extends Resource implements Drawable {
156156
/**
157157
* ImageGcDrawer to provide a callback to draw on a GC for various zoom levels
158158
*/
159-
private ImageGcDrawer imageGcDrawer;
159+
private ImageDrawer imageGcDrawer;
160160

161161
/**
162162
* Style flag used to differentiate normal, gray-scale and disabled images based
@@ -672,7 +672,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
672672
* </ul>
673673
* @since 3.130
674674
*/
675-
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
675+
public Image(Device device, int width, int height, ImageDrawer imageGcDrawer) {
676676
super(device);
677677
if (imageGcDrawer == null) {
678678
SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -1168,11 +1168,11 @@ public ImageData getImageData (int zoom) {
11681168

11691169
private ImageData drawWithImageGcDrawer(int width, int height, int zoom) {
11701170
Image image = new Image(device, width, height);
1171-
GC gc = new GC(image);
1171+
GC gc = new GC(image, ExtendedImageDrawer.getGCStyle(imageGcDrawer));
11721172
try {
11731173
imageGcDrawer.drawOn(gc, width, height);
11741174
ImageData imageData = image.getImageData(zoom);
1175-
imageGcDrawer.postProcess(imageData);
1175+
ExtendedImageDrawer.postProcess(imageGcDrawer, imageData);
11761176
return imageData;
11771177
} finally {
11781178
gc.dispose();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
631631
* </ul>
632632
* @since 3.130
633633
*/
634-
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
634+
public Image(Device device, int width, int height, ImageDrawer imageGcDrawer) {
635635
super(device);
636636
this.imageProvider = new ImageGcDrawerWrapper(imageGcDrawer, width, height);
637637
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
@@ -2485,12 +2485,12 @@ ImageDataProviderWrapper createCopy(Image image) {
24852485
}
24862486

24872487
private class ImageGcDrawerWrapper extends DynamicImageProviderWrapper {
2488-
private ImageGcDrawer drawer;
2488+
private ImageDrawer drawer;
24892489
private int width;
24902490
private int height;
24912491

2492-
ImageGcDrawerWrapper(ImageGcDrawer imageGcDrawer, int width, int height) {
2493-
checkProvider(imageGcDrawer, ImageGcDrawer.class);
2492+
ImageGcDrawerWrapper(ImageDrawer imageGcDrawer, int width, int height) {
2493+
checkProvider(imageGcDrawer, ImageDrawer.class);
24942494
this.drawer = imageGcDrawer;
24952495
this.width = width;
24962496
this.height = height;
@@ -2511,12 +2511,12 @@ ImageData getImageData(int zoom) {
25112511
ImageHandle getImageMetadata(int zoom) {
25122512
initialNativeZoom = zoom;
25132513
Image image = new Image(device, width, height, zoom);
2514-
GC gc = new GC(image, drawer.getGcStyle());
2514+
GC gc = new GC(image, ExtendedImageDrawer.getGCStyle(drawer));
25152515
try {
25162516
gc.data.nativeZoom = zoom;
25172517
drawer.drawOn(gc, width, height);
25182518
ImageData imageData = image.getImageMetadata(zoom).getImageData();
2519-
drawer.postProcess(imageData);
2519+
ExtendedImageDrawer.postProcess(drawer, imageData);
25202520
ImageData newData = adaptImageDataIfDisabledOrGray(imageData);
25212521
init(newData, zoom);
25222522
} finally {

0 commit comments

Comments
 (0)