|
16 | 16 | package com.diffplug.common.swt.widgets; |
17 | 17 |
|
18 | 18 | import com.diffplug.common.base.Preconditions; |
| 19 | +import com.diffplug.common.swt.SwtMisc; |
19 | 20 | import java.util.ArrayList; |
20 | 21 | import java.util.concurrent.atomic.AtomicReference; |
21 | 22 | import java.util.function.Consumer; |
|
24 | 25 | /** |
25 | 26 | * - immediately on app startup, call `MacDeepLink.startCapturingBeforeSwt()` |
26 | 27 | * - once SWT has initialized, call `MacDeepLink.swtHasInitializedBeginReceiving(Consumer<String>)` |
| 28 | + * - all urls which were captured before SWT initialized will be passed immediately (on the SWT thread) |
27 | 29 | * |
28 | 30 | * That's all! Don't do anything else. |
29 | 31 | */ |
30 | 32 | public class MacDeepLink { |
| 33 | + /** |
| 34 | + * state transitions are: |
| 35 | + * - `null` on startup |
| 36 | + * - `startCapturingBeforeSwt()` transitions to an `ArrayList<String>`, backlog urls get added to it |
| 37 | + * - `swtHasInitializedBeginReceiving()` transitions to a `Consumer<String>`, all new urls go there |
| 38 | + */ |
31 | 39 | private static final AtomicReference<@Nullable Object> state = new AtomicReference<>(); |
32 | 40 |
|
33 | | - static { |
34 | | - // Load the native library - try multiple strategies |
| 41 | + public static void startCapturingBeforeSwt() { |
35 | 42 | String libPath = System.getProperty("durian-swt.library.path"); |
36 | 43 | if (libPath != null) { |
37 | 44 | System.load(libPath + "/durian-swt-natives/DeepLinkBridge.dylib"); |
38 | 45 | } else { |
39 | 46 | throw new IllegalArgumentException("You need to set 'durian-swt.library.path'"); |
40 | 47 | } |
41 | | - } |
42 | 48 |
|
43 | | - public static void startCapturingBeforeSwt() { |
44 | 49 | var was = state.getAndSet(new ArrayList<>()); |
45 | 50 | Preconditions.checkArgument(was == null, "`startCapturingBeforeSwt() should be called first`"); |
46 | 51 | nativeBeforeSwt(); |
47 | 52 | } |
48 | 53 |
|
49 | 54 | public static void swtHasInitializedBeginReceiving(Consumer<String> handler) { |
| 55 | + SwtMisc.assertUI(); |
50 | 56 | var was = state.getAndSet(handler); |
51 | 57 | Preconditions.checkArgument(was instanceof ArrayList<?>, "Call `applicationStartBeforeSwt()` first."); |
52 | 58 |
|
53 | 59 | var backlog = (ArrayList<String>) was; |
54 | 60 | backlog.forEach(handler); |
| 61 | + |
55 | 62 | nativeAfterSwt(); |
56 | 63 | } |
57 | 64 |
|
|
0 commit comments