Skip to content

Commit 9d64802

Browse files
committed
adjust interface
1 parent aadbfa2 commit 9d64802

File tree

13 files changed

+153
-144
lines changed

13 files changed

+153
-144
lines changed

binaries/org.eclipse.swt.win32.win32.x86_64/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
33
Bundle-Name: %fragmentName
44
Bundle-Vendor: %providerName
55
Bundle-SymbolicName: org.eclipse.swt.win32.win32.x86_64; singleton:=true
6-
Bundle-Version: 4.0.0.qualifier
6+
Bundle-Version: 3.131.0.qualifier
77
Bundle-ManifestVersion: 2
88
Bundle-Localization: fragment
99
Export-Package:

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static boolean canLoadAtZoom(InputStream stream, int fileZoom, int target
4141
return ImageLoader.canLoadAtZoom(stream, fileZoom, targetZoom);
4242
}
4343

44+
public static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
45+
return ImageLoader.canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
46+
}
47+
4448
public static ElementAtZoom<ImageData> load(InputStream stream, int fileZoom, int targetZoom) {
4549
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(stream, fileZoom, targetZoom);
4650
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
@@ -51,20 +55,24 @@ public static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoo
5155
return ImageLoader.canLoadAtZoom(filename, fileZoom, targetZoom);
5256
}
5357

58+
public static boolean canLoadAtTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
59+
return ImageLoader.canLoadAtTargetSize(filename, srcWidth, srcHeight, targetWidth, targetHeight);
60+
}
61+
5462
public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int targetZoom) {
5563
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(filename, fileZoom, targetZoom);
5664
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
5765
return data.get(0);
5866
}
5967

60-
public static ElementAtTargetSize<ImageData> loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
61-
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(stream, targetWidth, targetHeight);
68+
public static ElementAtTargetSize<ImageData> loadByTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
69+
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
6270
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
6371
return data.get(0);
6472
}
6573

66-
public static ElementAtTargetSize<ImageData> loadByTargetSize(String filename, int targetWidth, int targetHeight) {
67-
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(filename, targetWidth, targetHeight);
74+
public static ElementAtTargetSize<ImageData> loadByTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
75+
List<ElementAtTargetSize<ImageData>> data = new ImageLoader().loadByTargetSize(filename, srcWidth, srcHeight, targetWidth, targetHeight);
6876
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
6977
return data.get(0);
7078
}

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

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

16-
import java.util.function.*;
17-
1816
/**
1917
* Interface to provide a callback mechanism to get information about images
2018
* when the application is moved from a low DPI monitor to a high DPI monitor.
@@ -45,34 +43,4 @@ public interface ImageDataProvider {
4543
*/
4644
ImageData getImageData (int zoom);
4745

48-
/**
49-
* @since 4.0
50-
*/
51-
default ImageData getImageData(int targetWidth, int targetHeight) {
52-
return null;
53-
}
54-
55-
/**
56-
* @since 4.0
57-
*/
58-
static ImageDataProvider fromZoom(IntFunction<ImageData> zoomFunction) {
59-
return zoomFunction::apply;
60-
}
61-
62-
/**
63-
* @since 4.0
64-
*/
65-
static ImageDataProvider fromSize(BiFunction<Integer, Integer, ImageData> sizeFunction) {
66-
return new SizeAwareImageDataProvider() {
67-
@Override
68-
public ImageData getImageData(int targetWidth, int targetHeight) {
69-
return sizeFunction.apply(targetWidth, targetHeight);
70-
}
71-
72-
@Override
73-
public ImageData getImageData(int zoom) {
74-
throw new UnsupportedOperationException();
75-
}
76-
};
77-
}
7846
}

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

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

16-
import java.util.function.*;
17-
1816
/**
1917
* Interface to provide a callback mechanism to get information about images
2018
* when the application is moved from a low DPI monitor to a high DPI monitor.
@@ -45,34 +43,4 @@ public interface ImageFileNameProvider {
4543
*/
4644
String getImagePath (int zoom);
4745

48-
/**
49-
* @since 4.0
50-
*/
51-
default String getImagePath(int targetWidth, int targetHeight) {
52-
return null;
53-
}
54-
55-
/**
56-
* @since 4.0
57-
*/
58-
static ImageFileNameProvider fromZoom(IntFunction<String> zoomFunction) {
59-
return zoomFunction::apply;
60-
}
61-
62-
/**
63-
* @since 4.0
64-
*/
65-
static ImageFileNameProvider fromSize(BiFunction<Integer, Integer, String> sizeFunction) {
66-
return new SizeAwareImageFileNameProvider() {
67-
@Override
68-
public String getImagePath(int targetWidth, int targetHeight) {
69-
return sizeFunction.apply(targetWidth, targetHeight);
70-
}
71-
72-
@Override
73-
public String getImagePath(int zoom) {
74-
throw new UnsupportedOperationException();
75-
}
76-
};
77-
}
7846
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int target
163163
return images;
164164
}
165165

166-
List<ElementAtTargetSize<ImageData>> loadByTargetSize(InputStream stream, int targetWidth, int targetHeight) {
166+
List<ElementAtTargetSize<ImageData>> loadByTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
167167
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
168168
reset();
169-
List<ElementAtTargetSize<ImageData>> images = NativeImageLoader.load(new ElementAtTargetSize<>(stream, targetWidth, targetHeight), this, targetWidth, targetHeight);
169+
List<ElementAtTargetSize<ImageData>> images = NativeImageLoader.load(new ElementAtTargetSize<>(stream, srcWidth, srcHeight), this, targetWidth, targetHeight);
170170
data = images.stream().map(ElementAtTargetSize::element).toArray(ImageData[]::new);
171171
return images;
172172
}
@@ -177,6 +177,11 @@ static boolean canLoadAtZoom(InputStream stream, int fileZoom, int targetZoom) {
177177
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(stream, fileZoom), targetZoom);
178178
}
179179

180+
static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
181+
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
182+
return FileFormat.canLoadAtTargetSize(new ElementAtTargetSize<>(stream, srcWidth, srcHeight), targetWidth, targetHeight);
183+
}
184+
180185
/**
181186
* Loads an array of <code>ImageData</code> objects from the
182187
* file with the specified name. Throws an error if either
@@ -210,10 +215,10 @@ List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoo
210215
return null;
211216
}
212217

213-
List<ElementAtTargetSize<ImageData>> loadByTargetSize(String filename, int targetWidth, int targetHeight) {
218+
List<ElementAtTargetSize<ImageData>> loadByTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
214219
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
215220
try (InputStream stream = new FileInputStream(filename)) {
216-
return loadByTargetSize(stream, targetWidth, targetHeight);
221+
return loadByTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
217222
} catch (IOException e) {
218223
SWT.error(SWT.ERROR_IO, e);
219224
}
@@ -230,6 +235,16 @@ static boolean canLoadAtZoom(String filename, int fileZoom, int targetZoom) {
230235
return false;
231236
}
232237

238+
static boolean canLoadAtTargetSize(String filename, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
239+
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
240+
try (InputStream stream = new FileInputStream(filename)) {
241+
return canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
242+
} catch (IOException e) {
243+
SWT.error(SWT.ERROR_IO, e);
244+
}
245+
return false;
246+
}
247+
233248
/**
234249
* Saves the image data in this ImageLoader to the specified stream.
235250
* The format parameter can have one of the following values:
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package org.eclipse.swt.graphics;
22

3+
import java.io.*;
4+
35
/**
4-
* @since 4.0
6+
* @since 3.131
57
*/
68
public interface SizeAwareImageDataProvider extends ImageDataProvider {
7-
@Override
9+
/**
10+
* Returns the image file path suitable for rendering at the given target size.
11+
*
12+
* <p>This method should only be called if {@link #canLoadAtTargetSize} returns <code>true</code>.</p>
13+
*
14+
* @throws UnsupportedOperationException if called when not supported
15+
*/
816
ImageData getImageData(int targetWidth, int targetHeight);
17+
18+
@Override
19+
default ImageData getImageData(int zoom) {
20+
throw new UnsupportedOperationException();
21+
};
22+
23+
static boolean canLoadAtTargetSize(InputStream stream, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
24+
return ImageDataLoader.canLoadAtTargetSize(stream, srcWidth, srcHeight, targetWidth, targetHeight);
25+
}
926
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
package org.eclipse.swt.graphics;
22

33
/**
4-
* @since 4.0
4+
* @since 3.131
55
*/
66
public interface SizeAwareImageFileNameProvider extends ImageFileNameProvider {
7-
@Override
7+
8+
/**
9+
* Returns the image file path suitable for rendering at the given target size.
10+
*
11+
* <p>This method should only be called if {@link #canLoadAtTargetSize} returns <code>true</code>.</p>
12+
*
13+
* @throws UnsupportedOperationException if called when not supported
14+
*/
815
String getImagePath(int targetWidth, int targetHeight);
16+
17+
@Override
18+
default String getImagePath(int zoom) {
19+
throw new UnsupportedOperationException();
20+
};
21+
22+
static boolean canLoadAtTargetSize(String fileName, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
23+
return ImageDataLoader.canLoadAtTargetSize(fileName, srcWidth, srcHeight, targetWidth, targetHeight);
24+
}
925
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ public record ElementAtZoom<T>(T element, int zoom) {
274274
}
275275
}
276276

277-
public record ElementAtTargetSize<T>(T element, int targetWidth, int targetHeight) {
277+
public record ElementAtTargetSize<T>(T element, int srcWidth, int srcHeight) {
278278
public ElementAtTargetSize {
279279
if (element == null) {
280280
SWT.error(SWT.ERROR_NULL_ARGUMENT);
281281
}
282-
if (targetWidth <= 0 || targetHeight <= 0) {
282+
if (srcWidth <= 0 || srcHeight <= 0) {
283283
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
284284
}
285-
if (targetHeight <= 0 || targetHeight <= 0) {
285+
if (srcHeight <= 0 || srcHeight <= 0) {
286286
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
287287
}
288288
}
@@ -308,16 +308,22 @@ public static ElementAtZoom<ImageData> validateAndGetImageDataAtZoom(ImageDataPr
308308
return imageDataAtZoom;
309309
}
310310

311-
public static ElementAtTargetSize<ImageData> validateAndGetImageDataAtTargetSize(ImageDataProvider provider, int targetWidth, int targetHeight) {
311+
public static ElementAtTargetSize<ImageData> validateAndGetImageDataAtTargetSize(ImageDataProvider provider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
312312
if (provider == null) {
313313
SWT.error(SWT.ERROR_NULL_ARGUMENT);
314314
}
315-
ElementAtTargetSize<ImageData> imageDataAtTargetSize = getElementAtTargetSize((x, z) -> provider.getImageData(x, z), targetWidth, targetHeight);
316-
if (imageDataAtTargetSize == null) {
315+
if (provider instanceof SizeAwareImageDataProvider sizeAwareProvider) {
316+
ElementAtTargetSize<ImageData> imageDataAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImageData(x, z), srcWidth, srcHeight, targetWidth, targetHeight);
317+
if (imageDataAtTargetSize == null) {
318+
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
319+
": ImageDataProvider [" + provider + "] returns null ImageData at 100% zoom.");
320+
}
321+
return imageDataAtTargetSize;
322+
} else {
317323
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
318-
": ImageDataProvider [" + provider + "] returns null ImageData at 100% zoom.");
324+
": ImageDataProvider [" + provider + "] is not capable of loading image data at a specific target size.");
319325
}
320-
return imageDataAtTargetSize;
326+
return null;
321327
}
322328

323329
/**
@@ -340,16 +346,22 @@ public static ElementAtZoom<String> validateAndGetImagePathAtZoom(ImageFileNameP
340346
return imagePathAtZoom;
341347
}
342348

343-
public static ElementAtTargetSize<String> validateAndGetImagePathAtTargetSize(ImageFileNameProvider provider, int targetWidth, int targetHeight) {
349+
public static ElementAtTargetSize<String> validateAndGetImagePathAtTargetSize(ImageFileNameProvider provider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
344350
if (provider == null) {
345351
SWT.error(SWT.ERROR_NULL_ARGUMENT);
346352
}
347-
ElementAtTargetSize<String> imagePathAtTargetSize = getElementAtTargetSize((x, z) -> provider.getImagePath(x, z), targetWidth, targetHeight);
348-
if (imagePathAtTargetSize == null) {
353+
if (provider instanceof SizeAwareImageFileNameProvider sizeAwareProvider) {
354+
ElementAtTargetSize<String> imagePathAtTargetSize = getElementAtTargetSize((x, z) -> sizeAwareProvider.getImagePath(x, z), srcWidth, srcHeight, targetWidth, targetHeight);
355+
if (imagePathAtTargetSize == null) {
356+
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
357+
": ImageFileNameProvider [" + provider + "] returns null filename at 100% zoom.");
358+
}
359+
return imagePathAtTargetSize;
360+
} else {
349361
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null,
350-
": ImageFileNameProvider [" + provider + "] returns null filename at 100% zoom.");
362+
": ImageFileNameProvider [" + provider + "] is not capable of determining a image path for a specific target size.");
351363
}
352-
return imagePathAtTargetSize;
364+
return null;
353365
}
354366

355367
private static <T> ElementAtZoom<T> getElementAtZoom(Function<Integer, T> elementForZoomProvider, int zoom) {
@@ -378,27 +390,27 @@ private static <T> ElementAtZoom<T> getElementAtZoom(Function<Integer, T> elemen
378390
return null;
379391
}
380392

381-
private static <T> ElementAtTargetSize<T> getElementAtTargetSize(BiFunction<Integer, Integer, T> elementForZoomProvider, int targetWidth, int targetHeight) {
393+
private static <T> ElementAtTargetSize<T> getElementAtTargetSize(BiFunction<Integer, Integer, T> elementForZoomProvider, int srcWidth, int srcHeight, int targetWidth, int targetHeight) {
382394
T dataAtOriginalZoom = elementForZoomProvider.apply(targetWidth, targetHeight);
383395
if (dataAtOriginalZoom != null) {
384-
return new ElementAtTargetSize<>(dataAtOriginalZoom, targetWidth, targetHeight);
396+
return new ElementAtTargetSize<>(dataAtOriginalZoom, srcWidth, srcHeight);
385397
}
386398
if (targetWidth >= 16 && targetWidth <= 24) {
387399
T dataAt150Percent = elementForZoomProvider.apply(24, 24);
388400
if (dataAt150Percent != null) {
389-
return new ElementAtTargetSize<>(dataAt150Percent, 24, 24);
401+
return new ElementAtTargetSize<>(dataAt150Percent, srcWidth, srcHeight);
390402
}
391403
}
392404
if (targetWidth > 16) {
393405
T dataAt200Percent = elementForZoomProvider.apply(32, 32);
394406
if (dataAt200Percent != null) {
395-
return new ElementAtTargetSize<>(dataAt200Percent, 32, 32);
407+
return new ElementAtTargetSize<>(dataAt200Percent, srcWidth, srcHeight);
396408
}
397409
}
398410
if (targetWidth != 16) {
399411
T dataAt100Percent = elementForZoomProvider.apply(16, 16);
400412
if (dataAt100Percent != null) {
401-
return new ElementAtTargetSize<>(dataAt100Percent, 16, 16);
413+
return new ElementAtTargetSize<>(dataAt100Percent, srcWidth, srcHeight);
402414
}
403415
}
404416
return null;

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static boolean canLoadAtZoom(ElementAtZoom<InputStream> is, int targetZoo
172172
}
173173

174174
public static boolean canLoadAtTargetSize(ElementAtTargetSize<InputStream> is, int targetWidth, int targetHeight) {
175-
return (is.targetWidth() == targetWidth && is.targetHeight() == targetHeight) || isDynamicallySizableFormat(is.element());
175+
return (is.srcWidth() == targetWidth && is.srcHeight() == targetHeight) || isDynamicallySizableFormat(is.element());
176176
}
177177

178178
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ private void drawWithTempHandle(Image srcImage, int srcX, int srcY, int srcWidth
12661266
drawIcon(tempImageHandle, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple);
12671267
break;
12681268
}
1269-
}, destWidth, destHeight);
1269+
}, srcWidth, srcHeight, destWidth, destHeight);
12701270
}
12711271

12721272
private void drawIcon(long imageHandle, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {

0 commit comments

Comments
 (0)