scoop:scoop.ui:0.1.0
src/scroll/pan.scoop
Selected production source · src/scroll/pan.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.abs5
import scoop.math.max6
import scoop.surface.*7
import scoop.window.*9
private class ScrollPanContact(10
val start: ContactState,11
val state: ScrollState,12
val binding: ULong13
) {14
var axes: ScrollAxes? = None15
var claimed = false16
var active = false17
var latest = start18
val velocity = ScrollVelocity()19
init {20
velocity.add(position(start), start.pointer.time)21
}22
fun sample(value: ContactState) {23
latest = value24
velocity.add(position(value), value.pointer.time)25
}26
}28
/** A direct-contact scroll behavior borrowing its mount's area and viewport. */29
public class ScrollPan internal constructor(30
private val area: InputArea,31
private val viewport: ScrollViewport,32
private var axes: ScrollAxes,33
private var chain: ScrollChain,34
private var options: TouchScrollOptions35
): Closeable {36
private var closed = false37
private val contacts = ArrayList<ContactId>()38
private var contact: ScrollPanContact? = None39
private var moved = false40
private var state = viewport.state41
private var applying = false42
private var ownIntent: ULong? = None43
private var intent = state.observeIntent { value -> positionRequested(value) }44
private val events = area.scope.addEventHandler { context, event -> observe(context, event) }45
private val defaults = area.scope.addDefaultAction { context, event -> handle(context, event) }46
private val finished = area.scope.addEventFinished { event -> finish(event) }48
init {49
viewport.watchInput(area.scope)50
}52
public val isPanning: Boolean get() {53
area.isClosed54
return contact?.active ?: false55
}56
public val isClosed: Boolean get() {57
area.isClosed58
return closed59
}61
public fun update(axes: ScrollAxes, chain: ScrollChain, options: TouchScrollOptions) {62
requireUi(!closed && !area.isClosed && viewport.active, "scroll pan behavior is closed")63
val changed =64
this.axes !=65
axes ||66
this.chain !=67
chain ||68
this.options.threshold !=69
options.threshold ||70
this.options.lockAxis !=71
options.lockAxis ||72
this.options.claimAtBoundary !=73
options.claimAtBoundary74
this.axes = axes75
this.chain = chain76
this.options = options77
if (changed || !options.enabled || state !== viewport.state)78
cancel()79
if (state !== viewport.state) {80
intent.close()81
state = viewport.state82
intent = state.observeIntent { value -> positionRequested(value) }83
}84
}86
private fun positionRequested(value: ScrollIntent) {87
when (value) {88
case LayoutCorrection(_) -> return89
else -> {}90
}91
if (applying && ownIntent == None) {92
ownIntent = Some(value.requestRevision)93
return94
}95
if (contact?.active ?: false)96
cancel()97
}99
private fun observe(context: RoutedEventContext, event: InputEvent) {100
if (closed)101
return102
when (event) {103
case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {104
forgetContact(contacts, id)105
if (contact?.start?.id == Some(id))106
clear(false)107
}108
case Contact(InputContactEvent.Began(val value)) -> {109
if (context.phase == RoutedPhase.Notification)110
return111
rememberContact(contacts, value.id)112
if (contacts.size > 1L) {113
cancel()114
return115
}116
if (options.enabled && viewport.active && state.hasSnapshot) {117
state.cancelAnimation()118
contact = Some(ScrollPanContact(value, state, viewport.bindingRevision))119
}120
}121
case Pointer(InputPointerEvent.Wheel(_, _, _, _)) -> clear(true, false)122
case Pointer(InputPointerEvent.Button(_, _, ElementState.Pressed, _)) -> {123
viewport.cancelAnimation()124
cancel()125
}126
else -> {}127
}128
}130
private fun current(value: ContactState): ScrollPanContact? {131
val candidate = contact ?: return None132
if (candidate.start.id != value.id)133
return None134
if (!options.enabled || !viewport.matches(candidate.state, candidate.binding)) {135
cancel()136
return None137
}138
return Some(candidate)139
}141
private fun handle(context: RoutedEventContext, event: InputEvent) {142
if (closed)143
return144
when (event) {145
case Contact(InputContactEvent.Moved(val value)) -> {146
moved = true147
val candidate = current(value) ?: return148
candidate.sample(value)149
if (!candidate.active) {150
if (!selectAxes(candidate, value))151
return152
if (context.contactRemaining == None) {153
if (!options.claimAtBoundary && !canMove(candidate, value)) {154
cancel()155
return156
}157
val acquired = area.scope.claimContacts([value.id])158
if (!acquired || !same(candidate) || !viewport.active || area.isClosed) {159
if (acquired && !area.isClosed)160
area.scope.cancelContactClaim(value.id)161
clear(false)162
return163
}164
candidate.claimed = true165
}166
candidate.active = true167
}168
consume(context, candidate)169
}170
case Contact(InputContactEvent.Ended(val value)) -> {171
val candidate = current(value) ?: return172
candidate.sample(value)173
if (candidate.active)174
consume(context, candidate, true)175
clear(false, false)176
}177
else -> {}178
}179
}181
private fun selectAxes(candidate: ScrollPanContact, value: ContactState): Boolean {182
val start = area.toLocal(position(candidate.start))183
val end = area.toLocal(position(value))184
if (start == None || end == None) {185
cancel()186
return false187
}188
val dx = abs(end!!.x - start!!.x)189
val dy = abs(end!!.y - start!!.y)190
if (!dx.isFinite() || !dy.isFinite()) {191
cancel()192
return false193
}194
if (options.lockAxis) {195
if (max(dx, dy) < options.threshold || max(dx, dy) == 0.0)196
return false197
val horizontal = dx >= dy198
if ((horizontal && !axes.horizontal) || (!horizontal && !axes.vertical)) {199
cancel()200
return false201
}202
candidate.axes =203
Some(204
if (horizontal)205
ScrollAxes.Horizontal206
else207
ScrollAxes.Vertical208
)209
} else {210
val distance =211
max(212
if (axes.horizontal)213
dx214
else215
0.0,216
if (axes.vertical)217
dy218
else219
0.0220
)221
if (distance < options.threshold || distance == 0.0)222
return false223
candidate.axes = Some(axes)224
}225
return true226
}228
private fun canMove(candidate: ScrollPanContact, value: ContactState): Boolean {229
val start = area.toLocal(position(candidate.start)) ?: return false230
val end = area.toLocal(position(value)) ?: return false231
val selected = candidate.axes!!232
val offset = candidate.state.offset233
val maximum = candidate.state.maximum234
return (235
selected.horizontal &&236
((end.x < start.x && offset.x < maximum.x) || (end.x > start.x && offset.x > 0.0))237
) ||238
(239
selected.vertical &&240
(241
(end.y < start.y && offset.y < maximum.y) ||242
(end.y > start.y && offset.y > 0.0)243
)244
)245
}247
private fun consume(248
context: RoutedEventContext,249
candidate: ScrollPanContact,250
released: Boolean = false251
) {252
if (!candidate.claimed && context.contactRemaining == None) {253
cancel()254
return255
}256
val selected = candidate.axes!!257
applying = true258
ownIntent = None259
try {260
viewport.consumeContact(261
context,262
selected,263
if (released)264
Some(ScrollRelease(position(candidate.latest), candidate.velocity.value()))265
else266
None267
)268
} finally {269
applying = false270
ownIntent = None271
}272
if (chain == ScrollChain.Contain && same(candidate))273
context.preventDefault()274
}276
private fun same(candidate: ScrollPanContact): Boolean =277
when (contact) {278
case Some(val current) -> current === candidate279
case None -> false280
}282
private fun finish(event: InputEvent) {283
if (closed)284
return285
when (event) {286
case Contact(InputContactEvent.Moved(val value)) -> {287
if (!moved && contact?.start?.id == Some(value.id))288
cancel()289
moved = false290
}291
case Contact(InputContactEvent.Ended(val value)) -> {292
forgetContact(contacts, value.id)293
if (contact?.start?.id == Some(value.id))294
cancel()295
}296
case Contact(InputContactEvent.Began(val value)) -> {297
when (contact) {298
case Some(val current) -> {299
if (current.start.id != value.id)300
cancel()301
}302
case None -> {}303
}304
}305
else -> {}306
}307
}309
public fun cancel() {310
area.isClosed311
if (!closed)312
clear(true)313
}314
private fun clear(release: Boolean, stopEffect: Boolean = true) {315
val previous = contact316
contact = None317
if (stopEffect && viewport.active)318
viewport.cancelAnimation()319
when (previous) {320
case Some(val value) -> {321
if (release && value.claimed && !area.isClosed)322
area.scope.cancelContactClaim(value.start.id)323
}324
case None -> {}325
}326
}327
public override fun close() {328
area.isClosed329
if (closed)330
return331
closed = true332
var failure = cleanupUi(None) { clear(true) }333
contacts.clear()334
failure = cleanupUi(failure) { intent.close() }335
failure = cleanupUi(failure) { events.close() }336
failure = cleanupUi(failure) { defaults.close() }337
failure = cleanupUi(failure) { finished.close() }338
throwUi(failure)339
}340
}342
private fun position(value: ContactState): Point =343
Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)