Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/activity.scoop

Selected production source · src/shared/input/activity.scoop

1package scoop.surface
3import scoop.window.ContactId
5/** Ownership that keeps a logical subtree involved in an input session. */
6public struct InputActivity(
7 val hasFocus: Boolean = false,
8 val hasPress: Boolean = false,
9 val hasScrollRoute: Boolean = false,
10 val hasTextInput: Boolean = false,
11 val hasBoundary: Boolean = false,
12 val hasContact: Boolean = false
13) {
14 public val isEmpty: Boolean get() =
15 !hasFocus && !hasPress && !hasScrollRoute && !hasTextInput && !hasBoundary && !hasContact
18internal struct InputContactActivity(val id: ContactId, val scope: InputScope)
20internal class InputActivitySnapshot(
21 val focus: InputScope? = None,
22 val press: InputScope? = None,
23 val scroll: InputScope? = None,
24 val text: InputScope? = None,
25 val boundaries: Array<InputBoundary> = [],
26 val contacts: Array<InputContactActivity> = []
27) {
28 val value: InputActivity get() =
29 InputActivity(
30 present(focus),
31 present(press),
32 present(scroll),
33 present(text),
34 boundaries.size > 0L,
35 contacts.size > 0L
36 )
37 fun same(other: InputActivitySnapshot): Boolean {
38 if (boundaries.size != other.boundaries.size || contacts.size != other.contacts.size)
39 return false
40 for (index in 0L ..< boundaries.size)
41 if (boundaries[index] !== other.boundaries[index])
42 return false
43 for (index in 0L ..< contacts.size)
44 if (
45 contacts[index].id !=
46 other.contacts[index].id ||
47 contacts[index].scope !==
48 other.contacts[index].scope
49 )
50 return false
51 return sameScope(focus, other.focus) &&
52 sameScope(press, other.press) &&
53 sameScope(scroll, other.scroll) &&
54 sameScope(text, other.text)
55 }
58private fun present(scope: InputScope?): Boolean =
59 when (scope) {
60 case Some(_) -> true
61 case None -> false
62 }
64private fun insideActivity(scope: InputScope?, boundary: InputScope): InputScope? {
65 var cursor = scope
66 while (true) {
67 val current = cursor ?: return None
68 if (current === boundary)
69 return scope
70 cursor = current.ancestor
71 }
74private fun firstActivity(path: Array<InputScope>?): InputScope? =
75 when (path) {
76 case Some(val value) -> if (value.size > 0L)
77 Some(value[0L])
78 else
79 None
80 case None -> None
81 }
83internal fun InputRouter.activityFor(scope: InputScope): InputActivitySnapshot {
84 checkThread()
85 if (closed || scope.closed)
86 return InputActivitySnapshot()
87 val scroll =
88 when (scrollRoute) {
89 case Some(val route) -> if (!route.cancelled)
90 firstActivity(Some(route.scopes))
91 else
92 None
93 case None -> None
94 }
95 val text =
96 when (ime) {
97 case Off -> None
98 case Opening(val area) -> Some(area.scope)
99 case On(val area) -> Some(area.scope)
100 case Closing(val area) -> Some(area.scope)
101 }
102 val ownedBoundaries = ArrayList<InputBoundary>()
103 for (boundary in boundaries)
104 if (boundary.accepts && present(insideActivity(Some(boundary.input), scope)))
105 ownedBoundaries.add(boundary)
106 val ownedContacts = ArrayList<InputContactActivity>()
107 for (route in contactRoutes)
108 if (
109 !route.cancelled &&
110 route.scopes.size >
111 0L &&
112 present(insideActivity(Some(route.scopes[0L]), scope))
113 )
114 ownedContacts.add(InputContactActivity(route.latest.id, route.scopes[0L]))
115 return InputActivitySnapshot(
116 insideActivity(logicalFocus?.scope, scope),
117 insideActivity(firstActivity(pressed), scope),
118 insideActivity(scroll, scope),
119 insideActivity(text, scope),
120 ownedBoundaries.toArray(),
121 ownedContacts.toArray()
122 )
125internal class InputActivityObserver(val scope: InputScope, val handler: () -> Unit): Closeable {
126 var closed = false
127 var snapshot = scope.owner.activityFor(scope)
128 public override fun close() {
129 val owner = scope.owner
130 owner.checkThread()
131 if (closed)
132 return
133 closed = true
134 var index = 0L
135 while (index < owner.activityObservers.size) {
136 if (owner.activityObservers[index] === this) {
137 owner.activityObservers.removeAt(index)
138 return
139 }
140 index += 1L
141 }
142 }
145internal fun InputRouter.notifyActivity() {
146 for (observer in activityObservers.toArray()) {
147 if (observer.closed)
148 continue
149 val next = activityFor(observer.scope)
150 if (!observer.snapshot.same(next)) {
151 observer.snapshot = next
152 observer.handler()
153 }
154 // A callback may have changed another observer's route. Reconcile before delivery.
155 if (pending || closed)
156 return
157 }
160internal fun InputRouter.retireActivity(scope: InputScope) {
161 for (observer in activityObservers.toArray())
162 if (observer.scope === scope)
163 observer.close()