scoop:scoop.ui.component:0.1.0
src/scroll/feedback.scoop
Selected production source · src/scroll/feedback.scoop
1
package scoop.ui.component3
import scoop.platform.time.MonotonicInstant4
import scoop.graphics.*5
import scoop.platform.time.MonotonicTimeSource6
import scoop.surface.*7
import scoop.time.*8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.window.*12
/** Both axes share one visibility animation, one native deadline and one passive pointer observer. */13
internal class ScrollbarFeedback(14
private val scope: ComponentScope,15
private val scrollState: () -> ScrollState,16
private val props: () -> ViewportScrollbarOptions,17
private val parent: InputArea,18
private val changed: () -> Unit19
): Closeable {20
var options = resolve()21
val opacity =22
scope.animation(23
if (options.autoHide)24
0.025
else26
1.027
)28
private val idle = ScrollbarIdle()29
private val timer = scope.createDeadline { tick() }30
private val probe = scope.createArea()31
private var state = scrollState()32
private var intent: Closeable? = None33
private var closed = false34
private var ready = false35
private var bounds = Rect(0.0, 0.0, 0.0, 0.0)36
private var horizontal: Rect? = None37
private var vertical: Rect? = None38
private var within = false39
private var edge = false40
private var horizontalHover = false41
private var verticalHover = false42
private var focused = false43
private val keys = ArrayList<NamedKey>()44
private var repeating: ScrollbarRepetition? = None45
private var scheduled: MonotonicInstant? = None46
private var windowActivity = scope.nativeWindow.activity48
init {49
probe.ignoreSelf = true50
probe.scope.parent = Some(parent.scope)51
observeState()52
scope.resource { parent.scope.observePointer { event -> pointerEvent(event) } }53
scope.resource { parent.scope.observeActivity { activityChanged() } }54
scope.resource { parent.scope.addEventFinished { event -> finished(event) } }55
scope.observeInteractionPreferences { _ -> update() }56
scope.observeWindowActivity {57
value ->58
val previous = windowActivity59
windowActivity = value60
if (61
previous.focused !=62
value.focused ||63
previous.visible !=64
value.visible ||65
previous.occluded !=66
value.occluded67
)68
activityChanged()69
}70
}72
private fun resolve(): ResolvedScrollbarBehavior {73
val value = props()74
return ResolvedScrollbarBehavior(75
value.scrollbarBehavior,76
scope.interactionPreferences,77
value.theme.scrollbarMetrics(value.scrollbar).thickness,78
value.theme.tokens.motion.preference == MotionPreference.Reduced79
)80
}81
private fun observeState() {82
intent =83
Some(84
state.observeIntent {85
event -> if (!closed && options.input.revealOnScroll) {86
when (event) {87
case User(_) -> wake()88
case Programmatic(_) -> wake()89
case LayoutCorrection(_) -> {}90
}91
}92
}93
)94
}95
fun update() {96
val previous = options97
options = resolve()98
// Newly enabled triggers also apply to the current accepted pointer.99
if (!previous.input.revealOnViewportHover && options.input.revealOnViewportHover)100
within = false101
if (!previous.input.revealOnEdgeHover && options.input.revealOnEdgeHover)102
edge = false103
if (state !== scrollState()) {104
intent?.close()105
state = scrollState()106
ready = false107
horizontal = None108
vertical = None109
repeating = None110
keys.clear()111
observeState()112
}113
if (!allowed())114
keys.clear()115
if (!allowed() || options.input.repeatDelay == None)116
repeating = None117
pointer(probe.pointerPosition)118
changed()119
}120
private fun allowed(): Boolean =121
!closed &&122
scope.isMounted &&123
props().enabled &&124
parent.scope.isInputAllowed &&125
scope.nativeWindow.state ==126
WindowState.Open &&127
scope.nativeWindow.isFocused &&128
scope.nativeWindow.isVisible &&129
!scope.nativeWindow.isOccluded131
private fun presented(): Boolean =132
!closed &&133
scope.isMounted &&134
scope.nativeWindow.state ==135
WindowState.Open &&136
scope.nativeWindow.isVisible &&137
!scope.nativeWindow.isOccluded &&138
probe.visibleBounds !=139
None141
fun hover(orientation: Orientation, value: Boolean) {142
if (orientation == Orientation.Horizontal) {143
if (horizontalHover == value)144
return145
horizontalHover = value146
} else {147
if (verticalHover == value)148
return149
verticalHover = value150
}151
refresh()152
}154
private fun activityChanged() {155
if (closed)156
return157
val next = parent.scope.activity.hasFocus && allowed()158
if (159
next &&160
!focused &&161
parent.isFocusActive &&162
parent.focusOrigin !=163
FocusOrigin.Pointer &&164
options.input.revealOnKeyboard165
)166
wake()167
focused = next168
if (!next)169
keys.clear()170
if (!allowed())171
repeating = None172
refresh()173
}175
private fun finished(event: InputEvent) {176
if (closed)177
return178
when (event) {179
case Keyboard(val key) -> {180
if (key.synthetic)181
return182
when (key.logicalKey) {183
case Named(val name) -> {184
var index = 0L185
while (index < keys.size) {186
if (keys[index] == name)187
keys.removeAt(index)188
else189
index += 1L190
}191
if (192
key.state == ElementState.Pressed && scrollbarHeldKey(name) && allowed()193
)194
keys.add(name)195
}196
else -> {}197
}198
if (199
key.state == ElementState.Pressed && allowed() && options.input.revealOnKeyboard200
)201
wake()202
else203
refresh()204
}205
else -> {}206
}207
}209
private fun pointerEvent(event: PointerObservation) {210
if (closed)211
return212
when (event) {213
case Pointer(PointerEvent.Left(_)) -> pointer(None)214
case Pointer(PointerEvent.Cancelled(_, _)) -> {215
keys.clear()216
repeating = None217
pointer(None)218
}219
case FocusLost -> {220
keys.clear()221
repeating = None222
pointer(None)223
}224
case Closed -> {225
ready = false226
repeating = None227
pointer(None)228
}229
case Pointer(_) -> pointer(probe.pointerPosition)230
}231
}232
private fun pointer(point: Point?) {233
val inside =234
allowed() &&235
ready &&236
parent.scope.isHovered &&237
when (point) {238
case Some(val value) -> bounds.contains(value)239
case None -> false240
}241
val near =242
inside &&243
scrollbarNearEdge(bounds, horizontal, vertical, point!!, options.edgeThickness)244
val reveal =245
(inside && !within && options.input.revealOnViewportHover) ||246
(near && !edge && options.input.revealOnEdgeHover)247
within = inside248
edge = near249
if (reveal)250
wake()251
else252
refresh()253
}254
private fun busy(): Boolean {255
if (!allowed())256
return false257
val activity = parent.scope.activity258
return horizontalHover ||259
verticalHover ||260
activity.hasPress ||261
activity.hasContact ||262
activity.hasScrollRoute ||263
state.isAnimating ||264
keys.size >265
0L ||266
(within && options.input.revealOnViewportHover) ||267
(edge && options.input.revealOnEdgeHover)268
}269
fun wake() {270
if (closed || !allowed())271
return272
configure()273
idle.wake(MonotonicTimeSource.now())274
refresh()275
}276
private fun configure() {277
idle.configure(278
ready && (horizontal != None || vertical != None) && (!options.autoHide || presented()),279
options.autoHide,280
options.input.idleDelay,281
busy(),282
MonotonicTimeSource.now()283
)284
}285
private fun refresh() {286
if (closed || !scope.isMounted)287
return288
configure()289
idle.expire(MonotonicTimeSource.now())290
val target =291
if (idle.visible)292
1.0293
else294
0.0295
val before = opacity.target296
changeProgress(297
opacity,298
target,299
if (ready && presented())300
options.transition(301
if (idle.visible)302
options.input.fadeIn303
else304
options.input.fadeOut,305
true306
)307
else308
None309
)310
schedule()311
if (before != target)312
changed()313
}314
fun repeat(orientation: Orientation, resume: Boolean, step: () -> Boolean) {315
if (!allowed() || options.input.repeatDelay == None || panelPresent(repeating))316
return317
repeating =318
Some(319
ScrollbarRepetition(320
orientation,321
MonotonicTimeSource.now() +322
if (resume)323
options.input.repeatInterval324
else325
options.input.repeatDelay!!,326
step327
)328
)329
schedule()330
}331
fun stopRepeat(orientation: Orientation) {332
if (repeating?.orientation == Some(orientation)) {333
repeating = None334
schedule()335
}336
}337
private fun schedule() {338
var next = idle.deadline339
when (repeating) {340
case Some(val value) -> if (next == None || value.next < next!!)341
next = Some(value.next)342
case None -> {}343
}344
if (next == scheduled)345
return346
scheduled = next347
when (next) {348
case Some(val time) -> timer.schedule(time)349
case None -> timer.cancel()350
}351
}352
private fun tick() {353
scheduled = None354
val now = MonotonicTimeSource.now()355
val repeat = repeating356
when (repeat) {357
case Some(val value) -> if (value.next <= now) {358
value.next = now + options.input.repeatInterval359
if (!allowed() || !value.step()) {360
if (panelPresent(repeating) && repeating!! === value)361
repeating = None362
}363
}364
case None -> {}365
}366
refresh()367
}368
fun scene(context: SceneContext, bounds: Rect, horizontal: Rect?, vertical: Rect?) {369
if (horizontal != None || vertical != None)370
context.place(probe, bounds)371
val owner = state372
context.onFrameComplete {373
frame -> if (374
frame.submission == FrameSubmission.Accepted && !closed && state === owner375
) {376
this.bounds = bounds377
this.horizontal = horizontal378
this.vertical = vertical379
ready = probe.visibleBounds != None380
if (horizontal == None && vertical == None)381
repeating = None382
pointer(probe.pointerPosition)383
}384
}385
}386
public override fun close() {387
if (closed)388
return389
closed = true390
try {391
intent?.close()392
} finally {393
intent = None394
repeating = None395
timer.close()396
}397
}398
}400
private class ScrollbarRepetition(401
val orientation: Orientation,402
var next: MonotonicInstant,403
val step: () -> Boolean404
)406
private fun scrollbarHeldKey(key: NamedKey): Boolean =407
key ==408
NamedKey.ArrowUp ||409
key ==410
NamedKey.ArrowDown ||411
key ==412
NamedKey.ArrowLeft ||413
key ==414
NamedKey.ArrowRight ||415
key ==416
NamedKey.PageUp ||417
key ==418
NamedKey.PageDown ||419
key ==420
NamedKey.Home ||421
key ==422
NamedKey.End ||423
key ==424
NamedKey.Space426
internal fun scrollbarNearEdge(427
bounds: Rect,428
horizontal: Rect?,429
vertical: Rect?,430
point: Point,431
thickness: Double432
): Boolean {433
if (thickness <= 0.0 || !bounds.contains(point))434
return false435
when (horizontal) {436
case Some(val rect) -> if (437
point.x >=438
rect.x &&439
point.x <440
rect.x +441
rect.width &&442
point.y >=443
rect.y +444
rect.height -445
thickness446
)447
return true448
case None -> {}449
}450
when (vertical) {451
case Some(val rect) -> {452
val inside =453
if (rect.x + rect.width / 2.0 < bounds.x + bounds.width / 2.0)454
point.x < rect.x + thickness455
else456
point.x >= rect.x + rect.width - thickness457
if (inside && point.y >= rect.y && point.y < rect.y + rect.height)458
return true459
}460
case None -> {}461
}462
return false463
}