|
1 | 1 | /*
|
2 |
| - * Copyright 2020 DiffPlug |
| 2 | + * Copyright (C) 2020-2022 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import com.diffplug.common.base.Preconditions;
|
20 | 20 | import org.eclipse.swt.SWT;
|
| 21 | +import org.eclipse.swt.graphics.Point; |
| 22 | +import org.eclipse.swt.graphics.Rectangle; |
21 | 23 | import org.eclipse.swt.layout.FillLayout;
|
22 | 24 | import org.eclipse.swt.layout.GridData;
|
23 | 25 | import org.eclipse.swt.layout.GridLayout;
|
@@ -230,4 +232,58 @@ public static LayoutsRowData newRowPlaceholder(Composite parent) {
|
230 | 232 | Label placeholder = new Label(parent, SWT.NONE);
|
231 | 233 | return setRowData(placeholder);
|
232 | 234 | }
|
| 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 | + } |
233 | 289 | }
|
0 commit comments