Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/arrow.scoop

Selected production source · src/tree/arrow.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.PI
5import scoop.surface.*
6import scoop.ui.*
8internal class TreeArrowInput(
9 val expanded: Boolean,
10 val enabled: Boolean,
11 val size: Double,
12 val color: Color,
13 val transition: AnimationSpec?
16internal fun <T> treeArrow(input: TreeRowInput<T>): View = element(
17 TreeArrowInput(
18 input.row.expanded, input.plan.input.enabled && input.row.node.enabled,
19 14.0 * input.plan.appearance.density, input
20 .plan
21 .appearance
22 .theme
23 .tokens
24 .palette
25 .muted
26 .foreground,
27 input
28 .plan
29 .appearance
30 .theme
31 .tokens
32 .motion
33 .resolve(input.plan.appearance.theme.tokens.motion.selection)
34 ),
35 { value, scope -> TreeArrow(value, scope) }
38private class TreeArrow(private val props: Props<TreeArrowInput>, scope: ComponentScope) :
39 Element<Unit>(scope) {
40 private val progress = scope.animation(if (props.value.expanded) 1.0 else 0.0)
41 private val shape = scope
42 .resource {
43 path {
44 moveTo(Point(9.0, 6.0));
45 lineTo(Point(15.0, 12.0));
46 lineTo(Point(9.0, 18.0))
47 }
48 }
49 public override fun update(): UiInvalidation {
50 val input = props.value
51 changeProgress(
52 progress, if (input.expanded) 1.0 else 0.0,
53 if (input.enabled) input.transition else None
54 )
55 return UiInvalidation.Layout
56 }
57 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
58 Unit
59 > = LayoutResult(
60 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
61 Unit
62 )
63 public override fun scene(context: SceneContext, value: Unit) {
64 val input = props.value
65 val amount = progress.value * PI / 2.0
66 val angle = if (context.layoutDirection == LayoutDirection.Rtl) PI - amount else amount
67 val size = context.size
68 context
69 .draw { paint ->
70 paint
71 .canvas
72 .withState { canvas ->
73 canvas.scale(size.width / 24.0, size.height / 24.0)
74 canvas.translate(12.0, 12.0);
75 canvas.rotate(angle);
76 canvas.translate(-12.0, -12.0)
77 canvas.drawPath(shape, Paint(input.color, PaintingStyle.Stroke, 2.0))
78 }
79 }
80 }