scoop:scoop.ui:0.1.0
src/text/handles/input.scoop
Selected production source · src/text/handles/input.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.math.hypot5
import scoop.surface.*6
import scoop.ui.*7
import scoop.window.*9
internal fun textHandlePoint(value: ContactState): Point =10
Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)12
internal fun textHandleObserve(13
host: TextSelectionHandles,14
context: RoutedEventContext,15
event: InputEvent16
) {17
if (host.closed)18
return19
when (event) {20
case Contact(InputContactEvent.Began(val value)) -> {21
if (context.phase == RoutedPhase.Notification)22
return23
rememberContact(host.contacts, value.id)24
if (host.contacts.size > 1L) {25
// Releasing the first route must not admit this second begin as a fresh tap.26
if (host.contact?.claimed ?: false)27
context.preventDefault()28
host.clear(true)29
}30
}31
case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {32
forgetContact(host.contacts, id)33
if (host.contact?.start?.id == Some(id))34
host.clear(false)35
}36
case FocusChanged(false) -> {37
if (!host.area.scope.activity.hasFocus)38
host.hide()39
}40
else -> {}41
}42
}44
internal fun textHandleWindow(host: TextSelectionHandles, event: WindowEvent) {45
if (host.closed || host.area.isClosed)46
return47
when (event) {48
case Contact(ContactEvent.Began(val value)) -> {49
if (!host.area.hitTest(textHandlePoint(value)))50
host.hide()51
}52
else -> if (interruptsTouch(event)) host.hide()53
}54
}56
internal fun textHandleInput(57
host: TextSelectionHandles,58
area: InputArea,59
anchor: Boolean,60
context: RoutedEventContext,61
event: InputEvent62
) {63
if (host.closed)64
return65
when (event) {66
case Contact(InputContactEvent.Began(val value)) -> {67
if (!host.allowed() || !host.ready() || host.contacts.size != 1L)68
return69
val selection = host.selection()70
val rect =71
host.caret(72
if (anchor)73
selection.anchor74
else75
selection.cursor76
) ?:77
return78
val caret =79
host.area.toWindow(80
Rect(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0, 0.0, 0.0)81
) ?:82
return83
val pointer = textHandlePoint(value)84
host.contact =85
Some(86
TextHandleContact(87
value,88
area,89
anchor,90
selection,91
Point(pointer.x - caret.x, pointer.y - caret.y)92
)93
)94
}95
case Contact(InputContactEvent.Moved(val value)) -> {96
host.moved = true97
val current = host.contact ?: return98
if (current.start.id != value.id)99
return100
current.latest = value101
if (!current.claimed) {102
val start = textHandlePoint(current.start)103
val point = textHandlePoint(value)104
val distance = hypot(point.x - start.x, point.y - start.y)105
if (distance == 0.0 || distance < host.threshold)106
return107
if (108
!host.allowed() ||109
!host.ready() ||110
!current.area.scope.claimContacts([value.id])111
) {112
host.clear(false)113
return114
}115
current.claimed = true116
if (!host.same(current) || !host.allowed()) {117
current.area.scope.cancelContactClaim(value.id)118
return119
}120
host.takeContact()121
if (!host.same(current) || !host.allowed())122
return123
current.scroll = Some(TextHandleScroll(host.focus, host.area, { host.hide() }))124
}125
context.consume()126
current.pendingHit = true127
host.move(current)128
}129
case Contact(InputContactEvent.Ended(val value)) -> {130
val current = host.contact ?: return131
if (current.start.id != value.id)132
return133
if (!current.claimed) {134
host.clear(false)135
return136
}137
current.latest = value138
current.ended = true139
current.pendingHit = true140
context.consume()141
host.move(current)142
}143
else -> {}144
}145
}147
internal fun textHandleFinished(host: TextSelectionHandles, event: InputEvent) {148
if (host.closed)149
return150
when (event) {151
case Contact(InputContactEvent.Moved(val value)) -> {152
if (!host.moved && host.contact?.start?.id == Some(value.id))153
host.clear(true)154
host.moved = false155
}156
case Contact(InputContactEvent.Ended(val value)) -> {157
forgetContact(host.contacts, value.id)158
val current = host.contact ?: return159
if (current.start.id == value.id && !current.ended)160
host.clear(false)161
}162
else -> {}163
}164
}