Skip to content

Commit

Permalink
introduce GC.create which returns an AutoCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-melcher committed Mar 28, 2024
1 parent b27a84d commit 37519e2
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ public synchronized void start() {
final Runnable [] timer = new Runnable [1];
timer [0] = () -> {
if (!active) return;
GC.drawOn(AnimatedProgress.this, gc -> paintStripes(gc));
try(var gc = GC.create(AnimatedProgress.this)) {
paintStripes(gc);
}
display.timerExec (SLEEP, timer [0]);
};
display.timerExec (SLEEP, timer [0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private Point getTotalSize(Image image, String text) {
size.y += r.height;
}

GC.drawOn(this, gc -> {
try(var gc = GC.create(this)) {
if (text != null && text.length() > 0) {
Point e = gc.textExtent(text, DRAW_FLAGS);
size.x += e.x;
Expand All @@ -279,7 +279,7 @@ private Point getTotalSize(Image image, String text) {
} else {
size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
}
});
}

return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3835,8 +3835,8 @@ boolean updateItems() {
}

boolean updateItems (int showIndex) {
final boolean changed[] = new boolean[] {false};
GC.drawOn(this, gc -> {
boolean changed = false;
try(var gc = GC.create(this)) {
if (!single && !mru && showIndex != -1) {
// make sure selected item will be showing
int firstIndex = showIndex;
Expand Down Expand Up @@ -3899,31 +3899,31 @@ boolean updateItems (int showIndex) {
}

boolean oldShowChevron = showChevron;
changed[0] = setItemSize(gc);
changed = setItemSize(gc);
updateButtons();
boolean chevronChanged = showChevron != oldShowChevron;
if (chevronChanged) {
if (updateTabHeight(false)) {
// Tab height has changed. Item sizes have to be set again.
changed[0] |= setItemSize(gc);
changed |= setItemSize(gc);
}
}
changed[0] |= setItemLocation(gc);
changed |= setItemLocation(gc);
setButtonBounds();
changed[0] |= chevronChanged;
if (changed[0] && getToolTipText() != null) {
changed |= chevronChanged;
if (changed && getToolTipText() != null) {
Point pt = getDisplay().getCursorLocation();
pt = toControl(pt);
_setToolTipText(pt.x, pt.y);
}
});
return changed[0];
}
return changed;
}
boolean updateTabHeight(boolean force){
int oldHeight = tabHeight;
GC.drawOn(this, gc ->{
try(var gc = GC.create(this)) {
tabHeight = renderer.computeSize(CTabFolderRenderer.PART_HEADER, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT).y;
});
}
if (fixedTabHeight == SWT.DEFAULT && controls != null && controls.length > 0) {
for (int i = 0; i < controls.length; i++) {
if ((controlAlignments[i] & SWT.WRAP) == 0 && !controls[i].isDisposed() && controls[i].getVisible()) {
Expand Down Expand Up @@ -4009,7 +4009,9 @@ void updateBkImages(boolean colorChanged) {
bkImageBounds[i] = bounds;
if (controlBkImages[i] != null) controlBkImages[i].dispose();
controlBkImages[i] = new Image(control.getDisplay(), bounds);
GC.drawOn(controlBkImages[i], gc-> renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc));
try(var gc = GC.create(controlBkImages[i])) {
renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc);
}
control.setBackground(null);
control.setBackgroundImage(controlBkImages[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
CTabItem[] items = folder.items;
CTabFolderRenderer renderer = folder.renderer;
// preferred width of tab area to show all tabs
final int[] tabW = new int[] {0};
final int[] selectedIndex = new int[] {folder.selectedIndex};
if (selectedIndex[0] == -1) selectedIndex[0] = 0;
final int[] wrapHeight = new int[] {0};
GC.drawOn(folder, gc -> {
int tabW = 0;
int selectedIndex = folder.selectedIndex;
if (selectedIndex == -1) selectedIndex = 0;
int width = 0, wrapHeight = 0;
try(var gc = GC.create(folder)) {
for (int i = 0; i < items.length; i++) {
if (folder.single) {
tabW[0] = Math.max(tabW[0], renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
tabW = Math.max(tabW, renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
} else {
int state = 0;
if (i == selectedIndex[0]) state |= SWT.SELECTED;
tabW[0] += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
if (i == selectedIndex) state |= SWT.SELECTED;
tabW += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
}
}

int width = 0;
boolean leftControl = false, rightControl = false;
if (wHint == SWT.DEFAULT) {
for (int i = 0; i < folder.controls.length; i++) {
Expand All @@ -67,7 +66,7 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
if (positions[0][i]) {
minY = Math.min(minY, rects[i].y);
maxY = Math.max(maxY, rects[i].y + rects[i].height);
wrapHeight[0] = maxY - minY;
wrapHeight = maxY - minY;
} else {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
Expand All @@ -80,8 +79,8 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
}
if (leftControl) width += CTabFolder.SPACING * 2;
if (rightControl) width += CTabFolder.SPACING * 2;
tabW[0] += width;
});
tabW += width;
}

int controlW = 0;
int controlH = 0;
Expand All @@ -95,8 +94,8 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
}
}

int minWidth = Math.max(tabW[0], controlW + folder.marginWidth);
int minHeight = (folder.minimized) ? 0 : controlH + wrapHeight[0];
int minWidth = Math.max(tabW, controlW + folder.marginWidth);
int minHeight = (folder.minimized) ? 0 : controlH + wrapHeight;
if (minWidth == 0) minWidth = CTabFolder.DEFAULT_WIDTH;
if (minHeight == 0) minHeight = CTabFolder.DEFAULT_HEIGHT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ public void dragOver(DropTargetEvent event) {
pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) {
if (System.currentTimeMillis() >= scrollBeginTime) {
Rectangle area = text.getClientArea();
GC.drawOn(text, gc->{
try(var gc = GC.create(text)) {
FontMetrics fm = gc.getFontMetrics();

double charWidth = fm.getAverageCharacterWidth();
int scrollAmount = (int) (10*charWidth);
if (pt.x < area.x + 3*charWidth) {
Expand All @@ -173,18 +174,18 @@ public void dragOver(DropTargetEvent event) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel + scrollAmount);
}
});
int lineHeight = text.getLineHeight();
if (pt.y < area.y + lineHeight) {
int topPixel = text.getTopPixel();
text.setTopPixel(topPixel - lineHeight);
}
if (pt.y > area.height - lineHeight) {
int topPixel = text.getTopPixel();
text.setTopPixel(topPixel + lineHeight);
int lineHeight = text.getLineHeight();
if (pt.y < area.y + lineHeight) {
int topPixel = text.getTopPixel();
text.setTopPixel(topPixel - lineHeight);
}
if (pt.y > area.height - lineHeight) {
int topPixel = text.getTopPixel();
text.setTopPixel(topPixel + lineHeight);
}
scrollBeginTime = 0;
scrollX = scrollY = -1;
}
scrollBeginTime = 0;
scrollX = scrollY = -1;
}
} else {
scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1500,10 +1500,10 @@ void setFont(Font font, int tabs) {
tabWidth = layout.getBounds().width;
layout.dispose();
if (styledText != null) {
GC.drawOn(styledText, gc->{
try(var gc = GC.create(styledText)) {
averageCharWidth = (int) gc.getFontMetrics().getAverageCharacterWidth();
fixedPitch = gc.stringExtent("l").x == gc.stringExtent("W").x; //$NON-NLS-1$ //$NON-NLS-2$
});
}
}
}
void setLineAlignment(int startLine, int count, int alignment) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@
* @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: GraphicsExample, PaintExample</a>
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/
public final class GC extends Resource {
public sealed class GC extends Resource permits GC.Closeable {

public static final class Closeable extends GC implements AutoCloseable {
Closeable(Drawable drawable, int style) {
super(drawable, style);
}
@Override
public void close() {
dispose();
}
}

public static GC.Closeable create(Drawable drawable) {
return new Closeable(drawable, SWT.NONE);
}

/**
* the handle to the OS device context
Expand Down Expand Up @@ -114,8 +128,7 @@ public final class GC extends Resource {
* foreground color, background color and font in the GC
* to match those in the drawable.
* <p>
* You must dispose the graphics context when it is no longer required
* or better use {@link #drawOn(Drawable, GCRunnable)} which takes care of disposing the GC.
* You must dispose the graphics context when it is no longer required.
* </p>
* @param drawable the drawable to draw on
* @exception IllegalArgumentException <ul>
Expand Down Expand Up @@ -178,22 +191,6 @@ public GC(Drawable drawable, int style) {
init();
}

/**
* creates a GC on the Drawable, calls run on the GCRunnable and disposes the GC in a finally block
* @param <E>
* @param drawable
* @param runnable
* @throws E
*/
public static <E extends Exception> void drawOn(Drawable drawable, GCRunnable<E> runnable) throws E {
GC gc = new GC(drawable);
try {
runnable.run(gc);
} finally {
gc.dispose();
}
}

static int checkStyle(int style) {
if ((style & SWT.LEFT_TO_RIGHT) != 0) style &= ~SWT.RIGHT_TO_LEFT;
return style & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
Expand Down

0 comments on commit 37519e2

Please sign in to comment.