Skip to content

Commit adee578

Browse files
committed
[Win32] Simplify and clean up Pattern
- Rename isDestroyed field to disposed (conventional Java boolean field naming) - Move null/disposed checks for gradient colors into the constructor where they belong, per the existing Javadoc contract - Use computeIfAbsent in getPatternHandle, removing the redundant newPatternHandle helper method - Extract colorRefToArgb helper to replace duplicated COLORREF->ARGB bit-manipulation in BasePatternHandle - Remove >> 0 no-op shifts in the midColor calculation - Use enhanced switch (arrow form) in PatternHandle.destroy() - Remove misleading public modifier from private inner class constructors
1 parent f51a25f commit adee578

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

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

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Pattern extends Resource {
4747

4848
private final Map<Integer, PatternHandle> zoomToHandle = new HashMap<>();
4949

50-
private boolean isDestroyed;
50+
private boolean disposed;
5151

5252
/**
5353
* Constructs a new Pattern given an image. Drawing with the resulting
@@ -167,6 +167,10 @@ public Pattern(Device device, float x1, float y1, float x2, float y2, Color colo
167167
*/
168168
public Pattern(Device device, float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) {
169169
super(device);
170+
if (color1 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
171+
if (color1.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
172+
if (color2 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
173+
if (color2.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
170174
this.baseX1 = x1;
171175
this.baseX2 = x2;
172176
this.baseY1 = y1;
@@ -180,18 +184,8 @@ public Pattern(Device device, float x1, float y1, float x2, float y2, Color colo
180184
this.device.registerResourceWithZoomSupport(this);
181185
}
182186

183-
private PatternHandle newPatternHandle(int zoom) {
184-
if (image != null) {
185-
return new ImagePatternHandle(zoom);
186-
}
187-
return new BasePatternHandle(zoom);
188-
}
189-
190187
private PatternHandle getPatternHandle(int zoom) {
191-
if (!zoomToHandle.containsKey(zoom)) {
192-
zoomToHandle.put(zoom, newPatternHandle(zoom));
193-
}
194-
return zoomToHandle.get(zoom);
188+
return zoomToHandle.computeIfAbsent(zoom, z -> image != null ? new ImagePatternHandle(z) : new BasePatternHandle(z));
195189
}
196190

197191
long getHandle(int zoom) {
@@ -203,7 +197,7 @@ void destroy() {
203197
device.deregisterResourceWithZoomSupport(this);
204198
zoomToHandle.values().forEach(PatternHandle::destroy);
205199
zoomToHandle.clear();
206-
this.isDestroyed = true;
200+
disposed = true;
207201
}
208202

209203
@Override
@@ -237,7 +231,7 @@ Pattern copy() {
237231
*/
238232
@Override
239233
public boolean isDisposed() {
240-
return isDestroyed;
234+
return disposed;
241235
}
242236

243237
/**
@@ -252,8 +246,16 @@ public String toString() {
252246
return "Pattern {" + zoomToHandle + "}";
253247
}
254248

249+
/**
250+
* Converts a Win32 COLORREF value (0x00BBGGRR) and an alpha byte into a
251+
* GDI+ ARGB color value (0xAARRGGBB).
252+
*/
253+
private static int colorRefToArgb(int colorRef, int alpha) {
254+
return ((alpha & 0xFF) << 24) | ((colorRef >> 16) & 0xFF) | (colorRef & 0xFF00) | ((colorRef & 0xFF) << 16);
255+
}
256+
255257
private class BasePatternHandle extends PatternHandle {
256-
public BasePatternHandle(int zoom) {
258+
BasePatternHandle(int zoom) {
257259
super(zoom);
258260
}
259261

@@ -264,19 +266,17 @@ long createHandle(int zoom) {
264266
float y1 = Win32DPIUtils.pointToPixel(baseY1, zoom);
265267
float x2 = Win32DPIUtils.pointToPixel(baseX2, zoom);
266268
float y2 = Win32DPIUtils.pointToPixel(baseY2, zoom);
267-
if (color1 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
268269
if (color1.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
269-
if (color2 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
270270
if (color2.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
271271
device.checkGDIP();
272272
int colorRef1 = color1.handle;
273-
int foreColor = ((alpha1 & 0xFF) << 24) | ((colorRef1 >> 16) & 0xFF) | (colorRef1 & 0xFF00) | ((colorRef1 & 0xFF) << 16);
273+
int foreColor = colorRefToArgb(colorRef1, alpha1);
274274
if (x1 == x2 && y1 == y2) {
275275
handle = Gdip.SolidBrush_new(foreColor);
276276
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
277277
} else {
278278
int colorRef2 = color2.handle;
279-
int backColor = ((alpha2 & 0xFF) << 24) | ((colorRef2 >> 16) & 0xFF) | (colorRef2 & 0xFF00) | ((colorRef2 & 0xFF) << 16);
279+
int backColor = colorRefToArgb(colorRef2, alpha2);
280280
PointF p1 = new PointF();
281281
p1.X = x1;
282282
p1.Y = y1;
@@ -287,7 +287,7 @@ long createHandle(int zoom) {
287287
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
288288
if (alpha1 != 0xFF || alpha2 != 0xFF) {
289289
int a = (int)((alpha1 & 0xFF) * 0.5f + (alpha2 & 0xFF) * 0.5f);
290-
int r = (int)(((colorRef1 & 0xFF) >> 0) * 0.5f + ((colorRef2 & 0xFF) >> 0) * 0.5f);
290+
int r = (int)((colorRef1 & 0xFF) * 0.5f + (colorRef2 & 0xFF) * 0.5f);
291291
int g = (int)(((colorRef1 & 0xFF00) >> 8) * 0.5f + ((colorRef2 & 0xFF00) >> 8) * 0.5f);
292292
int b = (int)(((colorRef1 & 0xFF0000) >> 16) * 0.5f + ((colorRef2 & 0xFF0000) >> 16) * 0.5f);
293293
int midColor = a << 24 | r << 16 | g << 8 | b;
@@ -301,7 +301,7 @@ long createHandle(int zoom) {
301301
private class ImagePatternHandle extends PatternHandle {
302302
private Image.GdipImage gdipImage;
303303

304-
public ImagePatternHandle(int zoom) {
304+
ImagePatternHandle(int zoom) {
305305
super(zoom);
306306
}
307307

@@ -336,27 +336,18 @@ private void cleanupBitmap() {
336336
private abstract class PatternHandle {
337337
private final long handle;
338338

339-
public PatternHandle(int zoom) {
339+
PatternHandle(int zoom) {
340340
this.handle = createHandle(zoom);
341341
}
342342

343343
abstract long createHandle(int zoom);
344344

345345
protected void destroy() {
346-
int type = Gdip.Brush_GetType(handle);
347-
switch (type) {
348-
case Gdip.BrushTypeSolidColor:
349-
Gdip.SolidBrush_delete(handle);
350-
break;
351-
case Gdip.BrushTypeHatchFill:
352-
Gdip.HatchBrush_delete(handle);
353-
break;
354-
case Gdip.BrushTypeLinearGradient:
355-
Gdip.LinearGradientBrush_delete(handle);
356-
break;
357-
case Gdip.BrushTypeTextureFill:
358-
Gdip.TextureBrush_delete(handle);
359-
break;
346+
switch (Gdip.Brush_GetType(handle)) {
347+
case Gdip.BrushTypeSolidColor -> Gdip.SolidBrush_delete(handle);
348+
case Gdip.BrushTypeHatchFill -> Gdip.HatchBrush_delete(handle);
349+
case Gdip.BrushTypeLinearGradient -> Gdip.LinearGradientBrush_delete(handle);
350+
case Gdip.BrushTypeTextureFill -> Gdip.TextureBrush_delete(handle);
360351
}
361352
}
362353
}

0 commit comments

Comments
 (0)