scoop:scoop.ui:0.1.0
src/interaction/typeahead.scoop
Selected production source · src/interaction/typeahead.scoop
1
package scoop.ui3
import scoop.platform.time.MonotonicTimeSource4
import scoop.surface.*5
import scoop.text.startsWith6
import scoop.time.Duration7
import scoop.window.*9
public class TypeaheadOptions public constructor(10
public val timeout: Duration = Duration.milliseconds(700L),11
public val enabled: Boolean = true,12
public val keyboard: Boolean = true,13
public val matches: (String, String) -> Boolean = { text, query -> text.startsWith(query) }14
) {15
init {16
requireUi(timeout > Duration.ZERO, "typeahead timeout must be positive")17
}18
}20
/** Unicode text buffering for non-editing lists; matching policy belongs to the caller. */21
public class Typeahead internal constructor(22
private val scope: ComponentScope,23
private var source: NavigationSource,24
private var currentKey: () -> ViewKey?,25
private var matched: (ViewKey) -> Unit,26
private var options: TypeaheadOptions27
): Closeable {28
private val input = scope.input29
private var closed = false30
private val buffer = state("")31
private var single = ""32
private var observation: Closeable? = None33
private val timer = scope.createDeadline { clear() }34
private val defaults = input.addDefaultAction { context, event -> handle(context, event) }35
private val activity =36
input.observeActivity {37
if (!input.activity.hasFocus)38
clear()39
}40
private val windowActivity =41
scope.observeWindowActivity {42
value -> if (!closed && (!value.focused || !value.visible || value.occluded))43
clear()44
}46
public val isClosed: Boolean get() {47
val mounted = scope.isMounted;48
return closed || !mounted || input.isClosed49
}50
public val query: String get() = buffer.value51
public val isPending: Boolean get() = timer.isScheduled53
public fun update(54
source: NavigationSource,55
currentKey: () -> ViewKey?,56
onMatch: (ViewKey) -> Unit,57
options: TypeaheadOptions? = None58
) {59
checkOpen()60
requireUi(source.count >= 0L, "negative navigation source count")61
val next = options ?: this.options62
if (this.source !== source || !next.enabled)63
clear()64
this.source = source65
this.currentKey = currentKey66
matched = onMatch67
this.options = next68
}69
public fun clear() {70
checkOverlayMutation()71
timer.cancel()72
observation?.close()73
observation = None74
if (buffer.value != "")75
buffer.value = ""76
single = ""77
}78
public fun push(text: String): Boolean {79
checkOpen()80
if (!options.enabled || text == "")81
return false82
requireUi(source.count >= 0L, "negative navigation source count")83
val repeated = single == text && buffer.value == text84
val query =85
if (repeated)86
text87
else88
buffer.value + text89
if (buffer.value == "")90
single = text91
else92
if (!repeated)93
single = ""94
buffer.value = query95
timer.schedule(MonotonicTimeSource.now() + options.timeout)96
when (observation) {97
case None -> observation =98
Some(99
input.observePointer {100
event -> when (event) {101
case FocusLost -> clear();102
case Closed -> clear();103
else -> {}104
}105
}106
)107
case Some(_) -> {}108
}109
val origin =110
when (currentKey()) {111
case Some(val key) -> navigationIndex(source, key)112
case None -> None113
}114
val result = findTypeahead(source, query, origin, repeated, options.matches) ?: return false115
matched(navigationKey(source, result))116
return true117
}118
private fun handle(context: RoutedEventContext, event: InputEvent) {119
if (isClosed || !options.enabled || !options.keyboard || !input.isInputAllowed)120
return121
val key =122
when (event) {123
case Keyboard(val value) -> value;124
else -> return125
}126
val modifiers = key.modifiers127
if (128
key.state !=129
ElementState.Pressed ||130
key.synthetic ||131
(modifiers.altGraph != Some(true) && (modifiers.control || modifiers.alt)) ||132
modifiers.superKey ||133
modifiers.function134
)135
return136
val text =137
when (key.logicalKey) {138
case Character(val value) -> value;139
else -> return140
}141
if (text == "")142
return143
context.consume()144
push(text)145
}146
private fun checkOpen() {147
checkOverlayMutation()148
if (isClosed)149
throw IllegalStateException(Some("typeahead is no longer mounted"))150
}151
public override fun close() {152
scope.isMounted153
checkOverlayMutation()154
if (closed)155
return156
closed = true157
var failure = cleanupUi(None) { windowActivity.close() }158
failure = cleanupUi(failure) { timer.close() }159
failure = cleanupUi(failure) { observation?.close() }160
observation = None161
failure = cleanupUi(failure) { defaults.close() }162
failure = cleanupUi(failure) { activity.close() }163
throwUi(failure)164
}165
}167
public fun ComponentScope.typeahead(168
source: NavigationSource,169
currentKey: () -> ViewKey?,170
onMatch: (ViewKey) -> Unit,171
options: TypeaheadOptions = TypeaheadOptions()172
): Typeahead {173
requireUi(source.count >= 0L, "negative navigation source count")174
return resource { Typeahead(this, source, currentKey, onMatch, options) }175
}177
internal fun findTypeahead(178
source: NavigationSource,179
query: String,180
current: Long?,181
next: Boolean,182
matches: (String, String) -> Boolean183
): Long? {184
val total = source.count185
if (total == 0L)186
return None187
val origin = current ?: 0L188
var index =189
if (next && current != None)190
(191
if (origin + 1L < total)192
origin + 1L193
else194
0L195
)196
else197
origin198
var visited = 0L199
while (visited < total) {200
if (source.enabledAt(index) && matches(source.searchTextAt(index), query))201
return Some(index)202
index =203
if (index + 1L < total)204
index + 1L205
else206
0L207
visited += 1L208
}209
return None210
}