Scoop API

scoop:scoop.ui:0.1.0

src/native/events.scoop

Selected production source · src/native/events.scoop

1package scoop.ui
3import scoop.collections.HashMap
4import scoop.window.*
6internal class UiEventRegistration<E>(
7 private val checkThread: () -> Unit,
8 action: (E) -> Unit,
9 private val once: Boolean
10): Closeable {
11 private var action: ((E) -> Unit)? = Some(action)
12 var detach: (() -> Unit)? = None
13 var afterClose: (() -> Unit)? = None
15 fun deliver(event: E) {
16 val callback = action ?: return
17 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 = None
28 val remove = detach
29 val finished = afterClose
30 detach = None
31 afterClose = None
32 remove?.invoke()
33 finished?.invoke()
34 }
37/** Each typed table indexes only the resource identity it owns. */
38internal 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] = group
45 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 break
52 }
53 if (group.size == 0L)
54 entries.remove(key)
55 }
56 return registration
57 }
58 fun deliver(key: K, event: E) {
59 val group = entries[key] ?: return
60 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 }
73internal interface UiWindowIntegration : Closeable {
74 fun attached(window: Window)
75 fun detached(window: Window)
76 fun event(event: ApplicationEvent)