Skip to content

Commit

Permalink
introduce GC.Closeable also in cocoa and gtk
Browse files Browse the repository at this point in the history
- format the changed coding parts
- remove the GC.create calls at locations where the diff gets too large
  • Loading branch information
tobias-melcher authored and HannesWell committed Mar 29, 2024
1 parent e3a6ef5 commit 573159d
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public synchronized void start() {
final Runnable [] timer = new Runnable [1];
timer [0] = () -> {
if (!active) return;
try(var gc = GC.create(AnimatedProgress.this)) {
try (var gc = GC.create(AnimatedProgress.this)) {
paintStripes(gc);
}
display.timerExec (SLEEP, timer [0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,13 @@ private Point getTotalSize(Image image, String text) {
size.y += r.height;
}

try(var gc = GC.create(this)) {
try (var gc = GC.create(this)) {
if (text != null && text.length() > 0) {
Point e = gc.textExtent(text, DRAW_FLAGS);
size.x += e.x;
size.y = Math.max(size.y, e.y);
if (image != null) size.x += GAP;
if (image != null)
size.x += GAP;
} else {
size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3829,93 +3829,92 @@ boolean updateItems() {
}

boolean updateItems (int showIndex) {
boolean changed = false;
try(var gc = GC.create(this)) {
if (!single && !mru && showIndex != -1) {
// make sure selected item will be showing
int firstIndex = showIndex;
if (priority[0] < showIndex) {
int maxWidth = getRightItemEdge(gc) - getLeftItemEdge(gc, CTabFolderRenderer.PART_BORDER);
int width = 0;
int[] widths = new int[items.length];
for (int i = priority[0]; i <= showIndex; i++) {
GC gc = new GC(this);
if (!single && !mru && showIndex != -1) {
// make sure selected item will be showing
int firstIndex = showIndex;
if (priority[0] < showIndex) {
int maxWidth = getRightItemEdge(gc) - getLeftItemEdge(gc, CTabFolderRenderer.PART_BORDER);
int width = 0;
int[] widths = new int[items.length];
for (int i = priority[0]; i <= showIndex; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
}
if (width > maxWidth) {
width = 0;
for (int i = showIndex; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
} else {
firstIndex = priority[0];
for (int i = showIndex + 1; i < items.length; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width >= maxWidth) break;
}
if (width > maxWidth) {
width = 0;
for (int i = showIndex; i >= 0; i--) {
if (width < maxWidth) {
for (int i = priority[0] - 1; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
} else {
firstIndex = priority[0];
for (int i = showIndex + 1; i < items.length; i++) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width >= maxWidth) break;
}
if (width < maxWidth) {
for (int i = priority[0] - 1; i >= 0; i--) {
int state = CTabFolderRenderer.MINIMUM_SIZE;
if (i == selectedIndex) state |= SWT.SELECTED;
if (widths[i] == 0) widths[i] = renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
width += widths[i];
if (width > maxWidth) break;
firstIndex = i;
}
}
}
}

}
if (firstIndex != priority[0]) {
int index = 0;
// enumerate tabs from first visible to the last existing one (sorted ascending)
for (int i = firstIndex; i < items.length; i++) {
priority[index++] = i;
}
if (firstIndex != priority[0]) {
int index = 0;
// enumerate tabs from first visible to the last existing one (sorted ascending)
for (int i = firstIndex; i < items.length; i++) {
priority[index++] = i;
}
// enumerate hidden tabs on the left hand from first visible one
// in the inverse order (sorted descending) so that the originally
// first opened tab is always at the end of the list
for (int i = firstIndex - 1; i >= 0; i--) {
priority[index++] = i;
}
// enumerate hidden tabs on the left hand from first visible one
// in the inverse order (sorted descending) so that the originally
// first opened tab is always at the end of the list
for (int i = firstIndex - 1; i >= 0; i--) {
priority[index++] = i;
}
}
}

boolean oldShowChevron = showChevron;
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 |= setItemSize(gc);
}
}
changed |= setItemLocation(gc);
setButtonBounds();
changed |= chevronChanged;
if (changed && getToolTipText() != null) {
Point pt = getDisplay().getCursorLocation();
pt = toControl(pt);
_setToolTipText(pt.x, pt.y);
boolean oldShowChevron = showChevron;
boolean 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 |= setItemSize(gc);
}
}
changed |= setItemLocation(gc);
setButtonBounds();
changed |= chevronChanged;
if (changed && getToolTipText() != null) {
Point pt = getDisplay().getCursorLocation();
pt = toControl(pt);
_setToolTipText(pt.x, pt.y);
}
gc.dispose();
return changed;
}
boolean updateTabHeight(boolean force){
int oldHeight = tabHeight;
try(var gc = GC.create(this)) {
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) {
Expand Down Expand Up @@ -4003,7 +4002,7 @@ void updateBkImages(boolean colorChanged) {
bkImageBounds[i] = bounds;
if (controlBkImages[i] != null) controlBkImages[i].dispose();
controlBkImages[i] = new Image(control.getDisplay(), bounds);
try(var gc = GC.create(controlBkImages[i])) {
try (var gc = GC.create(controlBkImages[i])) {
renderer.draw(CTabFolderRenderer.PART_BACKGROUND, 0, bounds, gc);
}
control.setBackground(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,56 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f
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 = Math.max(tabW, renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
} else {
int state = 0;
if (i == selectedIndex) state |= SWT.SELECTED;
tabW += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
}
GC gc = new GC(folder);
for (int i = 0; i < items.length; i++) {
if (folder.single) {
tabW = Math.max(tabW, renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x);
} else {
int state = 0;
if (i == selectedIndex) state |= SWT.SELECTED;
tabW += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x;
}
}

boolean leftControl = false, rightControl = false;
if (wHint == SWT.DEFAULT) {
for (int i = 0; i < folder.controls.length; i++) {
Control control = folder.controls[i];
if (!control.isDisposed() && control.getVisible()) {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
}
width += control.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
int width = 0, wrapHeight = 0;
boolean leftControl = false, rightControl = false;
if (wHint == SWT.DEFAULT) {
for (int i = 0; i < folder.controls.length; i++) {
Control control = folder.controls[i];
if (!control.isDisposed() && control.getVisible()) {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
}
width += control.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
}
} else {
Point size = new Point (wHint, hHint);
boolean[][] positions = new boolean[1][];
Rectangle[] rects = folder.computeControlBounds(size, positions);
int minY = Integer.MAX_VALUE, maxY = 0;
for (int i = 0; i < rects.length; i++) {
if (positions[0][i]) {
minY = Math.min(minY, rects[i].y);
maxY = Math.max(maxY, rects[i].y + rects[i].height);
wrapHeight = maxY - minY;
}
} else {
Point size = new Point (wHint, hHint);
boolean[][] positions = new boolean[1][];
Rectangle[] rects = folder.computeControlBounds(size, positions);
int minY = Integer.MAX_VALUE, maxY = 0;
for (int i = 0; i < rects.length; i++) {
if (positions[0][i]) {
minY = Math.min(minY, rects[i].y);
maxY = Math.max(maxY, rects[i].y + rects[i].height);
wrapHeight = maxY - minY;
} else {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
if ((folder.controlAlignments[i] & SWT.LEAD) != 0) {
leftControl = true;
} else {
rightControl = true;
}
width += rects[i].width;
rightControl = true;
}
width += rects[i].width;
}
}
if (leftControl) width += CTabFolder.SPACING * 2;
if (rightControl) width += CTabFolder.SPACING * 2;
tabW += width;
}
if (leftControl) width += CTabFolder.SPACING * 2;
if (rightControl) width += CTabFolder.SPACING * 2;
tabW += width;

gc.dispose();

int controlW = 0;
int controlH = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,30 @@ public void dragOver(DropTargetEvent event) {
pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) {
if (System.currentTimeMillis() >= scrollBeginTime) {
Rectangle area = text.getClientArea();
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) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel - scrollAmount);
}
if (pt.x > area.width - 3*charWidth) {
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);
}
scrollBeginTime = 0;
scrollX = scrollY = -1;
GC gc = new GC(text);
FontMetrics fm = gc.getFontMetrics();
gc.dispose();
double charWidth = fm.getAverageCharacterWidth();
int scrollAmount = (int) (10*charWidth);
if (pt.x < area.x + 3*charWidth) {
int leftPixel = text.getHorizontalPixel();
text.setHorizontalPixel(leftPixel - scrollAmount);
}
if (pt.x > area.width - 3*charWidth) {
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);
}
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,7 +1500,7 @@ void setFont(Font font, int tabs) {
tabWidth = layout.getBounds().width;
layout.dispose();
if (styledText != null) {
try(var gc = GC.create(styledText)) {
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$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,22 @@
* @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 {

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
* (Warning: This field is platform dependent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,22 @@
* @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 {

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
* (Warning: This field is platform dependent)
Expand Down
Loading

0 comments on commit 573159d

Please sign in to comment.