-
Notifications
You must be signed in to change notification settings - Fork 47
/
SpinnerContent.kt
109 lines (86 loc) · 3.67 KB
/
SpinnerContent.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jetbrains.livemap.canvascontrols
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.registration.Registration
import jetbrains.datalore.visualization.base.canvas.CanvasControl
import jetbrains.datalore.visualization.base.canvas.CanvasControl.AnimationEventHandler
import jetbrains.datalore.visualization.base.canvas.CanvasControlUtil.setAnimationHandler
import jetbrains.datalore.visualization.base.canvas.Context2d
import jetbrains.datalore.visualization.base.canvas.SingleCanvasControl
import kotlin.math.PI
internal class SpinnerContent : CanvasContent {
private lateinit var registration: Registration
private lateinit var canvasControl: SingleCanvasControl
private lateinit var spinnerCenter: DoubleVector
override fun show(parentControl: CanvasControl) {
canvasControl = SingleCanvasControl(parentControl)
with(canvasControl.createCanvas()) {
context2d.drawStaticElements()
registration = setAnimationHandler(
parentControl,
object : AnimationEventHandler {
override fun onEvent(millisTime: Long): Boolean {
context2d.drawSpinner(millisTime)
takeSnapshot()
.onSuccess { canvasControl.context.drawImage(it, 0.0, 0.0) }
return true
}
}
)
}
}
override fun hide() {
canvasControl.dispose()
registration.dispose()
}
private fun Context2d.drawStaticElements() {
save()
setFont("400 " + FONT_SIZE + "px Helvetica, Arial, sans-serif")
val textWidth = measureText(LOADING_TEXT)
val spinnerWidth = 2 * RADIUS + LINE_WIDTH
val width = spinnerWidth + SPACE + textWidth
val dimension = canvasControl.size
spinnerCenter = DoubleVector((dimension.x - width) / 2 + spinnerWidth / 2, dimension.y / 2.0)
setFillColor(BACKGROUND_COLOR)
fillRect(0.0, 0.0, dimension.x.toDouble(), dimension.y.toDouble())
setTextBaseline(Context2d.TextBaseline.MIDDLE)
setTextAlign(Context2d.TextAlign.LEFT)
setFillColor(FONT_COLOR)
fillText(LOADING_TEXT, (dimension.x + width) / 2 - textWidth, dimension.y / 2.0)
restore()
}
private fun Context2d.drawSpinner(time: Long) {
save()
setFillColor(BACKGROUND_COLOR)
fillRect(
spinnerCenter.x - BACK_RADIUS,
spinnerCenter.y - BACK_RADIUS,
2 * BACK_RADIUS,
2 * BACK_RADIUS
)
drawSpinnerArc(CIRCLE_COLOR, 0.0, 2 * PI)
val angle = 2.0 * PI * (time % LOOP_DURATION).toDouble() / LOOP_DURATION
drawSpinnerArc(ARC_COLOR, angle, ARC_LENGTH)
restore()
}
private fun Context2d.drawSpinnerArc(color: String, startAngle: Double, arcAngle: Double) {
setLineWidth(LINE_WIDTH)
setStrokeColor(color)
beginPath()
arc(spinnerCenter.x, spinnerCenter.y, RADIUS, startAngle, startAngle + arcAngle)
stroke()
}
companion object {
private const val BACKGROUND_COLOR = "#FFFFFF"
private const val LINE_WIDTH = 0.9
private const val RADIUS = 11.5
private const val BACK_RADIUS = RADIUS + LINE_WIDTH
private const val CIRCLE_COLOR = "#E8E8E8"
private const val ARC_COLOR = "#00BFFF"
private const val ARC_LENGTH = PI / 2
private const val LOOP_DURATION: Long = 1000
private const val SPACE = 15.0
private const val LOADING_TEXT = "Loading..."
private const val FONT_SIZE = 12.0
private const val FONT_COLOR = "#616161"
}
}