scoop:scoop.surface:0.1.0
src/shared/input/activity.scoop
Selected production source · src/shared/input/activity.scoop
1
package scoop.surface3
import scoop.window.ContactId5
/** Ownership that keeps a logical subtree involved in an input session. */6
public 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 = false13
) {14
public val isEmpty: Boolean get() =15
!hasFocus && !hasPress && !hasScrollRoute && !hasTextInput && !hasBoundary && !hasContact16
}18
internal struct InputContactActivity(val id: ContactId, val scope: InputScope)20
internal 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 > 0L36
)37
fun same(other: InputActivitySnapshot): Boolean {38
if (boundaries.size != other.boundaries.size || contacts.size != other.contacts.size)39
return false40
for (index in 0L ..< boundaries.size)41
if (boundaries[index] !== other.boundaries[index])42
return false43
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].scope49
)50
return false51
return sameScope(focus, other.focus) &&52
sameScope(press, other.press) &&53
sameScope(scroll, other.scroll) &&54
sameScope(text, other.text)55
}56
}58
private fun present(scope: InputScope?): Boolean =59
when (scope) {60
case Some(_) -> true61
case None -> false62
}64
private fun insideActivity(scope: InputScope?, boundary: InputScope): InputScope? {65
var cursor = scope66
while (true) {67
val current = cursor ?: return None68
if (current === boundary)69
return scope70
cursor = current.ancestor71
}72
}74
private fun firstActivity(path: Array<InputScope>?): InputScope? =75
when (path) {76
case Some(val value) -> if (value.size > 0L)77
Some(value[0L])78
else79
None80
case None -> None81
}83
internal 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
else92
None93
case None -> None94
}95
val text =96
when (ime) {97
case Off -> None98
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
)123
}125
internal class InputActivityObserver(val scope: InputScope, val handler: () -> Unit): Closeable {126
var closed = false127
var snapshot = scope.owner.activityFor(scope)128
public override fun close() {129
val owner = scope.owner130
owner.checkThread()131
if (closed)132
return133
closed = true134
var index = 0L135
while (index < owner.activityObservers.size) {136
if (owner.activityObservers[index] === this) {137
owner.activityObservers.removeAt(index)138
return139
}140
index += 1L141
}142
}143
}145
internal fun InputRouter.notifyActivity() {146
for (observer in activityObservers.toArray()) {147
if (observer.closed)148
continue149
val next = activityFor(observer.scope)150
if (!observer.snapshot.same(next)) {151
observer.snapshot = next152
observer.handler()153
}154
// A callback may have changed another observer's route. Reconcile before delivery.155
if (pending || closed)156
return157
}158
}160
internal fun InputRouter.retireActivity(scope: InputScope) {161
for (observer in activityObservers.toArray())162
if (observer.scope === scope)163
observer.close()164
}