scoop:scoop.ui.component:0.1.0
src/content/progress_ink.scoop
Selected production source · src/content/progress_ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*9
internal class ProgressInkProps(10
val position: Double?,11
val thickness: Double,12
val color: Color,13
val background: Color,14
val style: StyleOverride,15
val transition: AnimationSpec?,16
val motion: Boolean,17
val period: Duration18
)20
internal class ProgressInk(21
private val props: Props<ProgressInkProps>,22
scope: ComponentScope23
): Element<Unit>(scope) {24
private var loop: LoadingMotion? = None25
private var selection: Animatable<Double>? = None26
private var painter: StylePainter? = None27
private var previous: Double? = None28
private var previousThickness: Double? = None29
private var visible = false30
public override fun onMount() {31
loop = Some(LoadingMotion(scope))32
selection = Some(scope.animation(props.value.position ?: 0.0))33
previous = props.value.position34
painter = Some(scope.resource { StylePainter() })35
}36
public override fun update(): UiInvalidation {37
val input = props.value38
loop!!.update(39
!panelPresent(input.position) && input.motion && input.color.alpha > 0.0,40
input.period41
)42
when (input.position) {43
case Some(val value) -> {44
if (!panelPresent(previous) || !panelPresent(input.transition)) {45
if (selection!!.value != value || selection!!.state == AnimationState.Running)46
selection!!.snapTo(value)47
} else48
if (previous != Some(value))49
selection!!.animateTo(value, input.transition!!)50
}51
case None -> if (panelPresent(previous))52
selection!!.snapTo(0.0)53
}54
previous = input.position55
val changed = previousThickness != Some(input.thickness)56
previousThickness = Some(input.thickness)57
return if (changed)58
UiInvalidation.Layout59
else60
UiInvalidation.Scene61
}62
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<63
Unit64
> =65
LayoutResult(66
LayoutMetrics(67
constraints.constrain(Size(constraints.maxWidth ?: 0.0, props.value.thickness))68
),69
Unit70
)71
public override fun scene(context: SceneContext, measured: Unit) {72
val input = props.value73
val size = context.size74
val bounds = Rect(0.0, 0.0, size.width, size.height)75
val direction = context.layoutDirection76
val indicator =77
progressIndicator(78
size,79
input.position,80
selection!!.value,81
if (input.motion)82
loop!!.phase.value83
else84
0.5,85
direction86
)87
val visual =88
input.style.resolve(89
style {90
this.background = Some(Paint(input.background))91
this.corners = Corners.all(size.height / 2.0)92
this.clipContent = true93
}94
).copy { this.clipContent = true }95
painter!!.scene(context, visual) {96
scene -> scene.draw {97
paint ->98
visible = loadingVisible(paint.canvas, bounds, input.color.alpha)99
if (visible && indicator.width > 0.0 && indicator.height > 0.0)100
paint.canvas.drawRect(indicator, Paint(input.color))101
}102
}103
loadingParticipation(context, loop!!.phase, visible)104
loadingParticipation(context, selection!!, visible)105
}106
}108
internal fun progressIndicator(109
size: Size,110
position: Double?,111
selection: Double,112
phase: Double,113
direction: LayoutDirection114
): Rect {115
val known = panelPresent(position)116
val fraction =117
if (known)118
upperBound(lowerBound(selection, 0.0), 1.0)119
else120
0.3121
val width = size.width * fraction122
val start =123
if (known)124
0.0125
else126
(phase * 1.3 - 0.3) * size.width127
return Rect(128
if (direction == LayoutDirection.Rtl)129
size.width - start - width130
else131
start,132
0.0,133
width,134
size.height135
)136
}