scoop:scoop.ui:0.1.0
src/native/events.scoop
Selected production source · src/native/events.scoop
1
package scoop.ui3
import scoop.collections.HashMap4
import scoop.window.*6
internal class UiEventRegistration<E>(7
private val checkThread: () -> Unit,8
action: (E) -> Unit,9
private val once: Boolean10
): Closeable {11
private var action: ((E) -> Unit)? = Some(action)12
var detach: (() -> Unit)? = None13
var afterClose: (() -> Unit)? = None15
fun deliver(event: E) {16
val callback = action ?: return17
if (once)18
close()19
callback(event)20
}21
public override fun close() {22
checkThread()23
when (action) {24
case None -> return;25
case Some(_) -> {}26
}27
action = None28
val remove = detach29
val finished = afterClose30
detach = None31
afterClose = None32
remove?.invoke()33
finished?.invoke()34
}35
}37
/** Each typed table indexes only the resource identity it owns. */38
internal class UiEventTable<K, E>(private val checkThread: () -> Unit) where K: Equals<K>, K: Hash {39
private val entries = HashMap<K, ArrayList<UiEventRegistration<E>>>()41
fun subscribe(key: K, once: Boolean, action: (E) -> Unit): UiEventRegistration<E> {42
val registration = UiEventRegistration(checkThread, action, once)43
val group = entries[key] ?: ArrayList<UiEventRegistration<E>>()44
entries[key] = group45
group.add(registration)46
registration.detach =47
Some {48
for (index in 0L ..< group.size)49
if (group[index] === registration) {50
group.removeAt(index)51
break52
}53
if (group.size == 0L)54
entries.remove(key)55
}56
return registration57
}58
fun deliver(key: K, event: E) {59
val group = entries[key] ?: return60
for (entry in group.toArray())61
entry.deliver(event)62
}63
fun close() {64
val all = ArrayList<UiEventRegistration<E>>()65
for (group in entries.values)66
for (entry in group)67
all.add(entry)68
for (entry in all)69
entry.close()70
}71
}73
internal interface UiWindowIntegration : Closeable {74
fun attached(window: Window)75
fun detached(window: Window)76
fun event(event: ApplicationEvent)77
}