Scoop API

scoop:scoop.ui:0.1.0

src/interaction/typeahead.scoop

Selected production source · src/interaction/typeahead.scoop

1package scoop.ui
3import scoop.platform.time.MonotonicTimeSource
4import scoop.surface.*
5import scoop.text.startsWith
6import scoop.time.Duration
7import scoop.window.*
9public 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 }
20/** Unicode text buffering for non-editing lists; matching policy belongs to the caller. */
21public 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: TypeaheadOptions
27): Closeable {
28 private val input = scope.input
29 private var closed = false
30 private val buffer = state("")
31 private var single = ""
32 private var observation: Closeable? = None
33 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.isClosed
49 }
50 public val query: String get() = buffer.value
51 public val isPending: Boolean get() = timer.isScheduled
53 public fun update(
54 source: NavigationSource,
55 currentKey: () -> ViewKey?,
56 onMatch: (ViewKey) -> Unit,
57 options: TypeaheadOptions? = None
58 ) {
59 checkOpen()
60 requireUi(source.count >= 0L, "negative navigation source count")
61 val next = options ?: this.options
62 if (this.source !== source || !next.enabled)
63 clear()
64 this.source = source
65 this.currentKey = currentKey
66 matched = onMatch
67 this.options = next
68 }
69 public fun clear() {
70 checkOverlayMutation()
71 timer.cancel()
72 observation?.close()
73 observation = None
74 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 false
82 requireUi(source.count >= 0L, "negative navigation source count")
83 val repeated = single == text && buffer.value == text
84 val query =
85 if (repeated)
86 text
87 else
88 buffer.value + text
89 if (buffer.value == "")
90 single = text
91 else
92 if (!repeated)
93 single = ""
94 buffer.value = query
95 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 -> None
113 }
114 val result = findTypeahead(source, query, origin, repeated, options.matches) ?: return false
115 matched(navigationKey(source, result))
116 return true
117 }
118 private fun handle(context: RoutedEventContext, event: InputEvent) {
119 if (isClosed || !options.enabled || !options.keyboard || !input.isInputAllowed)
120 return
121 val key =
122 when (event) {
123 case Keyboard(val value) -> value;
124 else -> return
125 }
126 val modifiers = key.modifiers
127 if (
128 key.state !=
129 ElementState.Pressed ||
130 key.synthetic ||
131 (modifiers.altGraph != Some(true) && (modifiers.control || modifiers.alt)) ||
132 modifiers.superKey ||
133 modifiers.function
134 )
135 return
136 val text =
137 when (key.logicalKey) {
138 case Character(val value) -> value;
139 else -> return
140 }
141 if (text == "")
142 return
143 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.isMounted
153 checkOverlayMutation()
154 if (closed)
155 return
156 closed = true
157 var failure = cleanupUi(None) { windowActivity.close() }
158 failure = cleanupUi(failure) { timer.close() }
159 failure = cleanupUi(failure) { observation?.close() }
160 observation = None
161 failure = cleanupUi(failure) { defaults.close() }
162 failure = cleanupUi(failure) { activity.close() }
163 throwUi(failure)
164 }
167public 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) }
177internal fun findTypeahead(
178 source: NavigationSource,
179 query: String,
180 current: Long?,
181 next: Boolean,
182 matches: (String, String) -> Boolean
183): Long? {
184 val total = source.count
185 if (total == 0L)
186 return None
187 val origin = current ?: 0L
188 var index =
189 if (next && current != None)
190 (
191 if (origin + 1L < total)
192 origin + 1L
193 else
194 0L
195 )
196 else
197 origin
198 var visited = 0L
199 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 + 1L
205 else
206 0L
207 visited += 1L
208 }
209 return None