Skip to content

Commit 5756e3d

Browse files
committed
Rework new Image constructors accepting a ImageGcDrawer
When implementing the ImageGCDrawer as lambda, the resulting code is better to read if the ImageGCDrawer is passed as last argument: ''' new Image(device, 16, 16, (gc, w, h) -> { gc.draw ... }) ''' Furthermore this way the argument order also better reflects the order in which the arguments are applied: 1. an image with the given size is created. 2. the GC draws on that image Follow-up on - #1734
1 parent 3670670 commit 5756e3d

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/CTabFolder.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -734,16 +734,15 @@ Image createButtonImage(Display display, int button) {
734734
final Rectangle trim = renderer.computeTrim(button, SWT.NONE, 0, 0, 0, 0);
735735
final Point imageSize = new Point(size.x - trim.width, size.y - trim.height);
736736
Color transColor = renderer.parent.getBackground();
737-
final ImageGcDrawer imageGcDrawer = new TransparencyColorImageGcDrawer(transColor) {
737+
return new Image(display, imageSize.x, imageSize.y, new TransparencyColorImageGcDrawer(transColor) {
738738
@Override
739739
public void drawOn(GC gc, int imageWidth, int imageHeight) {
740740
Rectangle imageBounds = new Rectangle(0, 0, imageWidth, imageHeight);
741741
gc.setBackground(transColor);
742742
gc.fillRectangle(imageBounds);
743743
renderer.draw(button, SWT.NONE, imageBounds, gc);
744744
}
745-
};
746-
return new Image(display, imageGcDrawer, imageSize.x, imageSize.y);
745+
});
747746
}
748747

749748
private void notifyItemCountChange() {
@@ -4007,7 +4006,8 @@ void updateBkImages(boolean colorChanged) {
40074006
if (colorChanged || !bounds.equals(bkImageBounds[i])) {
40084007
bkImageBounds[i] = bounds;
40094008
if (controlBkImages[i] != null) controlBkImages[i].dispose();
4010-
controlBkImages[i] = new Image(control.getDisplay(), (gc, imageWidth, imageHeight) -> renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc), bounds.width, bounds.height);
4009+
controlBkImages[i] = new Image(control.getDisplay(), bounds.width, bounds.height,
4010+
(gc, w, h) -> renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc));
40114011
control.setBackground(null);
40124012
control.setBackgroundImage(controlBkImages[i]);
40134013
}

bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1593,32 +1593,31 @@ void createCaretBitmaps() {
15931593
leftCaretBitmap.dispose();
15941594
}
15951595
int lineHeight = renderer.getLineHeight();
1596-
final ImageGcDrawer leftCaretDrawer = (gc, width, height) -> {
1596+
leftCaretBitmap = new Image(display, caretWidth, lineHeight, (gc, width, height) -> {
15971597
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
15981598
gc.fillRectangle(0, 0, width, height);
15991599
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
16001600
gc.drawLine(0,0,0,height);
16011601
gc.drawLine(0,0,width-1,0);
16021602
gc.drawLine(0,1,1,1);
1603-
};
1604-
leftCaretBitmap = new Image(display, leftCaretDrawer, caretWidth, lineHeight);
1603+
});
16051604

16061605
if (rightCaretBitmap != null) {
16071606
if (defaultCaret != null && rightCaretBitmap.equals(defaultCaret.getImage())) {
16081607
defaultCaret.setImage(null);
16091608
}
16101609
rightCaretBitmap.dispose();
16111610
}
1612-
final ImageGcDrawer rightCaretDrawer = (gc, width, height) -> {
1611+
rightCaretBitmap = new Image(display, caretWidth, lineHeight, (gc, width, height) -> {
16131612
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
16141613
gc.fillRectangle(0, 0, width, height);
16151614
gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
16161615
gc.drawLine(width-1,0,width-1,height);
16171616
gc.drawLine(0,0,width-1,0);
16181617
gc.drawLine(width-1,1,1,1);
1619-
};
1620-
rightCaretBitmap = new Image(display, rightCaretDrawer, caretWidth, lineHeight);
1618+
});
16211619
}
1620+
16221621
/**
16231622
* Moves the selected text to the clipboard. The text will be put in the
16241623
* clipboard in plain text, HTML, and RTF formats.

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,9 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
878878
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
879879
* <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
880880
* </ul>
881-
* @since 3.129
881+
* @since 3.130
882882
*/
883-
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
883+
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
884884
super(device);
885885
if (imageGcDrawer == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
886886
this.imageGcDrawer = imageGcDrawer;
@@ -905,6 +905,15 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
905905
}
906906
}
907907

908+
/**
909+
* @since 3.129
910+
* @deprecated Instead use {@link #Image(Device, int, int, ImageGcDrawer)}
911+
*/
912+
@Deprecated(forRemoval = true, since = "2025-06 (removal in 2027-06 or later)")
913+
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
914+
this(device, width, height, imageGcDrawer);
915+
}
916+
908917
private ImageData drawWithImageGcDrawer(ImageGcDrawer imageGcDrawer, int width, int height, int zoom) {
909918
Image image = new Image(device, width, height);
910919
GC gc = new GC(image);

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,9 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
670670
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
671671
* <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
672672
* </ul>
673-
* @since 3.129
673+
* @since 3.130
674674
*/
675-
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
675+
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
676676
super(device);
677677
if (imageGcDrawer == null) {
678678
SWT.error(SWT.ERROR_NULL_ARGUMENT);
@@ -684,6 +684,15 @@ public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height)
684684
init ();
685685
}
686686

687+
/**
688+
* @since 3.129
689+
* @deprecated Instead use {@link #Image(Device, int, int, ImageGcDrawer)}
690+
*/
691+
@Deprecated(forRemoval = true, since = "2025-06 (removal in 2027-06 or later)")
692+
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
693+
this(device, width, height, imageGcDrawer);
694+
}
695+
687696
/**
688697
* Refreshes the image for the current device scale factor.
689698
* <p>

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,24 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
629629
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
630630
* <li>ERROR_NULL_ARGUMENT - if the ImageGcDrawer is null</li>
631631
* </ul>
632-
* @since 3.129
632+
* @since 3.130
633633
*/
634-
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
634+
public Image(Device device, int width, int height, ImageGcDrawer imageGcDrawer) {
635635
super(device);
636636
this.imageProvider = new ImageGcDrawerWrapper(imageGcDrawer, width, height);
637637
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
638638
init();
639639
}
640640

641+
/**
642+
* @since 3.129
643+
* @deprecated Instead use {@link #Image(Device, int, int, ImageGcDrawer)}
644+
*/
645+
@Deprecated(forRemoval = true, since = "2025-06 (removal in 2027-06 or later)")
646+
public Image(Device device, ImageGcDrawer imageGcDrawer, int width, int height) {
647+
this(device, width, height, imageGcDrawer);
648+
}
649+
641650
private ImageData adaptImageDataIfDisabledOrGray(ImageData data) {
642651
ImageData returnImageData = null;
643652
switch (this.styleFlag) {

0 commit comments

Comments
 (0)