Skip to content

Commit 9c88dec

Browse files
authored
Kotlin flow update (#19)
2 parents d6ec9ef + 02e7ba2 commit 9c88dec

File tree

16 files changed

+448
-131
lines changed

16 files changed

+448
-131
lines changed

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# DurianSwt releases
22

33
## [Unreleased]
4+
### Added
5+
- `SwtExec` can now plays nicely with Kotlin coroutines.
6+
- `CoatMux.Layer` now exposes its root `Control` instance ([#12](https://github.com/diffplug/durian-swt/issues/12)).
7+
- `VScrollCtl` and `VScrollBubble` for easier VScroll handling ([#10](https://github.com/diffplug/durian-swt/issues/10)).
8+
### Changed
9+
- **BREAKING** `SwtRx` is now based on Kotlin Flow rather than RxJava.
10+
### Removed
11+
- **BREAKING** Removed `ColorPool.getSystemColor()` ([#8](https://github.com/diffplug/durian-swt/issues/8)).
12+
- **BREAKING** Switched type arguments of `TypedDataField` ([#3](https://github.com/diffplug/durian-swt/issues/3)).
13+
- **BREAKING** Removed OSGi metadata.
414

515
## [3.6.1] - 2022-02-18
616
### Fixed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ subprojects { subProject ->
3535
options.linksOffline 'https://help.eclipse.org/2019-12/topic/org.eclipse.platform.doc.isv/reference/api/', rootProject.file('gradle/javadoc-eclipse').absolutePath
3636
}
3737

38-
apply from: 干.file('base/osgi.gradle')
39-
4038
if (subProject.name == 'durian-swt') {
4139
// configured there
4240
} else if (subProject.name == 'durian-swt.os') {

durian-swt/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
apply from: 干.file('base/kotlin.gradle')
2+
13
apply plugin: 'com.diffplug.eclipse.mavencentral'
24
eclipseMavenCentral {
35
release SWT_VERSION, {
@@ -15,6 +17,7 @@ dependencies {
1517
implementation "com.diffplug.durian:durian-concurrent:$VER_DURIAN"
1618
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
1719
compileOnly 'com.google.code.findbugs:annotations:3.0.1'
20+
compileOnly "org.jetbrains:annotations:23.0.0"
1821

1922
testImplementation "junit:junit:$VER_JUNIT"
2023
testImplementation "org.assertj:assertj-core:$VER_ASSERTJ"

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.common.swt
17+
18+
import com.diffplug.common.swt.Coat.Returning
19+
import org.eclipse.swt.widgets.Composite
20+
21+
/**
22+
* A function that can be "put on" a blank [Composite].
23+
*
24+
*
25+
* An SWT Composite is a blank canvas. As such, it's common to write functions
26+
* that look like `void initializeCmp(Composite cmp)`. In order to make higher-order
27+
* functionality, such as a utility for stacking `Composite`s, you need a way to pass
28+
* these kinds of functions as arguments. That's what `Coat` does.
29+
*/
30+
fun interface Coat {
31+
/**
32+
* Populates the given composite.
33+
* Caller promises that the composite has no layout and contains no children.
34+
*/
35+
fun putOn(cmp: Composite)
36+
37+
/** A Coat which returns a handle to the content it created. */
38+
fun interface Returning<T> {
39+
/**
40+
* Populates the given composite, and returns a handle for communicating with the created GUI.
41+
* Caller promises that the composite has no layout, and contains no children.
42+
*/
43+
fun putOn(cmp: Composite): T
44+
45+
companion object {
46+
/** Converts a non-returning Coat to a Coat.Returning. */
47+
fun <T> fromNonReturning(coat: Coat, returnValue: T): Returning<T> {
48+
return Returning { cmp: Composite ->
49+
coat.putOn(cmp)
50+
returnValue
51+
}
52+
}
53+
}
54+
}
55+
56+
companion object {
57+
/** A Coat which does nothing. */
58+
@JvmStatic
59+
fun empty(): Coat {
60+
return EMPTY
61+
}
62+
63+
val EMPTY = Coat { cmp: Composite -> }
64+
}
65+
}

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

Lines changed: 8 additions & 3 deletions
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.
@@ -82,6 +82,11 @@ public RxGetter<Boolean> rxOnTop() {
8282
return currentLayer.map(opt -> opt.isPresent() && opt.get() == this);
8383
}
8484

85+
/** The control at the root of this layer. */
86+
public Control getControl() {
87+
return control;
88+
}
89+
8590
/** The handle which was returned by the {@link Coat.Returning}. */
8691
public T getHandle() {
8792
return handle;
@@ -107,7 +112,7 @@ public Chit chit() {
107112

108113
/** Adds a persistent {@link Layer} which will be populated immediately by the given `Coat`, using `value` as the key. */
109114
public <T> Layer<T> addCoat(Coat coat, @Nullable T value) {
110-
return addCoat(Coat.Returning.fromNonReturning(coat, value));
115+
return addCoat(Coat.Returning.Companion.fromNonReturning(coat, value));
111116
}
112117

113118
/** Adds a persistent {@link Layer} which will be populated immediately by the given `Coat.Returning`, using the return value as the key. */
@@ -161,7 +166,7 @@ private <T> T makeTemporary(Layer<T> layer) {
161166

162167
/** Sets the current content of this `CoatMux`, gets disposed as soon as anything else becomes the top layer. */
163168
public void setCoat(Coat coat) {
164-
setCoatReturning(Coat.Returning.fromNonReturning(coat, null));
169+
setCoatReturning(Coat.Returning.Companion.fromNonReturning(coat, null));
165170
}
166171

167172
/** Sets the current content of this `CoatMux`, gets disposed as soon as anything else becomes the top layer. */
Lines changed: 5 additions & 22 deletions
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.
@@ -16,37 +16,20 @@
1616
package com.diffplug.common.swt;
1717

1818

19-
import com.diffplug.common.collect.Maps;
2019
import java.util.HashMap;
21-
import org.eclipse.swt.SWT;
2220
import org.eclipse.swt.graphics.Color;
2321
import org.eclipse.swt.graphics.RGB;
24-
import org.eclipse.swt.widgets.Display;
2522
import org.eclipse.swt.widgets.Widget;
2623

2724
/** Caches {@link Color}s, and automatically manages their disposal. */
2825
public class ColorPool {
29-
private final HashMap<RGB, Color> colorTable = Maps.newHashMap();
30-
private final Display display;
26+
private final HashMap<RGB, Color> colorTable = new HashMap<>();
3127

32-
private ColorPool(Widget parent) {
33-
display = parent.getDisplay();
34-
parent.addListener(SWT.Dispose, e -> colorTable.values().forEach(Color::dispose));
35-
}
28+
private ColorPool() {}
3629

3730
/** Returns a Color for the given RGB value. */
3831
public Color getColor(RGB rgb) {
39-
Color color = colorTable.get(rgb);
40-
if (color == null) {
41-
color = new Color(display, rgb);
42-
colorTable.put(rgb, color);
43-
}
44-
return color;
45-
}
46-
47-
/** Returns a Color for the SWT.COLOR_xxx. */
48-
public Color getSystemColor(int systemColor) {
49-
return display.getSystemColor(systemColor);
32+
return colorTable.computeIfAbsent(rgb, raw -> new Color(raw));
5033
}
5134

5235
/** Returns a ColorPool for the given Widget, creating one if necessary. */
@@ -59,5 +42,5 @@ public static ColorPool forWidget(ControlWrapper wrapper) {
5942
return onePerWidget.forWidget(wrapper.getRootControl());
6043
}
6144

62-
private static final OnePerWidget<Widget, ColorPool> onePerWidget = OnePerWidget.from(ColorPool::new);
45+
private static final OnePerWidget<Widget, ColorPool> onePerWidget = OnePerWidget.from(unused -> new ColorPool());
6346
}

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)