Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation_menu/chevron.scoop

Selected production source · src/navigation_menu/chevron.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
7internal class NavChevronProps(
8 val opened: Boolean,
9 val enabled: Boolean,
10 val size: Double,
11 val color: Color,
12 val transition: AnimationSpec?,
13 val vertical: Boolean
16internal class NavChevron(
17 private val props: Props<NavChevronProps>,
18 scope: ComponentScope
19): Element<Unit>(scope) {
20 private var progress: Animatable<Double>? = None
21 private var chevron: Path? = None
22 private var visible = false
23 public override fun onMount() {
24 progress =
25 Some(
26 scope.animation(
27 if (props.value.opened)
28 1.0
29 else
30 0.0
31 )
32 )
33 chevron =
34 Some(
35 scope.resource {
36 path {
37 moveTo(Point(6.0, 9.0));
38 lineTo(Point(12.0, 15.0));
39 lineTo(Point(18.0, 9.0))
40 }
41 }
42 )
43 }
44 public override fun update(): UiInvalidation {
45 changeProgress(
46 progress!!,
47 if (props.value.opened)
48 1.0
49 else
50 0.0,
51 if (props.value.enabled)
52 props.value.transition
53 else
54 None
55 )
56 return UiInvalidation.Layout
57 }
58 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
59 Unit
60 > =
61 LayoutResult(
62 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
63 Unit
64 )
65 public override fun scene(context: SceneContext, measured: Unit) {
66 val input = props.value
67 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
68 val turn =
69 progress!!.value *
70 3.141592653589793 +
71 if (!input.vertical)
72 0.0
73 else
74 if (context.layoutDirection == LayoutDirection.Rtl)
75 1.5707963267948966
76 else
77 -1.5707963267948966
78 context.draw {
79 paint ->
80 visible = loadingVisible(paint.canvas, bounds, input.color.alpha)
81 if (visible)
82 paint.canvas.withState {
83 canvas ->
84 canvas.scale(bounds.width / 24.0, bounds.height / 24.0)
85 canvas.translate(12.0, 12.0);
86 canvas.rotate(turn);
87 canvas.translate(-12.0, -12.0)
88 canvas.drawPath(chevron!!, Paint(input.color, PaintingStyle.Stroke, 2.0))
89 }
90 }
91 loadingParticipation(context, progress!!, visible)
92 }