-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSwingDispatcher.kt
81 lines (67 loc) · 2.63 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
/*
* Copyright 2016-2020 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 java.util.concurrent.*
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, TimeUnit.MILLISECONDS, ActionListener {
with(continuation) { resumeUndispatched(Unit) }
})
continuation.invokeOnCancellation { timer.stop() }
}
/** @suppress */
override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle {
val timer = schedule(timeMillis, TimeUnit.MILLISECONDS, ActionListener {
block.run()
})
return object : DisposableHandle {
override fun dispose() {
timer.stop()
}
}
}
private fun schedule(time: Long, unit: TimeUnit, action: ActionListener): Timer =
Timer(unit.toMillis(time).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() {
override val immediate: MainCoroutineDispatcher
get() = ImmediateSwingDispatcher
override fun toString() = toStringInternalImpl() ?: "Swing"
}