scoop:scoop.ui.component:0.1.0
src/select/scroll_button.scoop
Selected production source · src/select/scroll_button.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.platform.time.MonotonicTimeSource5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.window.*10
/** Hover repetition is a native deadline, never a perpetual animation or focus target. */11
internal class SelectScrollButton(12
private val scope: ComponentScope,13
private val read: () -> SelectPanelProps,14
private val down: Boolean15
) {16
private val area = scope.createArea { _, _ -> refresh() }17
private val timer = scope.createDeadline { tick() }18
private val press = scope.press(area, PressOptions(true, false)) { step() }19
private val glyph =20
scope.resource {21
path {22
moveTo(23
Point(24
-4.0,25
if (down)26
-2.027
else28
2.029
)30
)31
lineTo(32
Point(33
0.0,34
if (down)35
2.036
else37
-2.038
)39
)40
lineTo(41
Point(42
4.0,43
if (down)44
-2.045
else46
2.047
)48
)49
}50
}51
private var canScroll = false52
init {53
scope.resource { area.scope.observeActivity { refresh() } }54
scope.observeWindowActivity { _ -> refresh() }55
}56
fun update() {57
press.update(PressOptions(read().popup.options.enabled, false)) { step() }58
area.scope.enabled = read().popup.options.enabled59
refresh()60
}61
private fun allowed(): Boolean =62
scope.isMounted &&63
read().popup.open.value &&64
read().popup.options.enabled &&65
scope.nativeWindow.state ==66
WindowState.Open &&67
scope.nativeWindow.isFocused &&68
scope.nativeWindow.isVisible &&69
!scope.nativeWindow.isOccluded &&70
area.scope.isInputAllowed &&71
area.visibleBounds !=72
None &&73
canScroll74
private fun refresh() {75
if (allowed() && area.scope.isHovered) {76
if (!timer.isScheduled)77
timer.schedule(MonotonicTimeSource.now() + Duration.milliseconds(60L))78
} else79
timer.cancel()80
}81
private fun tick() {82
if (allowed() && area.scope.isHovered)83
step()84
refresh()85
}86
private fun step() {87
if (!allowed())88
return89
val input = read()90
input.popup.scroll.scrollBy(91
Point(92
0.0,93
(94
if (down)95
28.096
else97
-28.098
) *99
input.theme.tokens.typography.body.fontSize /100
14.0101
)102
)103
}104
fun scene(context: SceneContext, bounds: Rect?, snapshot: ScrollSnapshot) {105
val can =106
bounds !=107
None &&108
if (down)109
snapshot.offset.y < snapshot.maximum.y110
else111
snapshot.offset.y > 0.0112
context.onFrameComplete {113
frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {114
canScroll = can;115
refresh()116
}117
}118
when (bounds) {119
case Some(val rect) -> {120
context.place(area, rect)121
val input = read()122
val density = input.theme.tokens.typography.body.fontSize / 14.0123
val color = input.theme.tokens.palette.popover.foreground124
context.draw {125
paint -> paint.canvas.withState {126
canvas ->127
canvas.translate(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0)128
canvas.scale(density, density)129
canvas.drawPath(130
glyph,131
Paint(132
if (can)133
color134
else135
componentForeground(136
input.theme,137
color.withAlpha(color.alpha * 0.3),138
input.theme.tokens.palette.popover.background,139
3.0140
),141
PaintingStyle.Stroke,142
1.3333333333333333143
)144
)145
}146
}147
}148
case None -> {}149
}150
}151
}