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
+ }
0 commit comments