Skip to content

Commit 188aed6

Browse files
HeikoKlareCopilot
andcommitted
Refactor Transform nested types to remove inner class boilerplate
Convert SetElementsOperation and TranslateOperation from non-static inner classes to records, making their dependency on the device explicit via a constructor parameter rather than capturing the outer instance implicitly. Make MultiplyOperation a static nested class for the same reason. Also extract a destroyAllHandles() helper to eliminate duplicated code, simplify getTransformHandle() with Map.computeIfAbsent(), collapse a single-expression block lambda in isIdentity(), and remove unnecessary local handle aliases inside the operation apply() methods. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 07605c9 commit 188aed6

File tree

1 file changed

+29
-62
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+29
-62
lines changed

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

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public Transform(Device device, float[] elements) {
142142
public Transform (Device device, float m11, float m12, float m21, float m22, float dx, float dy) {
143143
super(device);
144144
this.device.checkGDIP();
145-
storeAndApplyOperationForAllHandles(new SetElementsOperation(m11, m12, m21, m22, dx, dy));
145+
storeAndApplyOperationForAllHandles(new SetElementsOperation(getDevice(), m11, m12, m21, m22, dx, dy));
146146
init();
147147
this.device.registerResourceWithZoomSupport(this);
148148
}
@@ -156,14 +156,17 @@ static float[] checkTransform(float[] elements) {
156156
@Override
157157
void destroy() {
158158
device.deregisterResourceWithZoomSupport(this);
159-
zoomToHandle.values().forEach(TransformHandle::destroy);
160-
zoomToHandle.clear();
159+
destroyAllHandles();
161160
this.isDestroyed = true;
162161
}
163162

164163
@Override
165164
void destroyHandlesExcept(Set<Integer> zoomLevels) {
166165
// As long as we keep the operations, we can cleanup all handles
166+
destroyAllHandles();
167+
}
168+
169+
private void destroyAllHandles() {
167170
zoomToHandle.values().forEach(TransformHandle::destroy);
168171
zoomToHandle.clear();
169172
}
@@ -210,7 +213,7 @@ public void identity() {
210213
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
211214
// identity invalidates all previous operations, so we remove them
212215
operations.clear();
213-
storeAndApplyOperationForAllHandles(new SetElementsOperation(1, 0, 0, 1, 0, 0));
216+
storeAndApplyOperationForAllHandles(new SetElementsOperation(getDevice(), 1, 0, 0, 1, 0, 0));
214217
}
215218

216219
/**
@@ -250,9 +253,7 @@ public boolean isDisposed() {
250253
*/
251254
public boolean isIdentity() {
252255
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
253-
return applyUsingAnyHandle(transformHandle -> {
254-
return Gdip.Matrix_IsIdentity(transformHandle.handle);
255-
});
256+
return applyUsingAnyHandle(transformHandle -> Gdip.Matrix_IsIdentity(transformHandle.handle));
256257
}
257258

258259
/**
@@ -330,7 +331,7 @@ public void setElements(float m11, float m12, float m21, float m22, float dx, fl
330331
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
331332
// setElements invalidates all previous operations, so we remove them
332333
operations.clear();
333-
storeAndApplyOperationForAllHandles(new SetElementsOperation(m11, m12, m21, m22, dx, dy));
334+
storeAndApplyOperationForAllHandles(new SetElementsOperation(getDevice(), m11, m12, m21, m22, dx, dy));
334335
}
335336

336337
/**
@@ -396,7 +397,7 @@ public void transform(float[] pointArray) {
396397
*/
397398
public void translate(float offsetX, float offsetY) {
398399
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
399-
storeAndApplyOperationForAllHandles(new TranslateOperation(offsetX, offsetY));
400+
storeAndApplyOperationForAllHandles(new TranslateOperation(getDevice(), offsetX, offsetY));
400401
}
401402

402403
private record TransformHandle(long handle, int zoom) {
@@ -408,12 +409,11 @@ void destroy() {
408409
private record InvertOperation() implements Operation {
409410
@Override
410411
public void apply(TransformHandle transformHandle) {
411-
long handle = transformHandle.handle;
412-
if (Gdip.Matrix_Invert(handle) != 0) SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
412+
if (Gdip.Matrix_Invert(transformHandle.handle) != 0) SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
413413
}
414414
}
415415

416-
private class MultiplyOperation implements Operation {
416+
private static class MultiplyOperation implements Operation {
417417
private final float[] elements;
418418

419419
public MultiplyOperation(Transform matrix) {
@@ -441,68 +441,40 @@ public void apply(TransformHandle transformHandle) {
441441
private record RotateOperation(float angle) implements Operation {
442442
@Override
443443
public void apply(TransformHandle transformHandle) {
444-
long handle = transformHandle.handle;
445-
Gdip.Matrix_Rotate(handle, angle, Gdip.MatrixOrderPrepend);
444+
Gdip.Matrix_Rotate(transformHandle.handle, angle, Gdip.MatrixOrderPrepend);
446445
}
447446
}
448447

449448
private record ScaleOperation(float scaleX, float scaleY) implements Operation {
450449
@Override
451450
public void apply(TransformHandle transformHandle) {
452-
long handle = transformHandle.handle;
453-
Gdip.Matrix_Scale(handle, scaleX, scaleY, Gdip.MatrixOrderPrepend);
451+
Gdip.Matrix_Scale(transformHandle.handle, scaleX, scaleY, Gdip.MatrixOrderPrepend);
454452
}
455453
}
456454

457-
private class SetElementsOperation implements Operation {
458-
private final float m11;
459-
private final float m12;
460-
private final float m21;
461-
private final float m22;
462-
private final float dx;
463-
private final float dy;
464-
465-
public SetElementsOperation(float m11, float m12, float m21, float m22, float dx, float dy) {
466-
this.m11 = m11;
467-
this.m12 = m12;
468-
this.m21 = m21;
469-
this.m22 = m22;
470-
this.dx = dx;
471-
this.dy = dy;
472-
}
473-
455+
private record SetElementsOperation(Drawable device, float m11, float m12, float m21, float m22, float dx, float dy) implements Operation {
474456
@Override
475457
public void apply(TransformHandle transformHandle) {
476-
Drawable drawable = getDevice();
477-
long handle = transformHandle.handle;
478-
int zoom = transformHandle.zoom;
479-
Gdip.Matrix_SetElements(handle, m11, m12, m21, m22, Win32DPIUtils.pointToPixel(drawable, dx, zoom), Win32DPIUtils.pointToPixel(drawable, dy, zoom));
458+
Gdip.Matrix_SetElements(transformHandle.handle, m11, m12, m21, m22,
459+
Win32DPIUtils.pointToPixel(device, dx, transformHandle.zoom),
460+
Win32DPIUtils.pointToPixel(device, dy, transformHandle.zoom));
480461
}
481462
}
482463

483464
private record ShearOperation(float shearX, float shearY) implements Operation {
484465
@Override
485466
public void apply(TransformHandle transformHandle) {
486-
long handle = transformHandle.handle;
487-
Gdip.Matrix_Shear(handle, shearX, shearY, Gdip.MatrixOrderPrepend);
467+
Gdip.Matrix_Shear(transformHandle.handle, shearX, shearY, Gdip.MatrixOrderPrepend);
488468
}
489469
}
490470

491-
private class TranslateOperation implements Operation {
492-
private final float offsetX;
493-
private final float offsetY;
494-
495-
public TranslateOperation(float offsetX, float offsetY) {
496-
this.offsetX = offsetX;
497-
this.offsetY = offsetY;
498-
}
499-
471+
private record TranslateOperation(Drawable device, float offsetX, float offsetY) implements Operation {
500472
@Override
501473
public void apply(TransformHandle transformHandle) {
502-
Drawable drawable = getDevice();
503-
long handle = transformHandle.handle;
504-
int zoom = transformHandle.zoom;
505-
Gdip.Matrix_Translate(handle, Win32DPIUtils.pointToPixel(drawable, offsetX, zoom), Win32DPIUtils.pointToPixel(drawable, offsetY, zoom), Gdip.MatrixOrderPrepend);
474+
Gdip.Matrix_Translate(transformHandle.handle,
475+
Win32DPIUtils.pointToPixel(device, offsetX, transformHandle.zoom),
476+
Win32DPIUtils.pointToPixel(device, offsetY, transformHandle.zoom),
477+
Gdip.MatrixOrderPrepend);
506478
}
507479
}
508480

@@ -545,20 +517,15 @@ public String toString() {
545517
private TransformHandle newTransformHandle(int zoom) {
546518
long newHandle = Gdip.Matrix_new(0, 0, 0, 0, 0, 0);
547519
if (newHandle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
548-
TransformHandle newTransformHandle = new TransformHandle(newHandle, zoom);
549-
for(Operation operation : operations) {
550-
operation.apply(newTransformHandle);
520+
TransformHandle transformHandle = new TransformHandle(newHandle, zoom);
521+
for (Operation operation : operations) {
522+
operation.apply(transformHandle);
551523
}
552-
return newTransformHandle;
524+
return transformHandle;
553525
}
554526

555527
private TransformHandle getTransformHandle(int zoom) {
556-
if (!zoomToHandle.containsKey(zoom)) {
557-
TransformHandle newHandle = newTransformHandle(zoom);
558-
zoomToHandle.put(zoom, newHandle);
559-
return newHandle;
560-
}
561-
return zoomToHandle.get(zoom);
528+
return zoomToHandle.computeIfAbsent(zoom, this::newTransformHandle);
562529
}
563530

564531
long getHandle(int zoom) {

0 commit comments

Comments
 (0)