Skip to content

Commit 87478cd

Browse files
committed
Add Layouts.setSingle and setSingleNoMargin.
1 parent d6ec9ef commit 87478cd

File tree

1 file changed

+57
-1
lines changed
  • durian-swt/src/main/java/com/diffplug/common/swt

1 file changed

+57
-1
lines changed

durian-swt/src/main/java/com/diffplug/common/swt/Layouts.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import com.diffplug.common.base.Preconditions;
2020
import org.eclipse.swt.SWT;
21+
import org.eclipse.swt.graphics.Point;
22+
import org.eclipse.swt.graphics.Rectangle;
2123
import org.eclipse.swt.layout.FillLayout;
2224
import org.eclipse.swt.layout.GridData;
2325
import org.eclipse.swt.layout.GridLayout;
@@ -230,4 +232,58 @@ public static LayoutsRowData newRowPlaceholder(Composite parent) {
230232
Label placeholder = new Label(parent, SWT.NONE);
231233
return setRowData(placeholder);
232234
}
235+
236+
////////////
237+
// Single //
238+
////////////
239+
public static void setSingle(Composite composite) {
240+
Single.child(composite);
241+
composite.setLayout(standardMargin);
242+
}
243+
244+
public static void setSingleNoMargin(Composite composite) {
245+
Single.child(composite);
246+
composite.setLayout(noMargin);
247+
}
248+
249+
private static final Single noMargin = new Single();
250+
private static final Single standardMargin = new Single();
251+
252+
private static class Single extends Layout {
253+
private static Control child(Composite composite) {
254+
Control[] children = composite.getChildren();
255+
if (children.length == 1) {
256+
return children[0];
257+
} else {
258+
throw new IllegalArgumentException("Must have 1 child, had " + children.length);
259+
}
260+
}
261+
262+
@Override
263+
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
264+
Point child = child(composite).computeSize(wHint, hHint, flushCache);
265+
if (this == noMargin) {
266+
return child;
267+
} else {
268+
child.x += Layouts.defaultMargin() * 2;
269+
child.y += Layouts.defaultMargin() * 2;
270+
return child;
271+
}
272+
}
273+
274+
@Override
275+
protected void layout(Composite composite, boolean flushCache) {
276+
Control child = child(composite);
277+
Rectangle area = composite.getClientArea();
278+
if (this == standardMargin) {
279+
area.x += Layouts.defaultMargin();
280+
area.y += Layouts.defaultMargin();
281+
area.width -= 2 * Layouts.defaultMargin();
282+
area.height -= 2 * Layouts.defaultMargin();
283+
area.width = Math.max(area.width, 1);
284+
area.height = Math.max(area.height, 1);
285+
}
286+
child.setBounds(area);
287+
}
288+
}
233289
}

0 commit comments

Comments
 (0)