-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSwingDispatcher.kt
86 lines (70 loc) · 2.76 KB
/
SwingDispatcher.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.swing
import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import java.awt.event.*
import javax.swing.*
import kotlin.coroutines.*
/**
* Dispatches execution onto Swing event dispatching thread and provides native [delay] support.
*/
@Suppress("unused")
public val Dispatchers.Swing : SwingDispatcher
get() = kotlinx.coroutines.swing.Swing
/**
* Dispatcher for Swing event dispatching thread.
*
* This class provides type-safety and a point for future extensions.
*/
public sealed class SwingDispatcher : MainCoroutineDispatcher(), Delay {
/** @suppress */
override fun dispatch(context: CoroutineContext, block: Runnable): Unit = SwingUtilities.invokeLater(block)
/** @suppress */
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
val timer = schedule(timeMillis) {
with(continuation) { resumeUndispatched(Unit) }
}
continuation.invokeOnCancellation { timer.stop() }
}
/** @suppress */
override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
val timer = schedule(timeMillis) {
block.run()
}
return DisposableHandle { timer.stop() }
}
private fun schedule(timeMillis: Long, action: ActionListener): Timer =
Timer(timeMillis.coerceAtMost(Int.MAX_VALUE.toLong()).toInt(), action).apply {
isRepeats = false
start()
}
}
internal class SwingDispatcherFactory : MainDispatcherFactory {
override val loadPriority: Int
get() = 0
override fun createDispatcher(allFactories: List<MainDispatcherFactory>): MainCoroutineDispatcher = Swing
}
private object ImmediateSwingDispatcher : SwingDispatcher() {
override val immediate: MainCoroutineDispatcher
get() = this
override fun isDispatchNeeded(context: CoroutineContext): Boolean = !SwingUtilities.isEventDispatchThread()
override fun toString() = toStringInternalImpl() ?: "Swing.immediate"
}
/**
* Dispatches execution onto Swing event dispatching thread and provides native [delay] support.
*/
internal object Swing : SwingDispatcher() {
/* A workaround so that the dispatcher's initialization crashes with an exception if running in a headless
environment. This is needed so that this broken dispatcher is not used as the source of delays. */
init {
Timer(1) { }.apply {
isRepeats = false
start()
}
}
override val immediate: MainCoroutineDispatcher
get() = ImmediateSwingDispatcher
override fun toString() = toStringInternalImpl() ?: "Swing"
}