scoop:scoop.ui:0.1.0
src/interaction/contact_press.scoop
Selected production source · src/interaction/contact_press.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.hypot5
import scoop.platform.time.MonotonicTimeSource6
import scoop.surface.*7
import scoop.time.Duration8
import scoop.window.*10
internal class ContactPressSample(val contact: ContactState, val position: Point, val count: Int)12
private class ContactPressCandidate(val start: ContactPressSample, val interval: Duration) {13
var latest = start.contact14
var claimed = false15
}17
/** Text and public hold behaviors share admission and one optional hold deadline. */18
internal class ContactPress(19
private val scope: ComponentScope,20
private val area: InputArea,21
private var enabled: Boolean,22
private var threshold: Double,23
private var delay: Duration?,24
private var multiTap: Boolean,25
private var interval: Duration?,26
private val ready: () -> Boolean,27
private val tap: ((ContactPressSample) -> Boolean)?,28
private val hold: (ContactPressSample) -> Unit29
): Closeable {30
private var closed = false31
private val contacts = ArrayList<ContactId>()32
private var candidate: ContactPressCandidate? = None33
private var taps: ContactTapSequence? = None34
private var timer: WindowDeadline? = None35
private var moved = false36
private val events = area.scope.addEventHandler { context, event -> observe(context, event) }37
private val defaults = area.scope.addDefaultAction { context, event -> handle(context, event) }38
private val finished = area.scope.addEventFinished { event -> finish(event) }39
private val windowEvents =40
scope.observeWindow(scope.nativeWindow) { event -> observeWindow(event) }41
private val activity =42
scope.observeWindowActivity {43
value -> if (!value.focused || !value.visible || value.occluded)44
cancel()45
}47
fun update(48
enabled: Boolean,49
threshold: Double,50
delay: Duration?,51
multiTap: Boolean,52
interval: Duration?53
) {54
if (55
this.enabled !=56
enabled ||57
this.threshold !=58
threshold ||59
this.delay !=60
delay ||61
this.multiTap !=62
multiTap ||63
this.interval !=64
interval65
)66
cancel()67
this.enabled = enabled68
this.threshold = threshold69
this.delay = delay70
this.multiTap = multiTap71
this.interval = interval72
}74
private fun allowed(): Boolean =75
!closed &&76
!area.isClosed &&77
scope.isMounted &&78
enabled &&79
area.scope.isInputAllowed &&80
scope.nativeWindow.state ==81
WindowState.Open &&82
scope.nativeWindow.isFocused &&83
scope.nativeWindow.isVisible &&84
!scope.nativeWindow.isOccluded &&85
ready()87
private fun observe(context: RoutedEventContext, event: InputEvent) {88
if (closed)89
return90
when (event) {91
case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {92
taps = None93
forgetContact(contacts, id)94
if (candidate?.start?.contact?.id == Some(id))95
clear(false)96
}97
case FocusChanged(false) -> {98
if (!area.scope.activity.hasFocus)99
cancel()100
}101
case Contact(InputContactEvent.Began(val value)) -> {102
if (context.phase == RoutedPhase.Notification)103
return104
rememberContact(contacts, value.id)105
if (contacts.size > 1L)106
cancel()107
}108
case Contact(InputContactEvent.Moved(val value)) -> blockHeld(context, value)109
case Contact(InputContactEvent.Ended(val value)) -> blockHeld(context, value)110
case Pointer(InputPointerEvent.Button(_, _, ElementState.Pressed, _)) -> cancel()111
case Pointer(InputPointerEvent.Wheel(_, _, _, _)) -> cancel()112
case Keyboard(val key) -> if (key.state == ElementState.Pressed)113
cancel()114
case TextCommand(_) -> cancel()115
else -> {}116
}117
}119
private fun observeWindow(event: WindowEvent) {120
if (closed || area.isClosed)121
return122
when (event) {123
case Contact(ContactEvent.Began(val value)) -> {124
val point = value.pointer.logicalPosition125
if (!area.hitTest(Point(point.x, point.y)))126
cancel()127
}128
else -> if (interruptsTouch(event)) cancel()129
}130
}132
private fun blockHeld(context: RoutedEventContext, value: ContactState) {133
val current = candidate ?: return134
if (current.claimed && current.start.contact.id == value.id) {135
moved = true136
context.preventDefault()137
}138
}140
private fun handle(context: RoutedEventContext, event: InputEvent) {141
if (closed)142
return143
when (event) {144
case Contact(InputContactEvent.Began(val value)) -> {145
if (contacts.size != 1L || !allowed())146
return147
val point = local(value) ?: return148
val interval =149
this.interval ?:150
scope.interactionPreferences.doubleClickInterval ?:151
Duration.milliseconds(500L)152
val count =153
if (multiTap)154
contactTapCount(area, taps, value, threshold, interval)155
else156
1157
taps = None158
candidate =159
Some(ContactPressCandidate(ContactPressSample(value, point, count), interval))160
when (delay) {161
case Some(val duration) -> {162
val clock = timer ?: scope.createDeadline { expired() }163
timer = Some(clock)164
clock.schedule(MonotonicTimeSource.now() + duration)165
}166
case None -> {}167
}168
}169
case Contact(InputContactEvent.Moved(val value)) -> {170
moved = true171
val current = candidate ?: return172
if (current.start.contact.id != value.id)173
return174
current.latest = value175
when (sample(current, value)) {176
case Some(_) -> {}177
case None -> cancel()178
}179
}180
case Contact(InputContactEvent.Ended(val value)) -> {181
val current = candidate ?: return182
if (current.start.contact.id != value.id)183
return184
val result = sample(current, value)185
clear(false)186
val action = tap ?: return187
when (result) {188
case Some(val value) -> {189
context.consume()190
if (multiTap)191
taps = Some(ContactTapSequence(current.start, current.interval))192
if (!action(value))193
taps = None194
}195
case None -> {}196
}197
}198
else -> {}199
}200
}202
private fun local(value: ContactState): Point? {203
val point = Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)204
return if (area.hitTest(point))205
area.toLocal(point)206
else207
None208
}210
private fun sample(current: ContactPressCandidate, value: ContactState): ContactPressSample? {211
if (!allowed() || contacts.size != 1L)212
return None213
val start = current.start.contact.pointer.logicalPosition214
val end = value.pointer.logicalPosition215
if (hypot(end.x - start.x, end.y - start.y) > threshold)216
return None217
val point = local(value) ?: return None218
val origin = area.toLocal(Point(start.x, start.y)) ?: return None219
val first = current.start.position220
if (hypot(origin.x - first.x, origin.y - first.y) > threshold)221
return None222
return Some(ContactPressSample(value, point, current.start.count))223
}225
private fun expired() {226
val current = candidate ?: return227
val result =228
when (sample(current, current.latest)) {229
case Some(val value) -> value230
case None -> {231
cancel();232
return233
}234
}235
try {236
if (!area.scope.claimContacts([current.start.contact.id])) {237
clear(false)238
return239
}240
current.claimed = true241
if (!same(current) || !allowed()) {242
if (!area.isClosed)243
area.scope.cancelContactClaim(current.start.contact.id)244
clear(false)245
return246
}247
hold(result)248
} catch (error: Throwable) {249
val failure = cleanupUi(Some(error)) { cancel() }250
throwUi(failure)251
}252
}254
private fun same(value: ContactPressCandidate): Boolean =255
when (candidate) {256
case Some(val current) -> current === value257
case None -> false258
}260
private fun finish(event: InputEvent) {261
if (closed)262
return263
when (event) {264
case Contact(InputContactEvent.Moved(val value)) -> {265
if (!moved && candidate?.start?.contact?.id == Some(value.id))266
cancel()267
moved = false268
}269
case Contact(InputContactEvent.Ended(val value)) -> {270
forgetContact(contacts, value.id)271
if (candidate?.start?.contact?.id == Some(value.id))272
clear(false)273
}274
case Contact(InputContactEvent.Began(val value)) -> {275
when (candidate) {276
case Some(val current) -> if (current.start.contact.id != value.id)277
cancel()278
case None -> taps = None279
}280
}281
else -> {}282
}283
}285
fun cancel() {286
if (!closed) {287
taps = None288
clear(true)289
}290
}292
private fun clear(release: Boolean) {293
val previous = candidate294
candidate = None295
moved = false296
timer?.cancel()297
when (previous) {298
case Some(val current) -> if (release && current.claimed && !area.isClosed)299
area.scope.cancelContactClaim(current.start.contact.id)300
case None -> {}301
}302
}304
public override fun close() {305
if (closed)306
return307
closed = true308
taps = None309
var failure = cleanupUi(None) { clear(true) }310
contacts.clear()311
failure = cleanupUi(failure) { timer?.close() }312
failure = cleanupUi(failure) { activity.close() }313
failure = cleanupUi(failure) { windowEvents.close() }314
failure = cleanupUi(failure) { events.close() }315
failure = cleanupUi(failure) { defaults.close() }316
failure = cleanupUi(failure) { finished.close() }317
throwUi(failure)318
}319
}