Scoop API

scoop:scoop.ui.component:0.1.0

src/content/progress_ink.scoop

Selected production source · src/content/progress_ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
9internal 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: Duration
20internal class ProgressInk(
21 private val props: Props<ProgressInkProps>,
22 scope: ComponentScope
23): Element<Unit>(scope) {
24 private var loop: LoadingMotion? = None
25 private var selection: Animatable<Double>? = None
26 private var painter: StylePainter? = None
27 private var previous: Double? = None
28 private var previousThickness: Double? = None
29 private var visible = false
30 public override fun onMount() {
31 loop = Some(LoadingMotion(scope))
32 selection = Some(scope.animation(props.value.position ?: 0.0))
33 previous = props.value.position
34 painter = Some(scope.resource { StylePainter() })
35 }
36 public override fun update(): UiInvalidation {
37 val input = props.value
38 loop!!.update(
39 !panelPresent(input.position) && input.motion && input.color.alpha > 0.0,
40 input.period
41 )
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 } else
48 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.position
55 val changed = previousThickness != Some(input.thickness)
56 previousThickness = Some(input.thickness)
57 return if (changed)
58 UiInvalidation.Layout
59 else
60 UiInvalidation.Scene
61 }
62 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
63 Unit
64 > =
65 LayoutResult(
66 LayoutMetrics(
67 constraints.constrain(Size(constraints.maxWidth ?: 0.0, props.value.thickness))
68 ),
69 Unit
70 )
71 public override fun scene(context: SceneContext, measured: Unit) {
72 val input = props.value
73 val size = context.size
74 val bounds = Rect(0.0, 0.0, size.width, size.height)
75 val direction = context.layoutDirection
76 val indicator =
77 progressIndicator(
78 size,
79 input.position,
80 selection!!.value,
81 if (input.motion)
82 loop!!.phase.value
83 else
84 0.5,
85 direction
86 )
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 = true
93 }
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 }
108internal fun progressIndicator(
109 size: Size,
110 position: Double?,
111 selection: Double,
112 phase: Double,
113 direction: LayoutDirection
114): Rect {
115 val known = panelPresent(position)
116 val fraction =
117 if (known)
118 upperBound(lowerBound(selection, 0.0), 1.0)
119 else
120 0.3
121 val width = size.width * fraction
122 val start =
123 if (known)
124 0.0
125 else
126 (phase * 1.3 - 0.3) * size.width
127 return Rect(
128 if (direction == LayoutDirection.Rtl)
129 size.width - start - width
130 else
131 start,
132 0.0,
133 width,
134 size.height
135 )